use crate::constants::{common::APP_NAME, window::*};
use crate::window::{util::show_windows_by_label, WindowLabel};
use tauri::{AppHandle, Runtime, WebviewUrl, WebviewWindowBuilder};

/// 根据tauri.conf.json配置创建主窗口
pub async fn create_main_window_impl<R: Runtime>(app: &AppHandle<R>) -> Result<(), String> {
    // 检查是否已经存在主窗口
    if show_windows_by_label(app, WindowLabel::MAIN) {
        return Ok(());
    }

    let label = WindowLabel::MAIN.as_ref();
    // 根据tauri.conf.json配置创建主窗口
    let _window = WebviewWindowBuilder::new(app, label, WebviewUrl::App("index.html".into()))
        .title(APP_NAME)
        .inner_size(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT)
        .resizable(true)
        .fullscreen(false)
        .decorations(false)
        .transparent(true)
        .always_on_top(false)
        .center()
        .shadow(true)
        .devtools(true)
        .zoom_hotkeys_enabled(true)
        .focusable(true)
        .accept_first_mouse(true)
        .visible(true)
        .build()
        .map_err(|e| format!("创建主窗口失败: {}", e))?;

    // 设置窗口圆角效果
    #[cfg(any(target_os = "windows", target_os = "macos"))]
    {
        use tauri::window::{Color, EffectsBuilder};
        let _ = _window.set_effects(Some(EffectsBuilder::new().radius(WINDOW_CORNER_RADIUS).color(Color(0, 0, 0, 255)).build()));
    }

    Ok(())
}
