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

pub mod drive;
pub mod sync;

use std::sync::Mutex;
use std::time::Duration;
use sync::AppState;
use tauri::{Manager, WebviewUrl};

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn main() {
    tauri::Builder::default()
        .setup(|app| {
            let window = app.get_window("main").unwrap();

            // Hide window initially
            window.hide()?;

            // Remove decorations initially
            window.set_decorations(false)?;

            // Create a handle to the main window for the delayed show operation
            let window_handle = window.clone();

            // Use a delayed show operation without JS
            std::thread::spawn(move || {
                // Give time for the UI to initialize (adjust duration as needed)
                std::thread::sleep(Duration::from_millis(500));

                // Execute on main thread since window operations are not thread-safe
                let window_for_closure = window_handle.clone();
                let _ = window_handle.run_on_main_thread(move || {
                    window_for_closure.show().expect("Failed to show window");
                    window_for_closure
                        .set_decorations(true)
                        .expect("Failed to set decorations");
                });
            });

            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            // Remove the now-unnecessary JS-dependent commands
            sync::save_config,
            drive::list_files,
            // ... other commands
        ])
        .run(tauri::generate_context!())
        .expect("Failed to run app");
}

// These commands are no longer needed as we're handling the window display
// directly in the setup function
// #[tauri::command]
// fn show_main_window(window: tauri::Window) {
//     window.show().expect("Failed to show window");
// }
//
// #[tauri::command]
// fn set_decorations(window: tauri::Window, value: bool) {
//     window.set_decorations(value).expect("Failed to set decorations");
// }
