//! This code was AUTOGENERATED using the kinobi library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun kinobi to update it.
//!
//! [https://github.com/metaplex-foundation/kinobi]
//!

use crate::generated::types::Key;
#[cfg(feature = "anchor")]
use anchor_lang::prelude::{AnchorDeserialize, AnchorSerialize};
#[cfg(not(feature = "anchor"))]
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AgentIdentityV2 {
    pub key: Key,
    pub bump: u8,
    pub padding: [u8; 6],
    #[cfg_attr(
        feature = "serde",
        serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
    )]
    pub asset: Pubkey,
    pub agent_token: Option<Pubkey>,
    pub reserved: [u8; 32],
}

const AGENT_IDENTITY_V2_AGENT_TOKEN_FIXED_SIZE_OPTION_SENTINEL: [u8; 32] = [
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

impl BorshSerialize for AgentIdentityV2 {
    fn serialize<W: borsh::maybestd::io::Write>(
        &self,
        writer: &mut W,
    ) -> borsh::maybestd::io::Result<()> {
        BorshSerialize::serialize(&self.key, writer)?;
        BorshSerialize::serialize(&self.bump, writer)?;
        BorshSerialize::serialize(&self.padding, writer)?;
        BorshSerialize::serialize(&self.asset, writer)?;
        match &self.agent_token {
            Some(value) => BorshSerialize::serialize(value, writer)?,
            None => borsh::maybestd::io::Write::write_all(
                writer,
                &AGENT_IDENTITY_V2_AGENT_TOKEN_FIXED_SIZE_OPTION_SENTINEL,
            )?,
        };
        BorshSerialize::serialize(&self.reserved, writer)?;
        Ok(())
    }
}

impl BorshDeserialize for AgentIdentityV2 {
    fn deserialize_reader<R: borsh::maybestd::io::Read>(
        reader: &mut R,
    ) -> borsh::maybestd::io::Result<Self> {
        let key = BorshDeserialize::deserialize_reader(reader)?;
        let bump = BorshDeserialize::deserialize_reader(reader)?;
        let padding = BorshDeserialize::deserialize_reader(reader)?;
        let asset = BorshDeserialize::deserialize_reader(reader)?;
        let agent_token = {
            let mut buffer = [0u8; 32];
            borsh::maybestd::io::Read::read_exact(reader, &mut buffer)?;
            if buffer == AGENT_IDENTITY_V2_AGENT_TOKEN_FIXED_SIZE_OPTION_SENTINEL {
                None
            } else {
                let mut slice: &[u8] = &buffer;
                Some(BorshDeserialize::deserialize(&mut slice)?)
            }
        };
        let reserved = BorshDeserialize::deserialize_reader(reader)?;
        Ok(Self {
            key,
            bump,
            padding,
            asset,
            agent_token,
            reserved,
        })
    }
}

#[cfg(feature = "anchor")]
impl anchor_lang::prelude::AnchorSerialize for AgentIdentityV2 {
    fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
        BorshSerialize::serialize(self, writer)
    }
}

#[cfg(feature = "anchor")]
impl anchor_lang::prelude::AnchorDeserialize for AgentIdentityV2 {
    fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
        BorshDeserialize::deserialize_reader(reader)
    }
}

impl AgentIdentityV2 {
    pub const LEN: usize = 104;

    /// Prefix values used to generate a PDA for this account.
    ///
    /// Values are positional and appear in the following order:
    ///
    ///   0. `AgentIdentityV2::PREFIX`
    ///   1. asset (`Pubkey`)
    pub const PREFIX: &'static [u8] = "agent_identity".as_bytes();

    pub fn create_pda(
        asset: Pubkey,
        bump: u8,
    ) -> Result<solana_program::pubkey::Pubkey, solana_program::pubkey::PubkeyError> {
        solana_program::pubkey::Pubkey::create_program_address(
            &["agent_identity".as_bytes(), asset.as_ref(), &[bump]],
            &crate::MPL_AGENT_IDENTITY_ID,
        )
    }

    pub fn find_pda(asset: &Pubkey) -> (solana_program::pubkey::Pubkey, u8) {
        solana_program::pubkey::Pubkey::find_program_address(
            &["agent_identity".as_bytes(), asset.as_ref()],
            &crate::MPL_AGENT_IDENTITY_ID,
        )
    }

    #[inline(always)]
    pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
        let mut data = data;
        Self::deserialize(&mut data)
    }
}

impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for AgentIdentityV2 {
    type Error = std::io::Error;

    fn try_from(
        account_info: &solana_program::account_info::AccountInfo<'a>,
    ) -> Result<Self, Self::Error> {
        let mut data: &[u8] = &(*account_info.data).borrow();
        Self::deserialize(&mut data)
    }
}
