use tauri::{
    plugin::{Builder, TauriPlugin},
    Manager, Runtime,
};

pub use models::*;

#[cfg(desktop)]
mod desktop;
#[cfg(mobile)]
mod mobile;

mod commands;
mod error;
mod models;

pub use error::{Error, Result};

#[cfg(desktop)]
use desktop::Wallpaper;
#[cfg(mobile)]
use mobile::Wallpaper;

/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the wallpaper APIs.
pub trait WallpaperExt<R: Runtime> {
    fn wallpaper(&self) -> &Wallpaper<R>;
}

impl<R: Runtime, T: Manager<R>> crate::WallpaperExt<R> for T {
    fn wallpaper(&self) -> &Wallpaper<R> {
        self.state::<Wallpaper<R>>().inner()
    }
}

/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
    Builder::new("wallpaper")
        .invoke_handler(tauri::generate_handler![
            commands::get_state,
            commands::get_gallery,
            commands::select_images,
            commands::delete_image,
            commands::get_image_ids,
            commands::delete_images,
            commands::set_image_crop,
            commands::set_setting,
            commands::set_interval,
            commands::next_wallpaper,
            commands::get_immich_connection,
            commands::connect_immich,
            commands::disconnect_immich,
            commands::get_immich_albums,
            commands::get_immich_assets,
            commands::import_immich_assets,
            commands::get_immich_import_progress
        ])
        .setup(|app, api| {
            #[cfg(mobile)]
            let wallpaper = mobile::init(app, api)?;
            #[cfg(desktop)]
            let wallpaper = desktop::init(app, api)?;
            app.manage(wallpaper);
            Ok(())
        })
        .build()
}
