use crate::errors::*;
use crate::graphics::threed::ThreeD;
use crate::graphics::Config;
use crate::pg::App;
use crate::{data_loader::Data, pg::WindowData};
use glow::NativeTexture;
use imgui::{ImString, Ui};

pub fn base(ui: &Ui, window: &winit::window::Window, run: &mut bool, app: &mut App) {
    ui.window("test")
        .size([300.0, 200.0], imgui::Condition::FirstUseEver)
        .build(|| {
            if ui.button("PPI") {
                let data =
                    load_data(r#"/Users/tsuki/Desktop/ZJSXAA_20230113070200_R.dat.gz"#).unwrap();
                app.create_render_window("ppi", [300.0, 300.0]).unwrap();
                app.create_ppi_render("ppi", None);
                app.set_window_modifer(
                    "ppi",
                    Some(ThreeD::new(1.0, 0.1, 100.0, 45.0).unwrap().into()),
                );
                app.bind_data(0, "ppi", &data).unwrap();

                let (rdpi, adpi, _) = {
                    let ppi = app.ppi_module.borrow_mut();
                    ppi.data_info(&data).unwrap()
                };

                app.set_config("ppi").map(|config| {
                    if let Config::PPI(config) = config {
                        config.rdpi = rdpi;
                        config.adpi = adpi;
                    }
                });
            }
        });

    app.show_window(ui);
    app.destroy_window();
}

fn create_display_window(title: &str, size: [f32; 2], copy_from: Option<ImString>) -> WindowData {
    WindowData::new(ImString::new(title), size, Some(ThreeD::default().into()))
}

fn load_data(path: impl AsRef<std::path::Path>) -> Result<Data> {
    let data = Data::from_path(path)?;
    Ok(data)
}
