use data::tables::BattleGroupConfigID;
use proto::DungeonQuestInfo;
use thiserror::Error;

#[derive(Default)]
pub struct DungeonQuestManager {
    inner_quests: Vec<u32>,
}

#[derive(Error, Debug)]
pub enum DungeonQuestError {
    #[error("dungeon inner quest with id {0} is not active")]
    QuestNotActive(u32),
}

impl DungeonQuestManager {
    pub fn new_for_battle_group(battle_group_id: BattleGroupConfigID) -> Self {
        Self {
            inner_quests: vec![battle_group_id.template().battle_event_id.value()],
        }
    }

    pub fn finish_quest(&mut self, quest_id: u32) -> Result<(), DungeonQuestError> {
        let idx = self
            .inner_quests
            .iter()
            .position(|id| *id == quest_id)
            .ok_or(DungeonQuestError::QuestNotActive(quest_id))?;

        self.inner_quests.remove(idx);
        Ok(())
    }

    pub fn has_active_quests(&self) -> bool {
        !self.inner_quests.is_empty()
    }

    pub fn to_client(&self) -> DungeonQuestInfo {
        DungeonQuestInfo {
            inner_quest_id_list: self.inner_quests.clone(),
            ..Default::default()
        }
    }
}
