use std::collections::HashMap;

use data::tables::{BattleEventConfigID, OnceRewardID};
use proto::{FightDropInfo, FightReward, RewardItems};

pub struct FightDropPool {
    special_drop: Vec<SpecialReward>,
}

pub struct SpecialReward {
    pub reward_id: OnceRewardID,
    pub item_count_map: HashMap<u32, u32>,
}

impl FightDropPool {
    pub fn new(battle_event_id: BattleEventConfigID) -> Self {
        let template = battle_event_id.template();

        Self {
            special_drop: template
                .special_drop
                .iter()
                .map(|id| SpecialReward::new(*id))
                .collect(),
        }
    }

    pub fn to_client(&self) -> FightDropInfo {
        FightDropInfo {
            normal_drop_list: Vec::new(),
            special_drop_list: self
                .special_drop
                .iter()
                .map(SpecialReward::to_client)
                .collect(),
            ..Default::default()
        }
    }
}

impl SpecialReward {
    pub fn new(template_id: OnceRewardID) -> Self {
        let template = template_id.template();

        Self {
            reward_id: template_id,
            item_count_map: template
                .reward_list
                .iter()
                .map(|r| (r.reward_item_id, r.reward_amount))
                .collect(),
        }
    }

    pub fn to_client(&self) -> FightReward {
        FightReward {
            reward_id: self.reward_id.value(),
            fight_reward_map: HashMap::from([(
                0,
                RewardItems {
                    reward_item_map: self.item_count_map.clone(),
                },
            )]),
            ..Default::default()
        }
    }
}
