use crate::{
    datafile::datafile::DataFile,
    targeting::{
        TargetingData,
        conditions::{TarCond, TargetingCondition},
        models::ConditionModel,
    },
};

pub(crate) struct ConditionWrapper {
    inverted: bool,
    condition: Option<TarCond>,
}

impl ConditionWrapper {
    // pub fn new(inverted: bool, condition: Option<TarCond>) -> Self {
    //     return Self { inverted, condition };
    // }

    pub fn check(&self, data: &TargetingData<impl DataFile>) -> bool {
        return self.condition.as_ref().map_or(false, |condition| condition.check(data).unwrap_or(false))
            != self.inverted;
    }

    pub fn from_json(model: ConditionModel) -> Self {
        let inverted = !model.is_include.unwrap_or(true);
        let condition = TarCond::from_json(model);
        return ConditionWrapper { inverted, condition };
    }

    #[cfg(test)]
    pub fn inverted(&self) -> bool {
        return self.inverted;
    }
    #[cfg(test)]
    pub fn condition(&self) -> Option<&TarCond> {
        return self.condition.as_ref();
    }
}
