use ex3_node_error::{Error, OtherError};
use ex3_serde::cbor;
use crate::tx_type_dto::{
    AssetRegistration, UpdateAssetWithdrawalFeeTo, UpdateChainConfirmationTimes,
    UpdateGlobalWithdrawalFeeTo,
};
use crate::PayloadDecoder;
use crate::Result;
impl PayloadDecoder {
    pub fn decode_to_register_asset(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::AssetRegistration> {
        let register_asset = cbor::deserialize::<AssetRegistration>(payload).map_err(|e| {
            <OtherError as Into<Error>>::into(OtherError::new(format!(
                "Failed to deserialize payload to AssetRegistration: {}",
                e
            )))
        })?;
        Ok(register_asset.into())
    }
    pub fn decode_to_update_global_withdrawal_fee_to(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateGlobalWithdrawalFeeTo> {
        let update_global_withdrawal_fee_to =
            cbor::deserialize::<UpdateGlobalWithdrawalFeeTo>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to update_global_withdrawal_fee_to: {}",
                    e
                )))
            })?;
        Ok(update_global_withdrawal_fee_to.into())
    }
    pub fn decode_to_update_asset_withdrawal_fee_to(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateAssetWithdrawalFeeTo> {
        let update_asset_withdrawal_fee_to =
            cbor::deserialize::<UpdateAssetWithdrawalFeeTo>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateAssetWithdrawalFee: {}",
                    e
                )))
            })?;
        Ok(update_asset_withdrawal_fee_to.into())
    }
    pub fn decode_to_update_chain_confirmation_times(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateChainConfirmationTimes> {
        let update_chain_confirmation_times =
            cbor::deserialize::<UpdateChainConfirmationTimes>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateChainConfirmationTimes: {}",
                    e
                )))
            })?;
        Ok(update_chain_confirmation_times.into())
    }
}