mod utils;
use gtk::prelude::*;
use gtk::{gio, glib, Application, ApplicationWindow};
use pipeline::offscreen_renderer::OffscreenRenderer;
use relm4::menu;
use relm4::RelmApp;
use std::ptr;
use tokio::runtime::Runtime;
use utils::creator;
mod chart;
mod components;
mod coords;
mod data;
mod dealers;
mod dynamic_col;
mod errors;
mod pipeline;
mod render;
mod window;
use components::app::{AppMode, AppModel};
use once_cell::sync::Lazy;

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

static RUNTIME: Lazy<Runtime> =
    Lazy::new(|| Runtime::new().expect("Setting up tokio runtime needs to succeed."));

static OFFSCREEN: Lazy<OffscreenRenderer> = Lazy::new(|| OffscreenRenderer::new().expect("Can't create offscreen renderer."));

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("/opt/homebrew/lib/libepoxy.0.dylib") }
                .or(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 = relm4::RelmApp::new(APP_ID);
    relm.run::<AppModel>(AppMode::Edit);
}
