use trigger_fileconfig::NapFileCfg;

pub trait TemplateExt {
    fn is_player_avatar(&self, avatar_id: i32) -> bool;
    fn procedure_allows_select_role(&self, procedure_id: i32) -> bool;
    fn is_last_procedure(&self, procedure_id: i32) -> bool;
}

impl TemplateExt for NapFileCfg {
    fn is_player_avatar(&self, avatar_id: i32) -> bool {
        self.avatar_base_template_tb()
            .data()
            .unwrap()
            .iter()
            .find_map(|tmpl| (tmpl.id() == avatar_id && tmpl.camp() == 0).then_some(true))
            .unwrap_or(false)
    }

    fn procedure_allows_select_role(&self, procedure_id: i32) -> bool {
        self.procedure_config_template_tb()
            .data()
            .unwrap()
            .iter()
            .find_map(|tmpl| {
                (tmpl.procedure_id() == procedure_id && tmpl.procedure_type() == 4).then_some(true)
            })
            .unwrap_or(false)
    }

    fn is_last_procedure(&self, procedure_id: i32) -> bool {
        // faggots nuked JumpTos from client data btw!

        self.procedure_config_template_tb()
            .data()
            .unwrap()
            .iter()
            .max_by_key(|tmpl| tmpl.procedure_id())
            .unwrap()
            .procedure_id()
            == procedure_id
    }
}
