/*
 * Text Video Agent Api
 *
 * 文本生成视频API服务
 *
 * The version of the OpenAPI document: 1.0.6
 * 
 * Generated by: https://openapi-generator.tech
 */


use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};


/// struct for typed errors of method [`get_voices_hl_api302_hl_router_sync_get_voices_get`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetVoicesHlApi302HlRouterSyncGetVoicesGetError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`hl_tts_api302_hl_router_sync_generate_speech_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum HlTtsApi302HlRouterSyncGenerateSpeechPostError {
    Status422(models::HttpValidationError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`upload_material_file_api302_hl_router_sync_file_upload_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UploadMaterialFileApi302HlRouterSyncFileUploadPostError {
    Status422(models::HttpValidationError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`voice_clone_api302_hl_router_sync_voice_clone_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum VoiceCloneApi302HlRouterSyncVoiceClonePostError {
    Status422(models::HttpValidationError),
    UnknownValue(serde_json::Value),
}


pub async fn get_voices_hl_api302_hl_router_sync_get_voices_get(configuration: &configuration::Configuration, ) -> Result<serde_json::Value, Error<GetVoicesHlApi302HlRouterSyncGetVoicesGetError>> {

    let uri_str = format!("{}/api/302/hl_router/sync/get/voices", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetVoicesHlApi302HlRouterSyncGetVoicesGetError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

pub async fn hl_tts_api302_hl_router_sync_generate_speech_post(configuration: &configuration::Configuration, text: &str, voice_id: &str, speed: Option<f64>, vol: Option<f64>, emotion: Option<&str>) -> Result<serde_json::Value, Error<HlTtsApi302HlRouterSyncGenerateSpeechPostError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_text = text;
    let p_voice_id = voice_id;
    let p_speed = speed;
    let p_vol = vol;
    let p_emotion = emotion;

    let uri_str = format!("{}/api/302/hl_router/sync/generate/speech", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    let mut multipart_form_params = std::collections::HashMap::new();
    multipart_form_params.insert("text", p_text.to_string());
    multipart_form_params.insert("voice_id", p_voice_id.to_string());
    if let Some(param_value) = p_speed {
        multipart_form_params.insert("speed", param_value.to_string());
    }
    if let Some(param_value) = p_vol {
        multipart_form_params.insert("vol", param_value.to_string());
    }
    if let Some(param_value) = p_emotion {
        multipart_form_params.insert("emotion", param_value.to_string());
    }
    req_builder = req_builder.form(&multipart_form_params);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<HlTtsApi302HlRouterSyncGenerateSpeechPostError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

pub async fn upload_material_file_api302_hl_router_sync_file_upload_post(configuration: &configuration::Configuration, audio_file: std::path::PathBuf, purpose: Option<&str>) -> Result<serde_json::Value, Error<UploadMaterialFileApi302HlRouterSyncFileUploadPostError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_audio_file = audio_file;
    let p_purpose = purpose;

    let uri_str = format!("{}/api/302/hl_router/sync/file/upload", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    let mut multipart_form = reqwest::multipart::Form::new();
    // TODO: support file upload for 'audio_file' parameter
    if let Some(param_value) = p_purpose {
        multipart_form = multipart_form.text("purpose", param_value.to_string());
    }
    req_builder = req_builder.multipart(multipart_form);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<UploadMaterialFileApi302HlRouterSyncFileUploadPostError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

pub async fn voice_clone_api302_hl_router_sync_voice_clone_post(configuration: &configuration::Configuration, text: &str, model: Option<&str>, need_noise_reduction: Option<bool>, voice_id: Option<&str>, prefix: Option<&str>, audio_file: Option<std::path::PathBuf>) -> Result<serde_json::Value, Error<VoiceCloneApi302HlRouterSyncVoiceClonePostError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_text = text;
    let p_model = model;
    let p_need_noise_reduction = need_noise_reduction;
    let p_voice_id = voice_id;
    let p_prefix = prefix;
    let p_audio_file = audio_file;

    let uri_str = format!("{}/api/302/hl_router/sync/voice/clone", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    let mut multipart_form = reqwest::multipart::Form::new();
    multipart_form = multipart_form.text("text", p_text.to_string());
    if let Some(param_value) = p_model {
        multipart_form = multipart_form.text("model", param_value.to_string());
    }
    if let Some(param_value) = p_need_noise_reduction {
        multipart_form = multipart_form.text("need_noise_reduction", param_value.to_string());
    }
    if let Some(param_value) = p_voice_id {
        multipart_form = multipart_form.text("voice_id", param_value.to_string());
    }
    if let Some(param_value) = p_prefix {
        multipart_form = multipart_form.text("prefix", param_value.to_string());
    }
    // TODO: support file upload for 'audio_file' parameter
    req_builder = req_builder.multipart(multipart_form);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<VoiceCloneApi302HlRouterSyncVoiceClonePostError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

