use serde::{Deserialize, Serialize};

use crate::spot_market::{SpotMarketTradingFee, SpotMarketTradingSettings};
use crate::{AssetAmount, AssetId, MarketId, WalletRegisterId};

/// Register market
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct RegisterSpotMarket {
    /// Base asset id
    pub base_asset_id: AssetId,

    /// Base asset quantity
    pub base_asset_quantity: AssetAmount,

    /// Quote asset id
    pub quote_asset_id: AssetId,

    /// Quote asset quantity
    pub quote_asset_quantity: AssetAmount,

    /// Market trading settings
    pub trading_settings: SpotMarketTradingSettings,

    /// Trading fee settings
    /// Note: if the trading fee is none, the market trading fee will use the global trading fee
    pub fee: Option<SpotMarketTradingFee>,

    /// Fee to
    /// Orderbook fee to & swap fee to use this value
    /// Note: fee to will receive the market trading fee
    /// Note: if the fee to is none, the market trading fee will use the global fee to
    pub fee_to: Option<WalletRegisterId>,
}

/// Update spot market trading settings
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct UpdateSpotMarketTradingSettings {
    /// Market id
    pub market_id: MarketId,

    /// Market trading settings
    pub trading_settings: SpotMarketTradingSettings,
}

/// Update spot market global fee to
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct UpdateSpotMarketGlobalFeeTo {
    /// Fee to
    pub fee_to: WalletRegisterId,
}

/// Update spot market fee to
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct UpdateSpotMarketFeeTo {
    /// Market id
    pub market_id: MarketId,

    /// Fee to
    /// Note: if the fee to is none, the market trading fee will use the global fee to
    pub fee_to: Option<WalletRegisterId>,
}

/// Update spot market global fee
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct UpdateSpotMarketGlobalFee {
    /// Fee
    pub fee: SpotMarketTradingFee,
}

/// Update market fee
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct UpdateMarketFee {
    /// Market id
    pub market_id: MarketId,

    /// Fee
    /// Note: if the fee is none, the market trading fee will use the global trading fee
    pub fee: Option<SpotMarketTradingFee>,
}

#[cfg(test)]
mod tests {
    use ex3_serde::{bincode, cbor};
    use num_bigint::BigUint;

    use crate::spot_market::SpotOrderPriceLimitation;

    use super::*;

    #[test]
    fn test_register_market_serde() {
        let register_market = RegisterSpotMarket {
            base_asset_id: AssetId::from(1u32),
            base_asset_quantity: AssetAmount::from(100u32),
            quote_asset_id: AssetId::from(2u32),
            quote_asset_quantity: AssetAmount::from(200u32),
            trading_settings: SpotMarketTradingSettings {
                price_max_decimal: 8,
                volume_max_decimal: 8,
                min_order_amount: AssetAmount::from(300u32),
                min_order_volume: AssetAmount::from(400u32),
                price_limitation: SpotOrderPriceLimitation {
                    bid_price_rate_cap: BigUint::from(100u32),
                    ask_price_rate_floor: BigUint::from(100u32),
                    rate_decimal: 8,
                },
            },
            fee: Some(SpotMarketTradingFee {
                taker_fee: BigUint::from(300u32),
                maker_fee: BigUint::from(500u32),
                amm_v2_fee_taking_ratio: BigUint::from(500u32),
                fee_decimal: 8,
            }),
            fee_to: Some(WalletRegisterId::from(1u32)),
        };

        // bincode
        let bytes = bincode::serialize(&register_market).unwrap();
        let register_market_deserialized: RegisterSpotMarket =
            bincode::deserialize(&bytes).unwrap();
        assert_eq!(register_market, register_market_deserialized);

        // cbor
        let bytes = cbor::serialize(&register_market).unwrap();
        let register_market_deserialized: RegisterSpotMarket = cbor::deserialize(&bytes).unwrap();
        assert_eq!(register_market, register_market_deserialized);
    }

    #[test]
    fn test_update_market_settings_serde() {
        let update_market_settings = UpdateSpotMarketTradingSettings {
            market_id: MarketId::from(1u32),
            trading_settings: SpotMarketTradingSettings {
                price_max_decimal: 8,
                volume_max_decimal: 8,
                min_order_amount: AssetAmount::from(100u32),
                min_order_volume: AssetAmount::from(100u32),
                price_limitation: SpotOrderPriceLimitation {
                    bid_price_rate_cap: BigUint::from(100u32),
                    ask_price_rate_floor: BigUint::from(100u32),
                    rate_decimal: 8,
                },
            },
        };

        // bincode
        let bytes = bincode::serialize(&update_market_settings).unwrap();
        let update_market_settings_deserialized: UpdateSpotMarketTradingSettings =
            bincode::deserialize(&bytes).unwrap();
        assert_eq!(update_market_settings, update_market_settings_deserialized);

        // cbor
        let bytes = cbor::serialize(&update_market_settings).unwrap();
        let update_market_settings_deserialized: UpdateSpotMarketTradingSettings =
            cbor::deserialize(&bytes).unwrap();
        assert_eq!(update_market_settings, update_market_settings_deserialized);
    }

