use crate::app::states::skirmish_states::{
    Players,
    units::{MinerUnit, Unit},
};

#[derive(Debug, Clone, PartialEq)]
pub enum Units {
    Miner(MinerUnit),
}

impl Units {
    #[inline]
    fn u(&self) -> &dyn Unit {
        match self {
            Self::Miner(m) => m,
        }
    }
}

impl Unit for Units {
    fn get_tag(&self) -> char {
        self.u().get_tag()
    }

    fn get_name(&self) -> &'static str {
        self.u().get_name()
    }

    fn get_owner(&self) -> Players {
        self.u().get_owner()
    }

    fn get_hp(&self) -> u16 {
        self.u().get_hp()
    }

    fn get_max_hp(&self) -> u16 {
        self.u().get_max_hp()
    }

    fn get_can_dig(&self) -> bool {
        self.u().get_can_dig()
    }

    fn get_digging_power(&self) -> f32 {
        self.u().get_digging_power()
    }
}

impl Unit for Option<Units> {
    fn is_unit(&self) -> bool {
        self.as_ref().map_or(false, |u| u.is_unit())
    }

    fn get_tag(&self) -> char {
        self.as_ref().map_or(' ', |u| u.get_tag())
    }

    fn get_name(&self) -> &'static str {
        self.as_ref().map_or("", |u| u.get_name())
    }

    fn get_owner(&self) -> Players {
        self.as_ref().map_or(Players::Unclaimed, |u| u.get_owner())
    }

    fn get_hp(&self) -> u16 {
        self.as_ref().map_or(0, |u| u.get_hp())
    }

    fn get_max_hp(&self) -> u16 {
        self.as_ref().map_or(0, |u| u.get_max_hp())
    }

    fn get_can_dig(&self) -> bool {
        self.as_ref().map_or(false, |u| u.get_can_dig())
    }

    fn get_digging_power(&self) -> f32 {
        self.as_ref().map_or(0.0, |u| u.get_digging_power())
    }
}
