use ex3_timestamp::TimeInNs;
use serde::{Deserialize, Serialize};

use crate::chain::Chain;
use crate::{AssetAmount, AssetId};

/// Withdrawal
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Withdrawal {
    /// The signature chain
    ///
    /// Note: it used for verification
    pub sign_chain: Chain,
    
    /// Specify the blockchain
    pub chain: Chain,

    /// Specify the network
    pub network: u8,

    /// Withdrawal to address
    pub to: String,

    /// Withdrawal asset id
    pub asset_id: AssetId,

    /// Withdrawal amount
    /// Note: This is the amount that the user will receive
    pub amount: AssetAmount,

    /// Withdrawal fee
    pub fee: AssetAmount,
}

/// Force withdrawal
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ForceWithdrawal {
    /// The signature chain
    ///
    /// Note: it used for verification
    pub sign_chain: Chain,

    /// Specify the blockchain
    pub chain: Chain,

    /// Specify the network
    pub network: u8,

    /// Withdrawal to address
    pub to: String,

    /// Withdrawal asset id
    pub asset_id: AssetId,

    /// Withdrawal amount
    /// Note: This is the amount that the user will receive
    pub amount: AssetAmount,

    /// Withdrawal fee
    pub fee: AssetAmount,

    /// Timestamp
    pub timestamp: TimeInNs,
}

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

    use super::*;

    #[test]
    fn test_serde() {
        let withdrawal = Withdrawal {
            chain: Chain::Ethereum,
            network: 1,
            to: "0x0000001312312321313213".to_string(),
            asset_id: AssetId::from(2000u32),
            amount: AssetAmount::from(10000u64),
            fee: AssetAmount::from(100u64),
        };

        // bincode
        let encoded = bincode::serialize(&withdrawal).unwrap();
        let decoded: Withdrawal = bincode::deserialize(&encoded).unwrap();
        assert_eq!(withdrawal, decoded);

        // cbor
        let encoded = cbor::serialize(&withdrawal).unwrap();
        let decoded: Withdrawal = cbor::deserialize(&encoded).unwrap();
        assert_eq!(withdrawal, decoded);
    }
}
