use data::tables::SectionConfigID;
use proto::{MainCityModelBin, TransformBin};

use super::{math::Vector3f, time::MainCityTime};

pub struct MainCityModel {
    pub position: Vector3f,
    pub rotation: Vector3f,
    pub main_city_time: MainCityTime,
    pub section_id: SectionConfigID,
}

impl MainCityModel {
    pub fn update_position(&mut self, pos: Vec<f64>, rot: Vec<f64>) {
        self.position = Vector3f::from_vec(pos);
        self.rotation = Vector3f::from_vec(rot);
    }

    pub fn switch_section(&mut self, id: SectionConfigID) {
        self.section_id = id;
        self.position = Vector3f::default();
        self.rotation = Vector3f::default();
    }

    pub fn to_bin(&self) -> MainCityModelBin {
        MainCityModelBin {
            transform: Some(TransformBin {
                position: self.position.to_vec(),
                rotation: self.position.to_vec(),
            }),
            time: Some(self.main_city_time.to_bin()),
            section_id: self.section_id.value(),
        }
    }

    pub fn from_bin(bin: MainCityModelBin) -> Self {
        let transform = bin.transform.unwrap_or_default();

        Self {
            position: Vector3f::from_vec(transform.position),
            rotation: Vector3f::from_vec(transform.rotation),
            main_city_time: bin.time.map(MainCityTime::from_bin).unwrap_or_default(),
            section_id: SectionConfigID::new_unchecked(bin.section_id),
        }
    }
}

impl Default for MainCityModel {
    fn default() -> Self {
        Self {
            position: Vector3f::default(),
            rotation: Vector3f::default(),
            main_city_time: MainCityTime::default(),
            section_id: SectionConfigID::new_unchecked(2),
        }
    }
}
