#![allow(deprecated)]

use coords::proj::Mercator;
use coords::Mapper;
use data::{Npz, Radar2d};
use femtovg::{Color, Paint};
use gtk::prelude::*;
use gtk::{gio, glib, Application, ApplicationWindow};
use relm4::RelmApp;
use std::ptr;
mod components;
mod coords;
mod data;
mod errors;
mod monitor;
mod pipeline;
mod render;
mod window;
use components::app::{AppMode, AppModel};
use monitor::Monitor;
use render::{BackgroundConfig, BackgroundWidget, ForegroundConfig, ForegroundWidget, Render};

const APP_ID: &str = "org.gtk_rs.HelloWorld2";

fn main() {
    // Load GL pointers from epoxy (GL context management library used by GTK).
    {
        #[cfg(target_os = "macos")]
        let library = unsafe { libloading::os::unix::Library::new("libepoxy.0.dylib") }.unwrap();
        #[cfg(all(unix, not(target_os = "macos")))]
        let library = unsafe { libloading::os::unix::Library::new("libepoxy.so.0") }.unwrap();
        #[cfg(windows)]
        let library = libloading::os::windows::Library::open_already_loaded("libepoxy-0.dll")
            .or_else(|_| libloading::os::windows::Library::open_already_loaded("epoxy-0.dll"))
            .unwrap();

        gio::resources_register_include!("p.gresource").expect("Failed to register resources.");

        epoxy::load_with(|name| {
            unsafe { library.get(name.as_bytes()) }
                .map(|symbol| *symbol)
                .unwrap_or(ptr::null())
        });
    }

    // gio::resources_register_include!("monitor.gresource").expect("Failed to register resources");
    // Create a new application
    // let app = Application::builder().application_id(APP_ID).build();
    // // Connect to "activate" signal of `app`
    // app.connect_activate(build_ui);
    // // Run the application
    // app.run()

    let relm = RelmApp::new(APP_ID);
    relm.run::<AppModel>(AppMode::Edit);
}

fn build_ui(app: &Application) {
    // Create a window and set the title
    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .build();

    let mut background_config = BackgroundConfig::new()
        .change_painter(Paint::color(Color::white()))
        .change_show_lat_lines(true)
        .change_show_lon_lines(true);
    let mut foreground_config = ForegroundConfig::new();

    let background_widget = BackgroundWidget::new(background_config);
    let foreground_widget = ForegroundWidget::new(foreground_config);
    let render = Render::new(background_widget, foreground_widget);

    let path = "/Users/ruomu/projects/cinrad_g/test2.npz";
    let data = Radar2d::<i8>::load(path, Npz).unwrap();
    let projection = Mercator::new();

    let mut mapper: Mapper = projection.into();
    mapper.set_lat_range(29.960..30.764);
    mapper.set_lon_range(120.038..120.965);

    render.set_mapper(mapper);
    let monitor = Monitor::new(render);
    monitor.load_data_2d(data).unwrap();

    window.set_child(Some(&monitor));
    window.set_default_width(1000);
    window.set_default_height(800);

    window.present();
}
