use glam::Mat4;

#[derive(Clone, Copy)]
pub struct CameraComponent
{
    pub fov: f32,
    pub aspect: f32,
    pub near: f32,
    pub far: f32,
    pub yaw: f32,
    pub pitch: f32,
    pub is_active: bool,
}

impl CameraComponent
{
    pub fn new(aspect: f32) -> Self
    {
        Self {
            fov: 45.0_f32.to_radians(),
            aspect,
            near: 0.1,
            far: 2000.0,
            yaw: -135.0_f32.to_radians(),
            pitch: -30.0_f32.to_radians(),
            is_active: true,
        }
    }

    pub fn projection_matrix(&self) -> Mat4
    {
        Mat4::perspective_rh(self.fov, self.aspect, self.near, self.far)
    }

    pub fn get_forward(&self) -> glam::Vec3
    {
        glam::Vec3::new(
            self.yaw.cos() * self.pitch.cos(),
            self.pitch.sin(),
            self.yaw.sin() * self.pitch.cos(),
        )
        .normalize()
    }

    pub fn get_right(&self) -> glam::Vec3
    {
        self.get_forward().cross(glam::Vec3::Y).normalize()
    }

    pub fn get_forward_horizontal(&self) -> glam::Vec3
    {
        glam::Vec3::new(self.yaw.cos(), 0.0, self.yaw.sin()).normalize()
    }

    pub fn get_right_horizontal(&self) -> glam::Vec3
    {
        self.get_forward_horizontal()
            .cross(glam::Vec3::Y)
            .normalize()
    }
}
