/*
 * Binance Margin Trading REST API
 *
 * OpenAPI Specification for the Binance Margin Trading 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::margin_trading::rest_api::models;

const HAS_TIME_UNIT: bool = false;

#[async_trait]
pub trait TransferApi: Send + Sync {
    async fn get_cross_margin_transfer_history(
        &self,
        params: GetCrossMarginTransferHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetCrossMarginTransferHistoryResponse>>;
    async fn query_max_transfer_out_amount(
        &self,
        params: QueryMaxTransferOutAmountParams,
    ) -> anyhow::Result<RestApiResponse<models::QueryMaxTransferOutAmountResponse>>;
}

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

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

/// Request parameters for the [`get_cross_margin_transfer_history`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_cross_margin_transfer_history`](#method.get_cross_margin_transfer_history).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetCrossMarginTransferHistoryParams {
    ///
    /// The `asset` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub asset: Option<String>,
    /// Transfer Type: `ROLL_IN`, `ROLL_OUT`
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub r#type: Option<String>,
    /// Only supports querying data from the past 90 days.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub start_time: Option<i64>,
    ///
    /// The `end_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub end_time: Option<i64>,
    /// Currently querying page. Start from 1. Default:1
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub current: Option<i64>,
    /// Default:10 Max:100
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub size: Option<i64>,
    /// isolated symbol
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub isolated_symbol: Option<String>,
    /// No more than 60000
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl GetCrossMarginTransferHistoryParams {
    /// Create a builder for [`get_cross_margin_transfer_history`].
    ///
    #[must_use]
    pub fn builder() -> GetCrossMarginTransferHistoryParamsBuilder {
        GetCrossMarginTransferHistoryParamsBuilder::default()
    }
}
/// Request parameters for the [`query_max_transfer_out_amount`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`query_max_transfer_out_amount`](#method.query_max_transfer_out_amount).
#[derive(Clone, Debug, Builder)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct QueryMaxTransferOutAmountParams {
    ///
    /// The `asset` parameter.
    ///
    /// This field is **required.
    #[builder(setter(into))]
    pub asset: String,
    /// isolated symbol
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub isolated_symbol: Option<String>,
    /// No more than 60000
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl QueryMaxTransferOutAmountParams {
    /// Create a builder for [`query_max_transfer_out_amount`].
    ///
    /// Required parameters:
    ///
    /// * `asset` — String
    ///
    #[must_use]
    pub fn builder(asset: String) -> QueryMaxTransferOutAmountParamsBuilder {
        QueryMaxTransferOutAmountParamsBuilder::default().asset(asset)
    }
}

#[async_trait]
impl TransferApi for TransferApiClient {
    async fn get_cross_margin_transfer_history(
        &self,
        params: GetCrossMarginTransferHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetCrossMarginTransferHistoryResponse>> {
        let GetCrossMarginTransferHistoryParams {
            asset,
            r#type,
            start_time,
            end_time,
            current,
            size,
            isolated_symbol,
            recv_window,
        } = params;

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

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

        if let Some(rw) = r#type {
            query_params.insert("type".to_string(), json!(rw));
        }

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

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

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

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

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

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

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

    async fn query_max_transfer_out_amount(
        &self,
        params: QueryMaxTransferOutAmountParams,
    ) -> anyhow::Result<RestApiResponse<models::QueryMaxTransferOutAmountResponse>> {
        let QueryMaxTransferOutAmountParams {
            asset,
            isolated_symbol,
            recv_window,
        } = params;

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

        query_params.insert("asset".to_string(), json!(asset));

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

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

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

#[cfg(all(test, feature = "margin_trading"))]
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 MockTransferApiClient {
        force_error: bool,
    }

    #[async_trait]
    impl TransferApi for MockTransferApiClient {
        async fn get_cross_margin_transfer_history(
            &self,
            _params: GetCrossMarginTransferHistoryParams,
        ) -> anyhow::Result<RestApiResponse<models::GetCrossMarginTransferHistoryResponse>>
        {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"amount":"0.10000000","asset":"BNB","status":"CONFIRMED","timestamp":1566898617,"txId":5240372201,"type":"ROLL_IN","transFrom":"SPOT","transTo":"ISOLATED_MARGIN"},{"amount":"5.00000000","asset":"USDT","status":"CONFIRMED","timestamp":1566888436,"txId":5239810406,"type":"ROLL_OUT","transFrom":"ISOLATED_MARGIN","transTo":"ISOLATED_MARGIN","fromSymbol":"BNBUSDT","toSymbol":"BTCUSDT"},{"amount":"1.00000000","asset":"EOS","status":"CONFIRMED","timestamp":1566888403,"txId":5239808703,"type":"ROLL_IN"}],"total":3}"#).unwrap();
            let dummy_response: models::GetCrossMarginTransferHistoryResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::GetCrossMarginTransferHistoryResponse");

            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_max_transfer_out_amount(
            &self,
            _params: QueryMaxTransferOutAmountParams,
        ) -> anyhow::Result<RestApiResponse<models::QueryMaxTransferOutAmountResponse>> {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"amount":"3.59498107"}"#).unwrap();
            let dummy_response: models::QueryMaxTransferOutAmountResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::QueryMaxTransferOutAmountResponse");

            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 get_cross_margin_transfer_history_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: false };

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

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"amount":"0.10000000","asset":"BNB","status":"CONFIRMED","timestamp":1566898617,"txId":5240372201,"type":"ROLL_IN","transFrom":"SPOT","transTo":"ISOLATED_MARGIN"},{"amount":"5.00000000","asset":"USDT","status":"CONFIRMED","timestamp":1566888436,"txId":5239810406,"type":"ROLL_OUT","transFrom":"ISOLATED_MARGIN","transTo":"ISOLATED_MARGIN","fromSymbol":"BNBUSDT","toSymbol":"BTCUSDT"},{"amount":"1.00000000","asset":"EOS","status":"CONFIRMED","timestamp":1566888403,"txId":5239808703,"type":"ROLL_IN"}],"total":3}"#).unwrap();
            let expected_response : models::GetCrossMarginTransferHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetCrossMarginTransferHistoryResponse");

            let resp = client.get_cross_margin_transfer_history(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 get_cross_margin_transfer_history_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: false };

            let params = GetCrossMarginTransferHistoryParams::builder().asset("asset_example".to_string()).r#type("r#type_example".to_string()).start_time(1623319461670).end_time(1641782889000).current(1).size(10).isolated_symbol("isolated_symbol_example".to_string()).recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"amount":"0.10000000","asset":"BNB","status":"CONFIRMED","timestamp":1566898617,"txId":5240372201,"type":"ROLL_IN","transFrom":"SPOT","transTo":"ISOLATED_MARGIN"},{"amount":"5.00000000","asset":"USDT","status":"CONFIRMED","timestamp":1566888436,"txId":5239810406,"type":"ROLL_OUT","transFrom":"ISOLATED_MARGIN","transTo":"ISOLATED_MARGIN","fromSymbol":"BNBUSDT","toSymbol":"BTCUSDT"},{"amount":"1.00000000","asset":"EOS","status":"CONFIRMED","timestamp":1566888403,"txId":5239808703,"type":"ROLL_IN"}],"total":3}"#).unwrap();
            let expected_response : models::GetCrossMarginTransferHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetCrossMarginTransferHistoryResponse");

            let resp = client.get_cross_margin_transfer_history(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 get_cross_margin_transfer_history_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: true };

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

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

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

            let params = QueryMaxTransferOutAmountParams::builder("asset_example".to_string())
                .build()
                .unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"amount":"3.59498107"}"#).unwrap();
            let expected_response: models::QueryMaxTransferOutAmountResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::QueryMaxTransferOutAmountResponse");

            let resp = client
                .query_max_transfer_out_amount(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_max_transfer_out_amount_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: false };

            let params = QueryMaxTransferOutAmountParams::builder("asset_example".to_string())
                .isolated_symbol("isolated_symbol_example".to_string())
                .recv_window(5000)
                .build()
                .unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"amount":"3.59498107"}"#).unwrap();
            let expected_response: models::QueryMaxTransferOutAmountResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::QueryMaxTransferOutAmountResponse");

            let resp = client
                .query_max_transfer_out_amount(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_max_transfer_out_amount_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: true };

            let params = QueryMaxTransferOutAmountParams::builder("asset_example".to_string())
                .build()
                .unwrap();

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