use std::collections::BTreeMap;

use proto::{EquipBin, ItemBin, WeaponBin};

use super::Equip;

#[derive(Default)]
pub struct Weapon {
    pub base: Equip,
    pub exp: u32,
    pub level: u32,
    pub promote_level: u32,
    pub affix_map: BTreeMap<u32, u32>,
}

impl Weapon {
    pub fn to_client(&self) -> proto::Item {
        self.base
            .to_client(proto::equip::Detail::Weapon(proto::Weapon {
                level: self.level,
                exp: self.exp,
                promote_level: self.promote_level,
                affix_map: self.affix_map.clone().into_iter().collect(),
                ..Default::default()
            }))
    }

    pub fn from_bin(bin: ItemBin, equip_bin: EquipBin, detail: WeaponBin) -> Self {
        Self {
            level: detail.level,
            exp: detail.exp,
            promote_level: detail.promote_level,
            affix_map: detail.affix_map.into_iter().collect(),
            base: Equip::from_bin(bin, equip_bin),
        }
    }

    pub fn to_bin(&self) -> ItemBin {
        let mut item = self.base.to_bin(self.get_item_type());
        if let Some(proto::item_bin::Detail::Equip(equip)) = item.detail.as_mut() {
            equip.detail = Some(proto::equip_bin::Detail::Weapon(WeaponBin {
                level: self.level,
                promote_level: self.promote_level,
                exp: self.exp,
                affix_map: self.affix_map.clone().into_iter().collect(),
            }))
        }

        item
    }

    pub fn get_item_type(&self) -> u32 {
        4
    }
}
