import { Sector } from "../classes/managers/LocationManager";
import { Planet } from "../classes/managers/PlanetManager";
import SystemManager from "../classes/managers/SystemManager";

const PLANET_TO_SYSTEM = 120;
const BETWEEN_PLANETS = 300;
const BETWEEN_SYSTEMS = 1000;
const BETWEEN_SECTORS = 10000;
const BETWEEN_GALAXIES = 100000;

export default function getDistanceBetween(a: Planet | SystemManager | Sector, b: Planet | SystemManager | Sector) {
    if("fields" in a) { // a is planet
        // b is planet
        if("fields" in b) {
            // same planet
            if(a._id.equals(b._id)) return 0;
            // same system
            if(a.system.data._id.equals(b.system.data._id)) return BETWEEN_PLANETS;
            // same sector
            if(a.system.data.sector._id.equals(b.system.data.sector._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS;
            // same galaxy
            if(a.system.data.sector.galaxy._id.equals(b.system.data.sector.galaxy._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
        // b is system
        else if("planets" in b) {
            // same system
            if(a.system.data._id.equals(b.data._id)) return PLANET_TO_SYSTEM;
            // same sector
            if(a.system.data.sector._id.equals(b.data.sector._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
            // same galaxy
            if(a.system.data.sector.galaxy._id.equals(b.data.sector.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
        // b is sector
        else {
            // same sector
            if(a.system.data.sector._id.equals(b._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
            // same galaxy
            if(a.system.data.sector.galaxy._id.equals(b.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
    } else if("planets" in a) { // a is system
        // b is planet
        if("fields" in b) {
            // same system
            if(a.data._id.equals(b.system.data._id)) return PLANET_TO_SYSTEM;
            // same sector
            if(a.data.sector._id.equals(b.system.data.sector._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
            // same galaxy
            if(a.data.sector.galaxy._id.equals(b.system.data.sector.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
        // b is system
        else if("planets" in b) {
            // same system
            if(a.data._id.equals(b.data._id)) return 0;
            // same sector
            if(a.data.sector._id.equals(b.data.sector._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
            // same galaxy
            if(a.data.sector.galaxy._id.equals(b.data.sector.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
        // b is sector
        else {
            // same sector
            if(a.data.sector._id.equals(b._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
            // same galaxy
            if(a.data.sector.galaxy._id.equals(b.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
    } else { // a is sector
        // b is planet
        if("fields" in b) {
            // same sector
            if(a._id.equals(b.system.data.sector._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS;
            // same galaxy
            if(a.galaxy._id.equals(b.system.data.sector.galaxy._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
        // b is system
        else if("planets" in b) {
            // same sector
            if(a._id.equals(b.data.sector._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
            // same galaxy
            if(a.galaxy._id.equals(b.data.sector.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
        // b is sector
        else {
            // same sector
            if(a._id.equals(b._id)) return 0;
            // same galaxy
            if(a.galaxy._id.equals(b.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
            // different galaxy
            return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
        }
    }
}