/*
 * Binance Convert REST API
 *
 * OpenAPI Specification for the Binance Convert REST API
 *
 * The version of the OpenAPI document: 1.0.0
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

#![allow(unused_imports)]
use async_trait::async_trait;
use derive_builder::Builder;
use reqwest;
use rust_decimal::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::BTreeMap;

use crate::common::{
    config::ConfigurationRestApi,
    models::{ParamBuildError, RestApiResponse},
    utils::send_request,
};
use crate::convert::rest_api::models;

const HAS_TIME_UNIT: bool = false;

#[async_trait]
pub trait MarketDataApi: Send + Sync {
    async fn list_all_convert_pairs(
        &self,
        params: ListAllConvertPairsParams,
    ) -> anyhow::Result<RestApiResponse<Vec<models::ListAllConvertPairsResponseInner>>>;
    async fn query_order_quantity_precision_per_asset(
        &self,
        params: QueryOrderQuantityPrecisionPerAssetParams,
    ) -> anyhow::Result<
        RestApiResponse<Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner>>,
    >;
}

#[derive(Debug, Clone)]
pub struct MarketDataApiClient {
    configuration: ConfigurationRestApi,
}

impl MarketDataApiClient {
    pub fn new(configuration: ConfigurationRestApi) -> Self {
        Self { configuration }
    }
}

/// Request parameters for the [`list_all_convert_pairs`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`list_all_convert_pairs`](#method.list_all_convert_pairs).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct ListAllConvertPairsParams {
    /// User spends coin
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub from_asset: Option<String>,
    /// User receives coin
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub to_asset: Option<String>,
}

impl ListAllConvertPairsParams {
    /// Create a builder for [`list_all_convert_pairs`].
    ///
    #[must_use]
    pub fn builder() -> ListAllConvertPairsParamsBuilder {
        ListAllConvertPairsParamsBuilder::default()
    }
}
/// Request parameters for the [`query_order_quantity_precision_per_asset`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`query_order_quantity_precision_per_asset`](#method.query_order_quantity_precision_per_asset).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct QueryOrderQuantityPrecisionPerAssetParams {
    /// The value cannot be greater than 60000
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl QueryOrderQuantityPrecisionPerAssetParams {
    /// Create a builder for [`query_order_quantity_precision_per_asset`].
    ///
    #[must_use]
    pub fn builder() -> QueryOrderQuantityPrecisionPerAssetParamsBuilder {
        QueryOrderQuantityPrecisionPerAssetParamsBuilder::default()
    }
}

#[async_trait]
impl MarketDataApi for MarketDataApiClient {
    async fn list_all_convert_pairs(
        &self,
        params: ListAllConvertPairsParams,
    ) -> anyhow::Result<RestApiResponse<Vec<models::ListAllConvertPairsResponseInner>>> {
        let ListAllConvertPairsParams {
            from_asset,
            to_asset,
        } = params;

        let mut query_params = BTreeMap::new();
        let body_params = BTreeMap::new();

        if let Some(rw) = from_asset {
            query_params.insert("fromAsset".to_string(), json!(rw));
        }

        if let Some(rw) = to_asset {
            query_params.insert("toAsset".to_string(), json!(rw));
        }

        send_request::<Vec<models::ListAllConvertPairsResponseInner>>(
            &self.configuration,
            "/sapi/v1/convert/exchangeInfo",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            false,
        )
        .await
    }

    async fn query_order_quantity_precision_per_asset(
        &self,
        params: QueryOrderQuantityPrecisionPerAssetParams,
    ) -> anyhow::Result<
        RestApiResponse<Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner>>,
    > {
        let QueryOrderQuantityPrecisionPerAssetParams { recv_window } = params;

        let mut query_params = BTreeMap::new();
        let body_params = BTreeMap::new();

        if let Some(rw) = recv_window {
            query_params.insert("recvWindow".to_string(), json!(rw));
        }

        send_request::<Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner>>(
            &self.configuration,
            "/sapi/v1/convert/assetInfo",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }
}

#[cfg(all(test, feature = "convert"))]
mod tests {
    use super::*;
    use crate::TOKIO_SHARED_RT;
    use crate::{errors::ConnectorError, models::DataFuture, models::RestApiRateLimit};
    use async_trait::async_trait;
    use std::collections::HashMap;

    struct DummyRestApiResponse<T> {
        inner: Box<dyn FnOnce() -> DataFuture<Result<T, ConnectorError>> + Send + Sync>,
        status: u16,
        headers: HashMap<String, String>,
        rate_limits: Option<Vec<RestApiRateLimit>>,
    }

    impl<T> From<DummyRestApiResponse<T>> for RestApiResponse<T> {
        fn from(dummy: DummyRestApiResponse<T>) -> Self {
            Self {
                data_fn: dummy.inner,
                status: dummy.status,
                headers: dummy.headers,
                rate_limits: dummy.rate_limits,
            }
        }
    }

    struct MockMarketDataApiClient {
        force_error: bool,
    }

    #[async_trait]
    impl MarketDataApi for MockMarketDataApiClient {
        async fn list_all_convert_pairs(
            &self,
            _params: ListAllConvertPairsParams,
        ) -> anyhow::Result<RestApiResponse<Vec<models::ListAllConvertPairsResponseInner>>>
        {
            if self.force_error {
                return Err(
                    ConnectorError::ConnectorClientError("ResponseError".to_string()).into(),
                );
            }

            let resp_json: Value = serde_json::from_str(r#"[{"fromAsset":"BTC","toAsset":"USDT","fromAssetMinAmount":"0.0004","fromAssetMaxAmount":"50","toAssetMinAmount":"20","toAssetMaxAmount":"2500000"}]"#).unwrap();
            let dummy_response: Vec<models::ListAllConvertPairsResponseInner> =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into Vec<models::ListAllConvertPairsResponseInner>");

            let dummy = DummyRestApiResponse {
                inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
                status: 200,
                headers: HashMap::new(),
                rate_limits: None,
            };

            Ok(dummy.into())
        }

        async fn query_order_quantity_precision_per_asset(
            &self,
            _params: QueryOrderQuantityPrecisionPerAssetParams,
        ) -> anyhow::Result<
            RestApiResponse<Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner>>,
        > {
            if self.force_error {
                return Err(
                    ConnectorError::ConnectorClientError("ResponseError".to_string()).into(),
                );
            }

            let resp_json: Value = serde_json::from_str(
                r#"[{"asset":"BTC","fraction":8},{"asset":"SHIB","fraction":2}]"#,
            )
            .unwrap();
            let dummy_response : Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner> = serde_json::from_value(resp_json.clone()).expect("should parse into Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner>");

            let dummy = DummyRestApiResponse {
                inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
                status: 200,
                headers: HashMap::new(),
                rate_limits: None,
            };

            Ok(dummy.into())
        }
    }

    #[test]
    fn list_all_convert_pairs_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockMarketDataApiClient { force_error: false };

            let params = ListAllConvertPairsParams::builder().build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"[{"fromAsset":"BTC","toAsset":"USDT","fromAssetMinAmount":"0.0004","fromAssetMaxAmount":"50","toAssetMinAmount":"20","toAssetMaxAmount":"2500000"}]"#).unwrap();
            let expected_response : Vec<models::ListAllConvertPairsResponseInner> = serde_json::from_value(resp_json.clone()).expect("should parse into Vec<models::ListAllConvertPairsResponseInner>");

            let resp = client.list_all_convert_pairs(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn list_all_convert_pairs_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockMarketDataApiClient { force_error: false };

            let params = ListAllConvertPairsParams::builder().from_asset("from_asset_example".to_string()).to_asset("to_asset_example".to_string()).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"[{"fromAsset":"BTC","toAsset":"USDT","fromAssetMinAmount":"0.0004","fromAssetMaxAmount":"50","toAssetMinAmount":"20","toAssetMaxAmount":"2500000"}]"#).unwrap();
            let expected_response : Vec<models::ListAllConvertPairsResponseInner> = serde_json::from_value(resp_json.clone()).expect("should parse into Vec<models::ListAllConvertPairsResponseInner>");

            let resp = client.list_all_convert_pairs(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn list_all_convert_pairs_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockMarketDataApiClient { force_error: true };

            let params = ListAllConvertPairsParams::builder().build().unwrap();

            match client.list_all_convert_pairs(params).await {
                Ok(_) => panic!("Expected an error"),
                Err(err) => {
                    assert_eq!(err.to_string(), "Connector client error: ResponseError");
                }
            }
        });
    }

    #[test]
    fn query_order_quantity_precision_per_asset_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockMarketDataApiClient { force_error: false };

            let params = QueryOrderQuantityPrecisionPerAssetParams::builder().build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"[{"asset":"BTC","fraction":8},{"asset":"SHIB","fraction":2}]"#).unwrap();
            let expected_response : Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner> = serde_json::from_value(resp_json.clone()).expect("should parse into Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner>");

            let resp = client.query_order_quantity_precision_per_asset(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn query_order_quantity_precision_per_asset_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockMarketDataApiClient { force_error: false };

            let params = QueryOrderQuantityPrecisionPerAssetParams::builder().recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"[{"asset":"BTC","fraction":8},{"asset":"SHIB","fraction":2}]"#).unwrap();
            let expected_response : Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner> = serde_json::from_value(resp_json.clone()).expect("should parse into Vec<models::QueryOrderQuantityPrecisionPerAssetResponseInner>");

            let resp = client.query_order_quantity_precision_per_asset(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn query_order_quantity_precision_per_asset_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockMarketDataApiClient { force_error: true };

            let params = QueryOrderQuantityPrecisionPerAssetParams::builder()
                .build()
                .unwrap();

            match client
                .query_order_quantity_precision_per_asset(params)
                .await
            {
                Ok(_) => panic!("Expected an error"),
                Err(err) => {
                    assert_eq!(err.to_string(), "Connector client error: ResponseError");
                }
            }
        });
    }
}
