use super::MOC;
use crate::{camera::CameraViewPort, SpaceMoc};
use al_api::moc::MOCOptions;

pub struct MOCHierarchy {
    full_res_depth: u8,
    // MOC at different resolution
    mocs: Vec<MOC>,
    gl: WebGlContext,
}
use al_core::WebGlContext;
impl MOCHierarchy {
    pub fn from_full_res_moc(
        gl: WebGlContext,
        full_res_moc: SpaceMoc,
        options: &MOCOptions,
    ) -> Self {
        let full_res_depth = full_res_moc.depth();

        let mut mocs: Vec<_> = (0..full_res_depth)
            .map(|d| MOC::new(gl.clone(), SpaceMoc(full_res_moc.degraded(d)), options))
            .collect();

        mocs.push(MOC::new(gl.clone(), full_res_moc, options));

        Self {
            mocs,
            full_res_depth,
            gl,
        }
    }

    pub fn set_options(&mut self, options: &MOCOptions) {
        for moc in &mut self.mocs {
            moc.set_options(options, self.gl.clone());
        }
    }

    pub fn select_moc_from_view(&mut self, camera: &mut CameraViewPort) -> &mut MOC {
        /*const MAX_NUM_CELLS_TO_DRAW: usize = 1500;

        let mut d = self.full_res_depth as usize;

        while d > 5 {
            self.mocs[d].cell_indices_in_view(camera);

            let num_cells = self.mocs[d].num_cells_in_view(camera);
            if num_cells < MAX_NUM_CELLS_TO_DRAW {
                break;
            }

            d = d - 1;
        }

        self.mocs[d].cell_indices_in_view(camera);
        &mut self.mocs[d]*/

        let w_screen_px = camera.get_width() as f64;
        let smallest_cell_size_px = 8.0;
        let mut d = self.full_res_depth as usize;

        let hpx_cell_size_rad = (smallest_cell_size_px / w_screen_px) * camera.get_aperture();

        while d > 0 {
            //self.mocs[d].cell_indices_in_view(camera);

            if crate::healpix::utils::MEAN_HPX_CELL_RES[d] > hpx_cell_size_rad {
                break;
            }

            d -= 1;
        }

        &mut self.mocs[d]
    }

    pub fn get_full_moc(&self) -> &SpaceMoc {
        &self.mocs.last().unwrap().moc
    }

    pub fn get_full_res_depth(&self) -> u8 {
        self.full_res_depth
    }
}
