// @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::MessageBuildApi;
use flare_im_core_sdk::serde_json::{self, Value};
use std::sync::Arc;

pub const MESSAGE_BUILD_OPERATIONS: &[&str] = &[
    "create_text",
    "create_quote",
    "create_thread_reply",
    "create_forward",
    "create_image",
    "create_image_group",
    "create_video",
    "create_audio",
    "create_file",
    "create_emoji",
    "create_location",
    "create_sticker",
    "create_link_card",
    "create_card",
    "create_mini_program",
    "create_rich_doc",
    "create_system",
    "create_notification",
    "create_vote",
    "create_task",
    "create_schedule",
    "create_announcement",
    "create_custom",
    "create_placeholder",
    "create_with_content",
];

pub fn is_message_build_operation(operation: &str) -> bool {
    MESSAGE_BUILD_OPERATIONS.contains(&operation)
}

pub async fn dispatch_message_build(
    api: &Arc<MessageBuildApi>,
    request: Value,
) -> Result<BindingResponse> {
    let operation = json_string(&request, "op")?;
    match operation.as_str() {
        "create_text" => {
            let conversation_id_s = conversation_id(&request)?;
            let text_s = json_string(&request, "text")?;
            let mention_all_v = optional_bool(&request, "mentionAll")?.unwrap_or(false);
            let mention_users_v =
                optional_value::<Vec<String>>(&request, "mentionUsers")?.unwrap_or_default();
            json(
                api.create_text(&conversation_id_s, &text_s, mention_all_v, &mention_users_v)
                    .await?,
            )
        }
        "create_quote" => {
            let conversation_id_s = conversation_id(&request)?;
            let quoted_message_id_s = json_string(&request, "quotedMessageId")?;
            let text_s = json_string(&request, "text")?;
            let quoted_sender_id_o = optional_string(&request, "quotedSenderId");
            let quoted_text_preview_o = optional_string(&request, "quotedTextPreview");
            let quoted_content_v = match &request.get("quotedContent") {
                Some(v) => Some(built_content_from_value(v)?),
                None => None,
            };
            json(
                api.create_quote(
                    &conversation_id_s,
                    &quoted_message_id_s,
                    &text_s,
                    quoted_sender_id_o.as_deref(),
                    quoted_text_preview_o.as_deref(),
                    quoted_content_v,
                )
                .await?,
            )
        }
        "create_thread_reply" => {
            let conversation_id_s = conversation_id(&request)?;
            let thread_id_s = json_string(&request, "threadId")?;
            let text_s = json_string(&request, "text")?;
            json(
                api.create_thread_reply(&conversation_id_s, &thread_id_s, &text_s)
                    .await?,
            )
        }
        "create_forward" => {
            let conversation_id_s = conversation_id(&request)?;
            let merge_v = optional_bool(&request, "merge")?.unwrap_or(false);
            let forward_title_s = json_string(&request, "forwardTitle")
                .or_else(|_| json_string(&request, "title"))?;
            let source_messages_v = json_vec_message(&request, "sourceMessages")?;
            json(
                api.create_forward(
                    &conversation_id_s,
                    merge_v,
                    &forward_title_s,
                    source_messages_v,
                )
                .await?,
            )
        }
        "create_image" => {
            let conversation_id_s = conversation_id(&request)?;
            let image_id_s = json_string(&request, "imageId")?;
            json(api.create_image(&conversation_id_s, &image_id_s).await?)
        }
        "create_image_group" => {
            let conversation_id_s = conversation_id(&request)?;
            let images_v = image_group_images(&request)?;
            let description_v = image_group_description(&request);
            let metadata_v = image_group_metadata(&request)?;
            json(
                api.create_image_group(&conversation_id_s, images_v, description_v, metadata_v)
                    .await?,
            )
        }
        "create_video" => {
            let conversation_id_s = conversation_id(&request)?;
            let video_id_s = json_string(&request, "videoId")?;
            json(api.create_video(&conversation_id_s, &video_id_s).await?)
        }
        "create_audio" => {
            let conversation_id_s = conversation_id(&request)?;
            let audio_id_s = json_string(&request, "audioId")?;
            json(api.create_audio(&conversation_id_s, &audio_id_s).await?)
        }
        "create_file" => {
            let conversation_id_s = conversation_id(&request)?;
            let file_id_s = json_string(&request, "fileId")?;
            json(api.create_file(&conversation_id_s, &file_id_s).await?)
        }
        "create_emoji" => {
            let conversation_id_s = conversation_id(&request)?;
            let emoji_s = json_string(&request, "emoji")?;
            json(api.create_emoji(&conversation_id_s, &emoji_s).await?)
        }
        "create_location" => json(
            api.create_location(build_create_location_request(request.clone())?)
                .await?,
        ),
        "create_sticker" => json(
            api.create_sticker(build_create_sticker_request(request.clone())?)
                .await?,
        ),
        "create_link_card" => {
            let conversation_id_s = conversation_id(&request)?;
            let url_s = json_string(&request, "url")?;
            let title_o = optional_string(&request, "title");
            let description_o = optional_string(&request, "description");
            let thumbnail_url_o = optional_string(&request, "thumbnailUrl");
            let site_name_o = optional_string(&request, "siteName");
            json(
                api.create_link_card(
                    &conversation_id_s,
                    &url_s,
                    title_o.as_deref(),
                    description_o.as_deref(),
                    thumbnail_url_o.as_deref(),
                    site_name_o.as_deref(),
                )
                .await?,
            )
        }
        "create_card" => {
            let conversation_id_s = conversation_id(&request)?;
            let id_s = json_string(&request, "id")?;
            let card_type_o = optional_string(&request, "cardType");
            let title_o = optional_string(&request, "title");
            let subtitle_o = optional_string(&request, "subtitle");
            let avatar_o = optional_string(&request, "avatar");
            json(
                api.create_card(
                    &conversation_id_s,
                    &id_s,
                    card_type_o.as_deref(),
                    title_o.as_deref(),
                    subtitle_o.as_deref(),
                    avatar_o.as_deref(),
                )
                .await?,
            )
        }
        "create_mini_program" => {
            let conversation_id_s = conversation_id(&request)?;
            let app_id_s = json_string(&request, "appId")?;
            let title_o = optional_string(&request, "title");
            let page_path_o = optional_string(&request, "pagePath");
            let thumbnail_url_o = optional_string(&request, "thumbnailUrl");
            let extra_v =
                optional_value::<std::collections::HashMap<String, String>>(&request, "extra")?;
            json(
                api.create_mini_program(
                    &conversation_id_s,
                    &app_id_s,
                    title_o.as_deref(),
                    page_path_o.as_deref(),
                    thumbnail_url_o.as_deref(),
                    extra_v,
                )
                .await?,
            )
        }
        "create_rich_doc" => json(
            api.create_rich_doc(
                from_value::<CreateRichDocJson>(request.clone(), "rich doc create")?.into(),
            )
            .await?,
        ),
        "create_system" => {
            let conversation_id_s = conversation_id(&request)?;
            let event_kind_s = json_string(&request, "eventKind")?;
            let body_s = json_string(&request, "body")?;
            json(
                api.create_system(&conversation_id_s, &event_kind_s, &body_s)
                    .await?,
            )
        }
        "create_notification" => {
            let conversation_id_s = conversation_id(&request)?;
            let title_s = json_string(&request, "title")?;
            let body_s = json_string(&request, "body")?;
            json(
                api.create_notification(&conversation_id_s, &title_s, &body_s)
                    .await?,
            )
        }
        "create_vote" => {
            let conversation_id_s = conversation_id(&request)?;
            let vote_id_s = json_string(&request, "voteId")?;
            let title_s = json_string(&request, "title")?;
            let options_v = json_vec_string(&request, "options")?;
            let participant_user_ids_v =
                optional_value::<Vec<String>>(&request, "participantUserIds")?;
            json(
                api.create_vote(
                    &conversation_id_s,
                    &vote_id_s,
                    &title_s,
                    options_v,
                    participant_user_ids_v,
                )
                .await?,
            )
        }
        "create_task" => {
            let conversation_id_s = conversation_id(&request)?;
            let task_id_s = json_string(&request, "taskId")?;
            let title_s = json_string(&request, "title")?;
            let status_o = optional_string(&request, "status");
            let participant_user_ids_v =
                optional_value::<Vec<String>>(&request, "participantUserIds")?;
            json(
                api.create_task(
                    &conversation_id_s,
                    &task_id_s,
                    &title_s,
                    status_o.as_deref(),
                    participant_user_ids_v,
                )
                .await?,
            )
        }
        "create_schedule" => {
            let conversation_id_s = conversation_id(&request)?;
            let schedule_id_s = json_string(&request, "scheduleId")?;
            let title_s = json_string(&request, "title")?;
            let start_time_ms_v = json_i64(&request, "startTimeMs")?;
            let end_time_ms_v = json_i64(&request, "endTimeMs")?;
            let participant_user_ids_v =
                optional_value::<Vec<String>>(&request, "participantUserIds")?;
            json(
                api.create_schedule(
                    &conversation_id_s,
                    &schedule_id_s,
                    &title_s,
                    start_time_ms_v,
                    end_time_ms_v,
                    participant_user_ids_v,
                )
                .await?,
            )
        }
        "create_announcement" => {
            let conversation_id_s = conversation_id(&request)?;
            let title_s = json_string(&request, "title")?;
            let body_s = json_string(&request, "body")?;
            json(
                api.create_announcement(&conversation_id_s, &title_s, &body_s)
                    .await?,
            )
        }
        "create_custom" => {
            let conversation_id_s = conversation_id(&request)?;
            let type_s = json_string(&request, "type")?;
            json(api.create_custom(&conversation_id_s, &type_s).await?)
        }
        "create_placeholder" => {
            let conversation_id_s = conversation_id(&request)?;
            let reason_s = json_string(&request, "reason")?;
            json(
                api.create_placeholder(&conversation_id_s, &reason_s)
                    .await?,
            )
        }
        "create_with_content" => {
            let conversation_id_s = conversation_id(&request)?;
            let content = &request
                .get("content")
                .ok_or_else(|| crate::binding_invalid_parameter("missing content"))?;
            json(
                api.create_with_content(&conversation_id_s, built_content_from_value(content)?)
                    .await?,
            )
        }
        _ => Err(binding_operation_not_supported(operation)),
    }
}

pub async fn dispatch_message_build_json(
    api: &Arc<MessageBuildApi>,
    request_json: &str,
) -> Result<BindingResponse> {
    let operation = dispatch_operation_from_json(request_json)?;
    match operation.as_str() {
        "create_rich_doc" => json(
            api.create_rich_doc(
                from_json_str::<CreateRichDocJson>(request_json, "rich doc create")?.into(),
            )
            .await?,
        ),
        _ => {
            let request = dispatch_params_from_json(request_json)?;
            dispatch_message_build(api, request).await
        }
    }
}
