// @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::ConversationApi;
use flare_im_core_sdk::model::{
    BootstrapHomeTimelineRequest, ConversationListQuery, OpenConversationTimelineRequest,
};
use flare_im_core_sdk::serde_json::{self, Value};

pub const CONVERSATION_DISPATCH_OPERATIONS: &[&str] = &[
    "list",
    "list_by_query",
    "list_including_archived",
    "get",
    "get_one",
    "get_group_by_user_ids",
    "get_multiple",
    "list_paginated",
    "list_raw",
    "bootstrap_home",
    "open_timeline",
    "mark_read",
    "delete",
    "set_pinned",
    "set_muted",
    "set_archived",
    "mark_unread",
    "update_draft",
    "clear_local_chat_history",
];

pub fn is_conversation_operation(operation: &str) -> bool {
    CONVERSATION_DISPATCH_OPERATIONS.contains(&operation)
}

pub async fn dispatch_conversation(
    api: &ConversationApi,
    operation: &str,
    params: Value,
) -> Result<BindingResponse> {
    match operation {
        "list" => {
            let conversations = api.list().await?;
            json(serde_json::json!({ "conversations": conversations }))
        }
        "list_by_query" => {
            let bound_value: ConversationListQuery =
                from_value(params.clone(), "ConversationListQuery")?;
            let conversations = api.list_by_query(bound_value).await?;
            json(serde_json::json!({ "conversations": conversations }))
        }
        "list_including_archived" => {
            let conversations = api.list_including_archived().await?;
            json(serde_json::json!({ "conversations": conversations }))
        }
        "get" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            json(api.get(&conversation_id_s).await?)
        }
        "get_one" => {
            let source_id_s = json_string(&params, "sourceId")?;
            let conversation_type_v = conversation_type(&params)?;
            json(api.get_one(&source_id_s, &conversation_type_v).await?)
        }
        "get_group_by_user_ids" => {
            let user_ids_v = json_vec_string(&params, "userIds")?;
            let display_name_o = optional_string(&params, "displayName");
            json(
                api.get_group_by_user_ids(&user_ids_v, display_name_o.as_deref())
                    .await?,
            )
        }
        "get_multiple" => {
            let conversation_ids_v = json_vec_string(&params, "conversationIds")?;
            let conversations = api.get_multiple(&conversation_ids_v).await?;
            json(serde_json::json!({ "conversations": conversations }))
        }
        "list_paginated" => {
            let cursor_o = optional_string(&params, "cursor");
            let limit_v = optional_u32(&params, "limit")?;
            let conversations = api.list_paginated(cursor_o.as_deref(), limit_v).await?;
            json(serde_json::json!({ "conversations": conversations }))
        }
        "list_raw" => {
            let conversations = api.list_raw().await?;
            json(serde_json::json!({ "conversations": conversations }))
        }
        "bootstrap_home" => {
            let bound_value: BootstrapHomeTimelineRequest =
                from_value(params.clone(), "BootstrapHomeTimelineRequest")?;
            json(api.bootstrap_home(bound_value).await?)
        }
        "open_timeline" => {
            let bound_value: OpenConversationTimelineRequest =
                from_value(params.clone(), "OpenConversationTimelineRequest")?;
            json(api.open_timeline(bound_value).await?)
        }
        "mark_read" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            let read_seq_v = json_u64(&params, "readSeq")?;
            api.mark_read(&conversation_id_s, read_seq_v).await?;
            Ok(BindingResponse::unit())
        }
        "delete" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            api.delete(&conversation_id_s).await?;
            Ok(BindingResponse::unit())
        }
        "set_pinned" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            let pinned_v = json_bool(&params, "pinned")?;
            api.set_pinned(&conversation_id_s, pinned_v).await?;
            Ok(BindingResponse::unit())
        }
        "set_muted" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            let muted_v = json_bool(&params, "muted")?;
            api.set_muted(&conversation_id_s, muted_v).await?;
            Ok(BindingResponse::unit())
        }
        "set_archived" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            let archived_v = json_bool(&params, "archived")?;
            api.set_archived(&conversation_id_s, archived_v).await?;
            Ok(BindingResponse::unit())
        }
        "mark_unread" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            json(api.mark_unread(&conversation_id_s).await?)
        }
        "update_draft" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            let draft_o = optional_string(&params, "draft");
            api.update_draft(&conversation_id_s, draft_o.as_deref())
                .await?;
            Ok(BindingResponse::unit())
        }
        "clear_local_chat_history" => {
            let conversation_id_s = json_string(&params, "conversationId")?;
            api.clear_local_chat_history(&conversation_id_s).await?;
            Ok(BindingResponse::unit())
        }
        _ => Err(binding_operation_not_supported(operation)),
    }
}

pub async fn dispatch_conversation_json(
    api: &ConversationApi,
    operation: &str,
    params_json: &str,
) -> Result<BindingResponse> {
    match operation {
        "list_by_query" => {
            let bound_value: ConversationListQuery =
                from_json_str(params_json, "ConversationListQuery")?;
            let conversations = api.list_by_query(bound_value).await?;
            json(serde_json::json!({ "conversations": conversations }))
        }
        "bootstrap_home" => {
            let bound_value: BootstrapHomeTimelineRequest =
                from_json_str(params_json, "BootstrapHomeTimelineRequest")?;
            json(api.bootstrap_home(bound_value).await?)
        }
        "open_timeline" => {
            let bound_value: OpenConversationTimelineRequest =
                from_json_str(params_json, "OpenConversationTimelineRequest")?;
            json(api.open_timeline(bound_value).await?)
        }
        _ => {
            let params = dispatch_params_from_json(params_json)?;
            dispatch_conversation(api, operation, params).await
        }
    }
}
