#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
mod screenshot;

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
mod commands;

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
mod shortcut;

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
mod utils;

mod state;
mod window;

use state::new_shared_watcher_state;
use window::setup_window;

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
pub fn desktop() {
    use tauri::{
        menu::{Menu, MenuItem},
        tray::TrayIconBuilder,
    };

    let watcher_state = new_shared_watcher_state();

    tauri::Builder::default()
        .plugin(tauri_plugin_os::init())
        .plugin(tauri_plugin_log::Builder::new().build())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_store::Builder::new().build())
        .plugin(tauri_plugin_http::init())
        .manage(watcher_state)
        .invoke_handler(tauri::generate_handler![
            commands::handle_selected_folder,
            commands::take_screenshot,
            shortcut::change_shortcut,
            shortcut::unregister_shortcut,
            shortcut::get_current_shortcut,
            shortcut::change_screenshot_shortcut,
            shortcut::unregister_screenshot_shortcut,
            shortcut::get_current_screenshot_shortcut,
        ])
        .setup(|app| {
            setup_window(app)?;

            let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
            let take_screenshot =
                MenuItem::with_id(app, "screenshot", "Screenshot", true, None::<&str>)?;

            let menu = Menu::with_items(app, &[&quit_i, &take_screenshot])?;

            TrayIconBuilder::new()
                .menu(&menu)
                .icon(app.default_window_icon().unwrap().clone())
                .on_menu_event(|app, event| match event.id.as_ref() {
                    "quit" => {
                        app.exit(0);
                    }
                    "screenshot" => {
                        let _ = screenshot::take_area_screenshot(app);
                    }
                    _ => {
                        log::info!("{}", event.id().0)
                    }
                })
                .build(app)?;

            shortcut::enable_shortcut(app);

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
#[cfg(target_os = "ios")]
pub fn mobile() {
    tauri::Builder::default()
        .plugin(tauri_plugin_os::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_store::Builder::new().build())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_ios_shared_token::init())
        .setup(|app| {
            setup_window(app)?;

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running ios tauri application");
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
#[cfg(target_os = "android")]
pub fn mobile() {
    tauri::Builder::default()
        .plugin(tauri_plugin_os::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_store::Builder::new().build())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_sharetarget::init())
        .setup(|app| {
            setup_window(app)?;

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running android tauri application");
}
