use super::ItemUID;

use data::tables::WeaponID;
use proto::{WeaponBin, WeaponInfo};

pub struct Weapon {
    pub uid: ItemUID,
    pub template_id: WeaponID,
    pub star: u32,
    pub level: u32,
    pub exp: u32,
    pub refine_level: u32,
}

impl Weapon {
    pub fn new(template_id: WeaponID, uid: ItemUID) -> Self {
        let template = template_id.template();

        Self {
            template_id,
            uid,
            level: (template.star_limit + 1) * 10,
            star: template.star_limit,
            refine_level: template.refine_limit,
            exp: 0,
        }
    }

    pub fn from_bin(bin: WeaponBin) -> Self {
        Self {
            template_id: WeaponID::new_unchecked(bin.template_id),
            uid: bin.uid.into(),
            star: bin.star,
            level: bin.level,
            exp: bin.exp,
            refine_level: bin.refine_level,
        }
    }

    pub fn to_bin(&self) -> WeaponBin {
        WeaponBin {
            template_id: self.template_id.value(),
            uid: self.uid.value(),
            star: self.star,
            level: self.level,
            exp: self.exp,
            refine_level: self.refine_level,
        }
    }

    pub fn to_client(&self) -> WeaponInfo {
        WeaponInfo {
            template_id: self.template_id.value(),
            uid: self.uid.value(),
            star: self.star,
            level: self.level,
            exp: self.exp,
            refine_level: self.refine_level,
            ..Default::default()
        }
    }
}
