/*
 * Ethereal Exchange API
 *
 * Ethereal HTTP API for real-time trading, order management, and market data access.
 *
 * The version of the OpenAPI document: 0.1.0
 *
 * Generated by: https://openapi-generator.tech
 */

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

/// struct for passing parameters to the method [`subaccount_controller_get_by_subaccount_id`]
#[derive(Clone, Debug, Default)]
pub struct SubaccountControllerGetBySubaccountIdParams {
    pub id: String,
}

/// struct for passing parameters to the method [`subaccount_controller_list_by_account`]
#[derive(Clone, Debug, Default)]
pub struct SubaccountControllerListByAccountParams {
    /// Address of the sender
    pub sender: String,
    /// Direction to paginate through objects
    pub order: Option<String>,
    /// Limit the number of objects to return
    pub limit: Option<f64>,
    /// Pointer to the current object in pagination dataset
    pub cursor: Option<String>,
    /// Bytes32 encoded subaccount name (0x prefix, zero padded)
    pub name: Option<String>,
    /// Order by field
    pub order_by: Option<String>,
}

/// struct for passing parameters to the method [`subaccount_controller_list_subaccount_balances`]
#[derive(Clone, Debug, Default)]
pub struct SubaccountControllerListSubaccountBalancesParams {
    /// Id representing the registered subaccount
    pub subaccount_id: String,
    /// Direction to paginate through objects
    pub order: Option<String>,
    /// Limit the number of objects to return
    pub limit: Option<f64>,
    /// Pointer to the current object in pagination dataset
    pub cursor: Option<String>,
    /// Order by field
    pub order_by: Option<String>,
}

/// struct for typed errors of method [`subaccount_controller_get_by_subaccount_id`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SubaccountControllerGetBySubaccountIdError {
    Status400(models::BadRequestDto),
    Status401(models::UnauthorizedDto),
    Status403(models::ForbiddenDto),
    Status404(models::NotFoundDto),
    Status422(models::UnprocessableEntityDto),
    Status429(models::TooManyRequestsDto),
    Status500(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`subaccount_controller_list_by_account`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SubaccountControllerListByAccountError {
    Status400(models::BadRequestDto),
    Status401(models::UnauthorizedDto),
    Status403(models::ForbiddenDto),
    Status404(models::NotFoundDto),
    Status422(models::UnprocessableEntityDto),
    Status429(models::TooManyRequestsDto),
    Status500(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`subaccount_controller_list_subaccount_balances`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SubaccountControllerListSubaccountBalancesError {
    Status400(models::BadRequestDto),
    Status401(models::UnauthorizedDto),
    Status403(models::ForbiddenDto),
    Status404(models::NotFoundDto),
    Status422(models::UnprocessableEntityDto),
    Status429(models::TooManyRequestsDto),
    Status500(),
    UnknownValue(serde_json::Value),
}

pub async fn subaccount_controller_get_by_subaccount_id(
    configuration: &configuration::Configuration,
    params: SubaccountControllerGetBySubaccountIdParams,
) -> Result<models::SubaccountDto, Error<SubaccountControllerGetBySubaccountIdError>> {
    let uri_str = format!(
        "{}/v1/subaccount/{id}",
        configuration.base_path,
        id = crate::apis::urlencode(params.id)
    );
    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 `models::SubaccountDto`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::SubaccountDto`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<SubaccountControllerGetBySubaccountIdError> =
            serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

pub async fn subaccount_controller_list_by_account(
    configuration: &configuration::Configuration,
    params: SubaccountControllerListByAccountParams,
) -> Result<models::PageOfSubaccountDtos, Error<SubaccountControllerListByAccountError>> {
    let uri_str = format!("{}/v1/subaccount", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = params.order {
        req_builder = req_builder.query(&[("order", &param_value.to_string())]);
    }
    if let Some(ref param_value) = params.limit {
        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
    }
    if let Some(ref param_value) = params.cursor {
        req_builder = req_builder.query(&[("cursor", &param_value.to_string())]);
    }
    if let Some(ref param_value) = params.name {
        req_builder = req_builder.query(&[("name", &param_value.to_string())]);
    }
    req_builder = req_builder.query(&[("sender", &params.sender.to_string())]);
    if let Some(ref param_value) = params.order_by {
        req_builder = req_builder.query(&[("orderBy", &param_value.to_string())]);
    }
    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 `models::PageOfSubaccountDtos`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::PageOfSubaccountDtos`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<SubaccountControllerListByAccountError> =
            serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

pub async fn subaccount_controller_list_subaccount_balances(
    configuration: &configuration::Configuration,
    params: SubaccountControllerListSubaccountBalancesParams,
) -> Result<
    models::PageOfSubaccountBalanceDtos,
    Error<SubaccountControllerListSubaccountBalancesError>,
> {
    let uri_str = format!("{}/v1/subaccount/balance", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = params.order {
        req_builder = req_builder.query(&[("order", &param_value.to_string())]);
    }
    if let Some(ref param_value) = params.limit {
        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
    }
    if let Some(ref param_value) = params.cursor {
        req_builder = req_builder.query(&[("cursor", &param_value.to_string())]);
    }
    req_builder = req_builder.query(&[("subaccountId", &params.subaccount_id.to_string())]);
    if let Some(ref param_value) = params.order_by {
        req_builder = req_builder.query(&[("orderBy", &param_value.to_string())]);
    }
    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 `models::PageOfSubaccountBalanceDtos`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::PageOfSubaccountBalanceDtos`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<SubaccountControllerListSubaccountBalancesError> =
            serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}
