// @generated by flare-im-core-sdk-xtask
// Source: bindings/contract/dispatch.json

// Do not edit by hand.
#![allow(clippy::too_many_lines, unused_imports)]

use crate::dispatch_support::*;
use crate::{BindingResponse, binding_operation_not_supported};
use flare_im_core_sdk::Result;
use flare_im_core_sdk::client::api::MediaApi;
use flare_im_core_sdk::serde_json::{self, Value};

pub const MEDIA_DISPATCH_OPERATIONS: &[&str] = &[
    "get_url",
    "temp_download_url",
    "resolve_access",
    "cache_remote",
    "cache_stats",
    "set_cache_max_bytes",
    "set_cache_root",
    "clear_cache",
    "upload_file",
    "upload_image",
    "upload_video",
    "upload_bytes",
    "delete_file",
    "user_download_get_subfolder",
    "user_download_set_subfolder",
    "user_download_get_saved_path",
    "user_download_delete_record",
];

pub fn is_media_operation(operation: &str) -> bool {
    MEDIA_DISPATCH_OPERATIONS.contains(&operation)
}

#[cfg(not(target_arch = "wasm32"))]
async fn dispatch_media_native_only(
    api: &MediaApi,
    operation: &str,
    params: Value,
) -> Result<BindingResponse> {
    match operation {
        "temp_download_url" => {
            let file_id_s = json_string(&params, "fileId")?;
            let expires_in_v = optional_i32(&params, "expiresIn")?.unwrap_or(3600);
            json(
                api.get_temp_url_for_file_download(&file_id_s, expires_in_v)
                    .await?,
            )
        }
        "user_download_get_subfolder" => {
            let subfolder = api.user_download_get_subfolder().await?;
            json(serde_json::json!({ "subfolder": subfolder }))
        }
        "user_download_set_subfolder" => {
            let name_s = string_any(&params, &["name", "subfolder"])?;
            api.user_download_set_subfolder(&name_s).await?;
            Ok(BindingResponse::unit())
        }
        "user_download_get_saved_path" => {
            let download_key_s = json_string(&params, "downloadKey")?;
            let path = api.user_download_get_saved_path(&download_key_s).await?;
            json(serde_json::json!({ "path": path }))
        }
        "user_download_delete_record" => {
            let download_key_s = json_string(&params, "downloadKey")?;
            api.user_download_delete_record(&download_key_s).await?;
            Ok(BindingResponse::unit())
        }
        _ => Err(binding_operation_not_supported(operation)),
    }
}

pub async fn dispatch_media(
    api: &MediaApi,
    operation: &str,
    params: Value,
) -> Result<BindingResponse> {
    #[cfg(not(target_arch = "wasm32"))]
    if matches!(
        operation,
        "temp_download_url"
            | "user_download_get_subfolder"
            | "user_download_set_subfolder"
            | "user_download_get_saved_path"
            | "user_download_delete_record"
    ) {
        return dispatch_media_native_only(api, operation, params).await;
    }

    match operation {
        "get_url" => {
            let file_id_s = string_any(&params, &["fileId", "mediaId"])?;
            let expires_in_v = optional_i32(&params, "expiresIn")?.unwrap_or(3600);
            json(api.get_file_url(&file_id_s, expires_in_v).await?)
        }
        "resolve_access" => {
            let file_id_s = json_string(&params, "fileId")?;
            let expires_in_v = optional_i32(&params, "expiresIn")?.unwrap_or(3600);
            json(api.resolve_media_access(&file_id_s, expires_in_v).await?)
        }
        "cache_remote" => {
            let file_id_s = json_string(&params, "fileId")?;
            let expires_in_v = optional_i32(&params, "expiresIn")?.unwrap_or(3600);
            json(api.cache_remote_media(&file_id_s, expires_in_v).await?)
        }
        "cache_stats" => json(api.media_cache_stats().await?),
        "set_cache_max_bytes" => {
            let max_bytes_v = json_u64(&params, "maxBytes")?;
            api.set_media_cache_max_bytes(max_bytes_v).await?;
            Ok(BindingResponse::unit())
        }
        "set_cache_root" => {
            let absolute_path_o = optional_string_any(&params, &["absolutePath", "path"]);
            api.set_media_cache_root(absolute_path_o.as_deref()).await?;
            Ok(BindingResponse::unit())
        }
        "clear_cache" => {
            api.clear_media_cache().await?;
            Ok(BindingResponse::unit())
        }
        "upload_file" => {
            let path_s = json_string(&params, "path")?;
            let options_o = optional_upload_options(&params, "options")?;
            json(api.upload_file_from_path(&path_s, options_o).await?)
        }
        "upload_image" => {
            let path_s = json_string(&params, "path")?;
            let options_o = optional_upload_options(&params, "options")?;
            json(
                api.upload_image_from_path_with_progress(&path_s, options_o, None)
                    .await?,
            )
        }
        "upload_video" => {
            let path_s = json_string(&params, "path")?;
            let options_o = optional_upload_options(&params, "options")?;
            json(
                api.upload_video_from_path_with_progress(&path_s, options_o, None)
                    .await?,
            )
        }
        "upload_bytes" => {
            let bytes_v = json_bytes_vec(&params, "bytes")?;
            let file_name_s = json_string(&params, "fileName")?;
            let mime_type_s = json_string(&params, "mimeType")?;
            let options_o = optional_upload_options(&params, "options")?;
            json(
                api.upload_bytes(bytes_v, &file_name_s, &mime_type_s, options_o)
                    .await?,
            )
        }
        "delete_file" => {
            let file_id_s = json_string(&params, "fileId")?;
            let hard_delete_v = json_bool(&params, "hardDelete")?;
            let deleted = api.delete_file(&file_id_s, hard_delete_v).await?;
            json(serde_json::json!({ "deleted": deleted }))
        }
        _ => Err(binding_operation_not_supported(operation)),
    }
}

pub async fn dispatch_media_json(
    api: &MediaApi,
    operation: &str,
    params_json: &str,
) -> Result<BindingResponse> {
    let params = dispatch_params_from_json(params_json)?;
    dispatch_media(api, operation, params).await
}
