use std::collections::BTreeSet;

use data::tables::UnlockConfigID;
use proto::{LockModelBin, UnlockData};

#[derive(Default)]
pub struct LockModel {
    unlock_list: BTreeSet<UnlockConfigID>,
}

impl LockModel {
    pub fn from_bin(bin: LockModelBin) -> Self {
        Self {
            unlock_list: bin
                .unlock_list
                .into_iter()
                .map(UnlockConfigID::new_unchecked)
                .collect(),
        }
    }

    pub fn to_bin(&self) -> LockModelBin {
        LockModelBin {
            unlock_list: self
                .unlock_list
                .clone()
                .into_iter()
                .map(|i| i.value())
                .collect(),
        }
    }

    pub fn to_client(&self) -> UnlockData {
        UnlockData {
            unlock_id_list: self
                .unlock_list
                .clone()
                .into_iter()
                .map(|i| i.value())
                .collect(),
            ..Default::default()
        }
    }

    pub fn add_unlock(&mut self, id: UnlockConfigID) {
        self.unlock_list.insert(id);
    }

    pub fn is_unlock(&self, id: UnlockConfigID) -> bool {
        self.unlock_list.contains(&id)
    }
}
