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

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

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

mod final_pg;

type Graphic = Rc<RefCell<dyn Graphics>>;

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

    let mut path = Path::builder().build();

    path.push([-1.0, 0.0, 0.0]);
    path.push([0.0, 0.5, 0.0]);
    path.push([1.0, 1.0, 0.0]);

    path.finish();

    let mut fast_path = AggFastPath::new().unwrap();

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

    fast_path.set_transform(&transform);
    fast_path.set_viewport(&viewport);
    fast_path.append(path);

    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 fast_path = Rc::new(RefCell::new(fast_path));
    let registers = vec![fast_path.clone() as Graphic];

    let mut data =
        Data::from_path(r#"/Users/tsuki/Desktop/ZJSXAA_20230113070200_R.dat.gz"#).unwrap();

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

            ui.window("test").build(|| {
                if ui.button("load") {
                    // ppi.attach_with_buffer(gl, &data).unwrap();
                }

                ui.separator();
                if ui.button("Add Layer") {
                    // ppi.add_ppi_layer(1);
                    // ppi.attach_with_buffer(gl, &data).unwrap();
                }

                if ui.button("Remove Layer") {
                    // ppi.add_ppi_layer(-1);
                    // ppi.attach_with_buffer(gl, &data).unwrap();
                }
            });

            // ui.window("Test")
            //     .size([800.0, 600.0], imgui::Condition::FirstUseEver)
            //     .build(|| {
            //         let window_size = ui.window_size();
            //         // imgui::Image::new(texture, window_size).build(ui);
            //     });
        },
        registers,
    );
}
