use crate::SWITCHBOARD_ATTESTATION_PROGRAM_ID;
use bytemuck::{Pod, Zeroable};
use libsecp256k1;
use std::cell::Ref;
use solana_program::pubkey::Pubkey;
use anchor_lang::zero_copy;
use anchor_lang::prelude::ErrorCode;
use solana_program::sysvar::clock::Clock;
use anchor_lang::prelude::AccountInfo;
use anchor_lang::ZeroCopy;
use anchor_lang::Owner;
use anchor_lang::Discriminator;
use crate::cfg_client;
use crate::OnDemandError;
use crate::Quote;

#[repr(u8)]
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
pub enum VerificationStatus {
    #[default]
    None = 0,
    VerificationPending = 1 << 0,
    VerificationFailure = 1 << 1,
    VerificationSuccess = 1 << 2,
    VerificationOverride = 1 << 3,
}
impl From<VerificationStatus> for u8 {
    fn from(value: VerificationStatus) -> Self {
        match value {
            VerificationStatus::VerificationPending => 1 << 0,
            VerificationStatus::VerificationFailure => 1 << 1,
            VerificationStatus::VerificationSuccess => 1 << 2,
            VerificationStatus::VerificationOverride => 1 << 3,
            _ => 0,
        }
    }
}
impl From<u8> for VerificationStatus {
    fn from(value: u8) -> Self {
        match value {
            1 => VerificationStatus::VerificationPending,
            2 => VerificationStatus::VerificationFailure,
            4 => VerificationStatus::VerificationSuccess,
            8 => VerificationStatus::VerificationOverride,
            _ => VerificationStatus::default(),
        }
    }
}

#[allow(dead_code)]
#[zero_copy(unsafe)]
#[derive(Debug)]
pub struct Ewma {
    average: i128,
    last_update: u64,
    decay_rate: i128,
    precision: u32,
}

#[zero_copy(unsafe)]
#[repr(packed)]
#[derive(Debug)]
pub struct OracleAccountData {
    /// Represents the state of the quote verifiers enclave.
    pub enclave: Quote,

    // Accounts Config
    /// The authority of the EnclaveAccount which is permitted to make account changes.
    pub authority: Pubkey,
    /// Queue used for attestation to verify a MRENCLAVE measurement.
    pub queue: Pubkey,

    // Metadata Config
    /// The unix timestamp when the quote was created.
    pub created_at: i64,

    // Queue Config
    /// Whether the quote is located on the AttestationQueues buffer.
    pub is_on_queue: u8,
    /// The last time the quote heartbeated on-chain.
    pub last_heartbeat: i64,

    // Token Config
    /// The SwitchboardWallet account containing the reward escrow for verifying quotes on-chain.
    /// This is the root escrow address, NOT the ATA for a specific mint.
    pub reward_escrow: Pubkey,
    /// The SwitchboardWallet account containing the queues required min_stake.
    /// Needs to be separate from the reward_escrow. Allows easier 3rd party management of stake from rewards.
    /// This is the root escrow address, NOT the ATA for a specific mint.
    pub stake_escrow: Pubkey,

    /// Tracks the number of successful price attestations.
    pub success_ewma: Ewma,
    /// Tracks the number of failed price attestations (user reported).
    pub failure_ewma: Ewma,

    /// URI location of the verifier's gateway.
    pub gateway_uri: [u8; 64],
    pub permissions: u64,
    /// Reserved.
    pub _ebuf: [u8; 1024],
}

impl anchor_lang::AccountDeserialize for OracleAccountData {
    fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
        if buf.len() < OracleAccountData::discriminator().len() {
            return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorNotFound.into());
        }
        let given_disc = &buf[..8];
        if OracleAccountData::discriminator() != given_disc {
            return Err(
                anchor_lang::error::Error::from(anchor_lang::error::AnchorError {
                    error_name: anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch.name(),
                    error_code_number: anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch
                        .into(),
                    error_msg: anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch
                        .to_string(),
                    error_origin: Some(anchor_lang::error::ErrorOrigin::Source(
                        anchor_lang::error::Source {
                            filename: "programs/attestation_program/src/lib.rs",
                            line: 1u32,
                        },
                    )),
                    compared_values: None,
                })
                .with_account_name("OracleAccountData"),
            );
        }
        Self::try_deserialize_unchecked(buf)
    }
    fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
        let data: &[u8] = &buf[8..];
        bytemuck::try_from_bytes(data)
            .map(|r: &Self| *r)
            .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize.into())
    }
}

unsafe impl Pod for OracleAccountData {}
unsafe impl Zeroable for OracleAccountData {}

