use std::fmt;

use ex3_payload_derive::Ex3Payload;
use serde::de::{Deserializer, SeqAccess, Visitor};
use serde::ser::{SerializeSeq, Serializer};
use serde::{Deserialize, Serialize};

use crate::{AssetAmount, AssetId, WalletRegisterId};

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Transfer {
    /// Transfer to wallet id
    pub to: WalletRegisterId,

    /// Transfer amount
    pub amount: AssetAmount,
}

/// Batch transfer
#[derive(Clone, Debug, Eq, PartialEq, Ex3Payload)]
pub struct BatchTransfer {
    pub asset_id: AssetId,
    #[index(0)]
    pub items: Vec<Transfer>,
}

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

    use super::*;

    #[test]
    fn test_bincode_serde() {
        let batch_transfer = BatchTransfer {
            asset_id: 1u8.into(),
            items: vec![
                Transfer {
                    to: 1u8.into(),
                    amount: 100u8.into(),
                },
                Transfer {
                    to: 2u8.into(),
                    amount: 200u8.into(),
                },
            ],
        };

        let encoded = bincode::serialize(&batch_transfer).unwrap();
        let decoded: BatchTransfer = bincode::deserialize(&encoded).unwrap();
        assert_eq!(batch_transfer, decoded);
    }

    #[test]
    fn test_cbor_serde() {
        let batch_transfer = BatchTransfer {
            asset_id: 1u8.into(),
            items: vec![
                Transfer {
                    to: 1u8.into(),
                    amount: 100u8.into(),
                },
                Transfer {
                    to: 2u8.into(),
                    amount: 200u8.into(),
                },
            ],
        };

        let encoded = bincode::serialize(&batch_transfer).unwrap();
        let decoded: BatchTransfer = bincode::deserialize(&encoded).unwrap();
        assert_eq!(batch_transfer, decoded);
    }
}
