use tauri::{PhysicalPosition, PhysicalSize, Window};

#[tauri::command]
fn dock_window(window: Window, side: String) -> Result<(), String> {
    let monitor = window
        .current_monitor()
        .map_err(|error| error.to_string())?
        .ok_or_else(|| "无法识别当前屏幕".to_string())?;
    let window_size = window.outer_size().map_err(|error| error.to_string())?;
    let work_area = monitor.work_area();
    let dock_size = PhysicalSize::new(window_size.width, work_area.size.height);
    let x = if side == "left" {
        work_area.position.x
    } else {
        work_area.position.x + work_area.size.width as i32 - dock_size.width as i32
    };

    window
        .set_size(dock_size)
        .map_err(|error| error.to_string())?;
    window
        .set_position(PhysicalPosition::new(x, work_area.position.y))
        .map_err(|error| error.to_string())?;
    window
        .set_always_on_top(true)
        .map_err(|error| error.to_string())?;
    Ok(())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![dock_window])
        .run(tauri::generate_context!())
        .expect("failed to run 森林AI Mini");
}