    #[test]
    fn test_update_global_market_fee_to_serde() {
        let update_global_market_fee_to = UpdateSpotMarketGlobalFeeTo {
            fee_to: WalletRegisterId::from(1u32),
        };

        // bincode
        let bytes = bincode::serialize(&update_global_market_fee_to).unwrap();
        let update_global_market_fee_to_deserialized: UpdateSpotMarketGlobalFeeTo =
            bincode::deserialize(&bytes).unwrap();
        assert_eq!(
            update_global_market_fee_to,
            update_global_market_fee_to_deserialized
        );

        // cbor
        let bytes = cbor::serialize(&update_global_market_fee_to).unwrap();
        let update_global_market_fee_to_deserialized: UpdateSpotMarketGlobalFeeTo =
            cbor::deserialize(&bytes).unwrap();
        assert_eq!(
            update_global_market_fee_to,
            update_global_market_fee_to_deserialized
        );
    }

    #[test]
    fn test_update_market_fee_to_serde() {
        let update_market_fee_to_1 = UpdateSpotMarketFeeTo {
            market_id: MarketId::from(1u32),
            fee_to: Some(WalletRegisterId::from(1u32)),
        };

        // bincode
        let bytes = bincode::serialize(&update_market_fee_to_1).unwrap();
        let update_market_fee_to_deserialized: UpdateSpotMarketFeeTo =
            bincode::deserialize(&bytes).unwrap();
        assert_eq!(update_market_fee_to_1, update_market_fee_to_deserialized);

        // cbor
        let bytes = cbor::serialize(&update_market_fee_to_1).unwrap();
        let update_market_fee_to_deserialized: UpdateSpotMarketFeeTo =
            cbor::deserialize(&bytes).unwrap();
        assert_eq!(update_market_fee_to_1, update_market_fee_to_deserialized);

        let update_market_fee_to_2 = UpdateSpotMarketFeeTo {
            market_id: MarketId::from(1u32),
            fee_to: Some(WalletRegisterId::from(1u32)),
        };

        // bincode
        let bytes = bincode::serialize(&update_market_fee_to_2).unwrap();
        let update_market_fee_to_deserialized: UpdateSpotMarketFeeTo =
            bincode::deserialize(&bytes).unwrap();
        assert_eq!(update_market_fee_to_2, update_market_fee_to_deserialized);

        // cbor
        let bytes = cbor::serialize(&update_market_fee_to_2).unwrap();
        let update_market_fee_to_deserialized: UpdateSpotMarketFeeTo =
            cbor::deserialize(&bytes).unwrap();
        assert_eq!(update_market_fee_to_2, update_market_fee_to_deserialized);
    }

    #[test]
    fn test_update_global_market_fee_serde() {
        let update_global_market_fee = UpdateSpotMarketGlobalFee {
            fee: SpotMarketTradingFee {
                taker_fee: BigUint::from(100u32),
                maker_fee: BigUint::from(200u32),
                fee_decimal: 8,
                amm_v2_fee_taking_ratio: BigUint::from(300u32),
            },
        };

        // bincode
        let bytes = bincode::serialize(&update_global_market_fee).unwrap();
        let update_global_market_fee_deserialized: UpdateSpotMarketGlobalFee =
            bincode::deserialize(&bytes).unwrap();
        assert_eq!(
            update_global_market_fee,
            update_global_market_fee_deserialized
        );

        // cbor
        let bytes = cbor::serialize(&update_global_market_fee).unwrap();
        let update_global_market_fee_deserialized: UpdateSpotMarketGlobalFee =
            cbor::deserialize(&bytes).unwrap();
        assert_eq!(
            update_global_market_fee,
            update_global_market_fee_deserialized
        );
    }

    #[test]
    fn test_update_market_fee_serde() {
        let update_market_fee_1 = UpdateMarketFee {
            market_id: MarketId::from(1u32),
            fee: Some(SpotMarketTradingFee {
                taker_fee: BigUint::from(100u32),
                maker_fee: BigUint::from(200u32),
                fee_decimal: 8,
                amm_v2_fee_taking_ratio: BigUint::from(200u32),
            }),
        };

        // bincode
        let bytes = bincode::serialize(&update_market_fee_1).unwrap();
        let update_market_fee_deserialized: UpdateMarketFee = bincode::deserialize(&bytes).unwrap();
        assert_eq!(update_market_fee_1, update_market_fee_deserialized);

        // cbor
        let bytes = cbor::serialize(&update_market_fee_1).unwrap();
        let update_market_fee_deserialized: UpdateMarketFee = cbor::deserialize(&bytes).unwrap();
        assert_eq!(update_market_fee_1, update_market_fee_deserialized);

        let update_market_fee_2 = UpdateMarketFee {
            market_id: MarketId::from(1u32),
            fee: None,
        };

        // bincode
        let bytes = bincode::serialize(&update_market_fee_2).unwrap();
        let update_market_fee_deserialized: UpdateMarketFee = bincode::deserialize(&bytes).unwrap();
        assert_eq!(update_market_fee_2, update_market_fee_deserialized);

        // cbor
        let bytes = cbor::serialize(&update_market_fee_2).unwrap();
        let update_market_fee_deserialized: UpdateMarketFee = cbor::deserialize(&bytes).unwrap();
        assert_eq!(update_market_fee_2, update_market_fee_deserialized);
    }
}
