use serde::de::DeserializeOwned;
use tauri::{
    plugin::{PluginApi, PluginHandle},
    AppHandle, Runtime,
};

use crate::models::*;

#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_wallpaper);

// initializes the Kotlin or Swift plugin classes
pub fn init<R: Runtime, C: DeserializeOwned>(
    _app: &AppHandle<R>,
    api: PluginApi<R, C>,
) -> crate::Result<Wallpaper<R>> {
    #[cfg(target_os = "android")]
    let handle = api.register_android_plugin("de.wechselbild.wallpaper", "WallpaperPlugin")?;
    #[cfg(target_os = "ios")]
    let handle = api.register_ios_plugin(init_plugin_wallpaper)?;
    Ok(Wallpaper(handle))
}

/// Access to the wallpaper APIs.
pub struct Wallpaper<R: Runtime>(PluginHandle<R>);

impl<R: Runtime> Wallpaper<R> {
    pub fn get_state(&self) -> crate::Result<WallpaperState> {
        self.0.run_mobile_plugin("getState", ()).map_err(Into::into)
    }
    pub fn get_gallery(&self, payload: GalleryRequest) -> crate::Result<GalleryPage> {
        self.0
            .run_mobile_plugin("getGallery", payload)
            .map_err(Into::into)
    }
    pub fn select_images(&self) -> crate::Result<WallpaperState> {
        self.0
            .run_mobile_plugin("selectImages", ())
            .map_err(Into::into)
    }
    pub fn delete_image(&self, payload: DeleteImageRequest) -> crate::Result<WallpaperState> {
        self.0
            .run_mobile_plugin("deleteImage", payload)
            .map_err(Into::into)
    }
    pub fn get_image_ids(&self) -> crate::Result<Vec<String>> {
        let response: ImageIdsResponse = self.0.run_mobile_plugin("getImageIds", ())?;
        Ok(response.ids)
    }
    pub fn delete_images(&self, payload: DeleteImagesRequest) -> crate::Result<WallpaperState> {
        self.0.run_mobile_plugin("deleteImages", payload).map_err(Into::into)
    }
    pub fn set_image_crop(&self, payload: ImageCropRequest) -> crate::Result<GalleryImage> {
        self.0
            .run_mobile_plugin("setImageCrop", payload)
            .map_err(Into::into)
    }
    pub fn set_setting(&self, payload: SettingRequest) -> crate::Result<WallpaperState> {
        self.0
            .run_mobile_plugin("setSetting", payload)
            .map_err(Into::into)
    }
    pub fn set_interval(&self, payload: IntervalRequest) -> crate::Result<WallpaperState> {
        self.0
            .run_mobile_plugin("setInterval", payload)
            .map_err(Into::into)
    }
    pub fn next_wallpaper(&self) -> crate::Result<WallpaperState> {
        self.0
            .run_mobile_plugin("nextWallpaper", ())
            .map_err(Into::into)
    }
    pub fn get_immich_connection(&self) -> crate::Result<ImmichConnection> {
        self.0.run_mobile_plugin("getImmichConnection", ()).map_err(Into::into)
    }
    pub fn connect_immich(&self, payload: ImmichConnectRequest) -> crate::Result<ImmichConnection> {
        self.0.run_mobile_plugin("connectImmich", payload).map_err(Into::into)
    }
    pub fn disconnect_immich(&self) -> crate::Result<ImmichConnection> {
        self.0.run_mobile_plugin("disconnectImmich", ()).map_err(Into::into)
    }
    pub fn get_immich_albums(&self) -> crate::Result<ImmichAlbumsResponse> {
        self.0.run_mobile_plugin("getImmichAlbums", ()).map_err(Into::into)
    }
    pub fn get_immich_assets(&self, payload: ImmichAssetsRequest) -> crate::Result<ImmichAssetsPage> {
        self.0.run_mobile_plugin("getImmichAssets", payload).map_err(Into::into)
    }
    pub fn import_immich_assets(&self, payload: ImmichImportRequest) -> crate::Result<WallpaperState> {
        self.0.run_mobile_plugin("importImmichAssets", payload).map_err(Into::into)
    }
    pub fn get_immich_import_progress(&self) -> crate::Result<ImmichImportProgress> {
        self.0.run_mobile_plugin("getImmichImportProgress", ()).map_err(Into::into)
    }
}