impl Discriminator for OracleAccountData {
    const DISCRIMINATOR: [u8; 8] = [128, 30, 16, 241, 170, 73, 55,  54];
}

impl Owner for OracleAccountData {
    fn owner() -> Pubkey {
        *SWITCHBOARD_ATTESTATION_PROGRAM_ID
    }
}
impl ZeroCopy for OracleAccountData {}

impl OracleAccountData {
    pub fn size() -> usize {
        8 + std::mem::size_of::<OracleAccountData>()
    }

    /// Returns the deserialized Switchboard Quote account
    ///
    /// # Arguments
    ///
    /// * `quote_account_info` - A Solana AccountInfo referencing an existing Switchboard QuoteAccount
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use switchboard_solana::OracleAccountData;
    ///
    /// let quote_account = OracleAccountData::new(quote_account_info)?;
    /// ```
    pub fn new<'info>(
        quote_account_info: &'info AccountInfo<'info>,
    ) -> anchor_lang::Result<Ref<'info, OracleAccountData>> {
        let data = quote_account_info.try_borrow_data()?;
        if data.len() < OracleAccountData::discriminator().len() {
            return Err(ErrorCode::AccountDiscriminatorNotFound.into());
        }

        let mut disc_bytes = [0u8; 8];
        disc_bytes.copy_from_slice(&data[..8]);
        if disc_bytes != OracleAccountData::discriminator() {
            return Err(ErrorCode::AccountDiscriminatorMismatch.into());
        }

        Ok(Ref::map(data, |data| {
            bytemuck::from_bytes(&data[8..std::mem::size_of::<OracleAccountData>() + 8])
        }))
    }

    /// Returns the deserialized Switchboard Quote account
    ///
    /// # Arguments
    ///
    /// * `data` - A Solana AccountInfo's data buffer
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use switchboard_solana::OracleAccountData;
    ///
    /// let quote_account = OracleAccountData::new(quote_account_info.try_borrow_data()?)?;
    /// ```
    pub fn new_from_bytes(data: &[u8]) -> anchor_lang::Result<&OracleAccountData> {
        if data.len() < OracleAccountData::discriminator().len() {
            return Err(ErrorCode::AccountDiscriminatorNotFound.into());
        }

        let mut disc_bytes = [0u8; 8];
        disc_bytes.copy_from_slice(&data[..8]);
        if disc_bytes != OracleAccountData::discriminator() {
            return Err(ErrorCode::AccountDiscriminatorMismatch.into());
        }

        Ok(bytemuck::from_bytes(
            &data[8..std::mem::size_of::<OracleAccountData>() + 8],
        ))
    }

    pub fn signer(&self) -> Pubkey {
        self.enclave.enclave_signer
    }

    pub fn is_verified(&self, clock: &Clock) -> bool {
        match self.enclave.verification_status.into() {
            VerificationStatus::VerificationOverride => true,
            VerificationStatus::VerificationSuccess => {
                self.enclave.valid_until > clock.unix_timestamp
            }
            _ => false,
        }
    }

    pub fn verify(&self, clock: &Clock) -> std::result::Result<(), OnDemandError> {
        if !self.is_verified(clock) {
            return Err(OnDemandError::InvalidQuote);
        }

        Ok(())
    }

    pub fn gateway_uri(&self) -> Option<String> {
        let uri = self.gateway_uri;
        let uri = String::from_utf8_lossy(&uri);
        let uri = uri
            .split_at(uri.find('\0').unwrap_or(uri.len()))
            .0
            .to_string();
        if uri.is_empty() {
            return None;
        }
        Some(uri)
    }

    pub fn ed25519_signer(&self) -> Option<Pubkey> {
        let key = self.enclave.enclave_signer;
        if key == Pubkey::default() {
            return None;
        }
        Some(key)
    }

    pub fn secp256k1_signer(&self) -> Option<[u8; 64]> {
        let key = self.enclave.secp256k1_signer;
        if key == [0u8; 64] {
            return None;
        }
        Some(key)
    }

    pub fn libsecp256k1_signer(&self) -> Option<libsecp256k1::PublicKey> {
        let bytes = self.secp256k1_signer()?;
        let tag_full_pubkey: Vec<u8> = vec![4u8];
        let bytes = [tag_full_pubkey, bytes.into()].concat().try_into().ok()?;
        libsecp256k1::PublicKey::parse(&bytes).ok()
    }

    cfg_client! {
        pub async fn fetch_async(
            client: &solana_client::nonblocking::rpc_client::RpcClient,
            pubkey: Pubkey,
        ) -> std::result::Result<Self, crate::OnDemandError> {
            crate::client::fetch_zerocopy_account_async(client, pubkey).await
        }
    }
}
