use std::cell::RefCell;
use std::rc::Rc;

use data_loader::Data;
use graphics::colormap::linear::LinearColormap;
use graphics::ppi::PPI;
use graphics::transforms::position::Position;
use graphics::transforms::viewport::Viewport;
use graphics::{AttaWithBuffer, Graphics, AttaWithWindow};
use imgui::Condition;

mod camera;
mod components;
mod data_loader;
mod errors;
mod graphics;
mod support;
mod utils;
use imgui::*;

mod final_pg;

fn main() {
    env_logger::init();

    let transform = Position::new().unwrap();
    let mut viewport = Viewport::new().unwrap();
    viewport.set_global(0.0, 0.0, 600.0, 800.0);

    let mut cmap = LinearColormap::new().unwrap();

    cmap.set_colors(vec![
        [170, 170, 170, 255],
        [0, 34, 255, 255],
        [1, 160, 246, 255],
        [0, 236, 236, 255],
        [0, 216, 0, 255],
        [1, 144, 0, 255],
        [255, 255, 0, 255],
        [231, 192, 0, 255],
        [255, 144, 0, 255],
        [255, 0, 0, 255],
        [214, 0, 0, 255],
        [192, 0, 0, 255],
        [255, 0, 240, 255],
        [150, 0, 180, 255],
    ]);

    cmap.set_range(0.0, 70.0);

    let mut ppi = PPI::new().unwrap();
    ppi.set_transform(&transform);
    ppi.set_viewport(&viewport);
    ppi.set_colormap(Box::new(cmap));

    let ppi = Rc::new(RefCell::new(ppi));

    let registers = vec![ppi.clone()];
    let registers = registers.into_iter().map(|r| {
        r as Rc<RefCell<dyn Graphics>>
    }).collect();


    support::supporter::init(move |run, ui, window, gl| {
        let mut ppi = ppi.borrow_mut();
        let texture = ppi.attach_with_window(gl).unwrap();

        ui.window("test").build(||{
            if ui.button("load") {
                let data = Data::from_path(r#"C:\Users\qwin7\Downloads\ZJSXAA_20230113070200_R.dat.gz"#).unwrap();
                ppi.attach_with_buffer(gl, &data).unwrap();
            }
        });

        ui.window("Test").size([200.0,400.0], imgui::Condition::FirstUseEver).build(|| {
            imgui::Image::new(texture, [200.0, 400.0]).build(ui);
        });

        
    }, 
    registers,
);
}