use std::collections::HashMap;

use data::tables::{BuddyBaseID, RobotBuddyConfigID};
use proto::BuddyUnitInfo;

use crate::logic::{BaseProperty, BuddyTeamType};

pub struct BuddyUnit {
    pub buddy_id: BuddyUnitID,
    pub buddy_team: BuddyTeamType,
    pub override_property_map: HashMap<BaseProperty, i32>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BuddyUnitID {
    Base(BuddyBaseID),
    Robot(RobotBuddyConfigID),
}

impl BuddyUnitID {
    pub fn base_id(&self) -> BuddyBaseID {
        match *self {
            Self::Base(id) => id,
            Self::Robot(id) => id.template().buddy_id,
        }
    }
}

impl BuddyUnit {
    pub fn to_client(&self) -> BuddyUnitInfo {
        BuddyUnitInfo {
            buddy_id: match self.buddy_id {
                BuddyUnitID::Base(id) => id.value(),
                BuddyUnitID::Robot(id) => id.value(),
            },
            r#type: self.buddy_team.to_protocol().into(),
            mp_property_override_map: self
                .override_property_map
                .iter()
                .map(|(prop, value)| (*prop as u32, *value))
                .collect(),
        }
    }
}
