#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

mod api;
mod config;

use std::sync::atomic::{AtomicU32, Ordering};
use tauri::Manager;
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut};

#[tauri::command]
fn open_devtools(app: tauri::AppHandle) {
    #[cfg(debug_assertions)]
    if let Some(window) = app.get_webview_window("main") {
        let _ = window.open_devtools();
    }
}

fn main() {
    // Define zoom level in steps of 10% (100 = 1.0x)
    static ZOOM_LEVEL: AtomicU32 = AtomicU32::new(100);

    let zoom_in = Shortcut::new(Some(Modifiers::SUPER), Code::Equal); // Cmd + =
    let zoom_out = Shortcut::new(Some(Modifiers::SUPER), Code::Minus); // Cmd + -
    let zoom_reset = Shortcut::new(Some(Modifiers::SUPER), Code::Digit0); // Cmd + 0

    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(
            tauri_plugin_global_shortcut::Builder::new()
                .with_handler(move |app, _shortcut, _event| {
                    let window = app.get_webview_window("main").unwrap();
                    let current = ZOOM_LEVEL.load(Ordering::SeqCst);

                    if _shortcut.id() == zoom_in.id() {
                        let new_zoom = (current + 10).min(200); // Max 200%
                        ZOOM_LEVEL.store(new_zoom, Ordering::SeqCst);
                        let _ = window.set_zoom(new_zoom as f64 / 100.0);
                    } else if _shortcut.id() == zoom_out.id() {
                        let new_zoom = (current - 10).max(50); // Min 50%
                        ZOOM_LEVEL.store(new_zoom, Ordering::SeqCst);
                        let _ = window.set_zoom(new_zoom as f64 / 100.0);
                    } else if _shortcut.id() == zoom_reset.id() {
                        ZOOM_LEVEL.store(100, Ordering::SeqCst);
                        let _ = window.set_zoom(1.0);
                    }
                })
                .build(),
        )
        .setup(move |app| {
            config::init_config();
            app.global_shortcut().register(zoom_in)?;
            app.global_shortcut().register(zoom_out)?;
            app.global_shortcut().register(zoom_reset)?;
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            api::health::get_health,
            api::health::get_health_detailed,
            api::health::get_config_info,
            api::health::get_ingest_stats,
            api::health::get_sftpgo_status,
            api::health::get_inference_health,
            api::search::search_videos,
            api::search::search_chunks,
            api::identity::list_identities,
            api::identity::register_identity,
            api::identity::get_identity_videos,
            api::video::list_videos,
            api::video::get_videos,
            api::video::get_video_faces,
            api::video::get_chunk_detail,
            api::person::get_person_thumbnail,
            api::person::get_person_thumbnail_b64,
            api::translation::translate_text,
            open_devtools,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
