use std::collections::HashMap;

use data::prop_type::{PROP_EXP, PROP_LEVEL};
use proto::{avatar_bin::Detail, AvatarBin, FormalAvatarBin};

use crate::creature::property::append_int_prop_value;

use super::avatar::BaseAvatar;

#[derive(Default)]
pub struct FormalAvatar {
    pub base: BaseAvatar,
    pub exp: u32,
    // TODO: fetter, expedition
    pub avatar_repeat_count: u32,
    pub is_focus: bool,
    pub taken_promote_reward_level_list: Vec<u32>,
    pub costume_id: u32,
}

impl FormalAvatar {
    pub fn to_client(&self) -> proto::AvatarInfo {
        let mut map = HashMap::new();
        append_int_prop_value(&mut map, PROP_LEVEL, self.base.level.into());
        append_int_prop_value(&mut map, PROP_EXP, self.exp.into());

        let mut info = self.base.to_client(map);
        info.costume_id = self.costume_id;

        info
    }

    pub fn from_bin(bin: AvatarBin, detail_bin: FormalAvatarBin) -> Self {
        Self {
            base: BaseAvatar::from_bin(bin),
            exp: detail_bin.exp,
            avatar_repeat_count: detail_bin.avatar_repeat_count,
            is_focus: detail_bin.is_focus,
            taken_promote_reward_level_list: detail_bin.taken_promote_reward_level_list,
            costume_id: detail_bin.costume_id,
        }
    }

    pub fn to_bin(&self) -> AvatarBin {
        self.base.to_bin(Detail::FormalAvatar(FormalAvatarBin {
            exp: self.exp,
            avatar_repeat_count: self.avatar_repeat_count,
            is_focus: self.is_focus,
            taken_promote_reward_level_list: self.taken_promote_reward_level_list.clone(),
            costume_id: self.costume_id,
            ..Default::default()
        }))
    }
}
