use crate::body::BodyId;
use crate::fleet::{Fleet, FleetId, FleetsManager};
use crate::solar_system::{Kilometers, SolarSystem};
use crate::solar_system::body::OrbitalBody;
use crate::timeman::Second;

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum ContactType
{
    Body,
    Fleet
}

#[derive(Copy, Clone, Eq, PartialEq)]
pub struct MapContact
{
    obj_type: ContactType,
    id: usize
}

impl MapContact
{
    pub fn from_body(
        id: BodyId)
    -> Self {
        Self {
            obj_type: ContactType::Body,
            id: id
        }
    }

    pub fn from_fleet(
        id: FleetId)
    -> Self {
        Self {
            obj_type: ContactType::Fleet,
            id: id
        }
    }

    pub fn body(&self)
    -> Option<BodyId>
    {
        match self.obj_type {
            ContactType::Body => Some(self.id),
            _ => None
        }
    }

    pub fn fleet(&self)
    -> Option<FleetId>
    {
        match self.obj_type {
            ContactType::Fleet => Some(self.id),
            _ => None
        }
    }

    pub fn name<'ss>(
        &self,
        star_system: &'ss SolarSystem,
        fleets_man: &'ss FleetsManager)
    -> &'ss String
    {
        match self.obj_type {
            ContactType::Body => star_system.body(self.id).name(),
            ContactType::Fleet => fleets_man.fleet(self.id).unwrap().name()
        }
    }

    pub fn origin_position<'ss>(
        &self,
        star_system: &'ss SolarSystem,
        fleets_man: &'ss FleetsManager,
        time: Second)
    -> cgmath::Vector3<Kilometers>
    {
        match self.obj_type {
            ContactType::Body => 
                star_system.body(self.id).origin_position(star_system, time),
            ContactType::Fleet =>
                *fleets_man.fleet(self.id).unwrap().origin_position()
        }
    }

    pub fn offset_position<'ss>(
        &self,
        star_system: &'ss SolarSystem,
        fleets_man: &'ss FleetsManager,
        time: Second)
    -> cgmath::Vector3<Kilometers>
    {
        match self.obj_type {
            ContactType::Body => 
                star_system.body(self.id).offset_position(time),
            ContactType::Fleet =>
                *fleets_man.fleet(self.id).unwrap().offset_position()
        }
    }
}
