//! This code was AUTOGENERATED using the codama library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun codama to update it.
//!
//! <https://github.com/codama-idl/codama>
//!

use borsh::{BorshDeserialize, BorshSerialize};

pub const DELEGATE_STAKE_DISCRIMINATOR: u32 = 2;

/// Accounts.
#[derive(Debug)]
pub struct DelegateStake {
    /// Initialized stake account to be delegated
    pub stake: solana_pubkey::Pubkey,
    /// Vote account to which this stake will be delegated
    pub vote: solana_pubkey::Pubkey,
    /// Clock sysvar
    pub clock_sysvar: solana_pubkey::Pubkey,
    /// Stake history sysvar that carries stake warmup/cooldown history
    pub stake_history: solana_pubkey::Pubkey,
    /// Validator whitelist entry PDA for vote account
    pub whitelist_entry: solana_pubkey::Pubkey,
    /// Unused account, formerly the stake config
    pub unused: solana_pubkey::Pubkey,
    /// Stake authority
    pub stake_authority: solana_pubkey::Pubkey,
}

impl DelegateStake {
    pub fn instruction(&self) -> solana_instruction::Instruction {
        self.instruction_with_remaining_accounts(&[])
    }
    #[allow(clippy::arithmetic_side_effects)]
    #[allow(clippy::vec_init_then_push)]
    pub fn instruction_with_remaining_accounts(
        &self,
        remaining_accounts: &[solana_instruction::AccountMeta],
    ) -> solana_instruction::Instruction {
        let mut accounts = Vec::with_capacity(7 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new(self.stake, false));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.vote, false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.clock_sysvar,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.stake_history,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.whitelist_entry,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.unused,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.stake_authority,
            true,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let data = DelegateStakeInstructionData::new().try_to_vec().unwrap();

        solana_instruction::Instruction {
            program_id: crate::SPHERENET_STAKE_INTERFACE_ID,
            accounts,
            data,
        }
    }
}

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DelegateStakeInstructionData {
    discriminator: u32,
}

impl DelegateStakeInstructionData {
    pub fn new() -> Self {
        Self { discriminator: 2 }
    }

    pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
        borsh::to_vec(self)
    }
}

impl Default for DelegateStakeInstructionData {
    fn default() -> Self {
        Self::new()
    }
}

/// Instruction builder for `DelegateStake`.
///
/// ### Accounts:
///
///   0. `[writable]` stake
///   1. `[]` vote
///   2. `[optional]` clock_sysvar (default to `SysvarC1ock11111111111111111111111111111111`)
///   3. `[optional]` stake_history (default to `SysvarStakeHistory1111111111111111111111111`)
///   4. `[]` whitelist_entry
///   5. `[]` unused
///   6. `[signer]` stake_authority
#[derive(Clone, Debug, Default)]
pub struct DelegateStakeBuilder {
    stake: Option<solana_pubkey::Pubkey>,
    vote: Option<solana_pubkey::Pubkey>,
    clock_sysvar: Option<solana_pubkey::Pubkey>,
    stake_history: Option<solana_pubkey::Pubkey>,
    whitelist_entry: Option<solana_pubkey::Pubkey>,
    unused: Option<solana_pubkey::Pubkey>,
    stake_authority: Option<solana_pubkey::Pubkey>,
    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
}

impl DelegateStakeBuilder {
    pub fn new() -> Self {
        Self::default()
    }
    /// Initialized stake account to be delegated
    #[inline(always)]
    pub fn stake(&mut self, stake: solana_pubkey::Pubkey) -> &mut Self {
        self.stake = Some(stake);
        self
    }
    /// Vote account to which this stake will be delegated
    #[inline(always)]
    pub fn vote(&mut self, vote: solana_pubkey::Pubkey) -> &mut Self {
        self.vote = Some(vote);
        self
    }
    /// `[optional account, default to 'SysvarC1ock11111111111111111111111111111111']`
    /// Clock sysvar
    #[inline(always)]
    pub fn clock_sysvar(&mut self, clock_sysvar: solana_pubkey::Pubkey) -> &mut Self {
        self.clock_sysvar = Some(clock_sysvar);
        self
    }
    /// `[optional account, default to 'SysvarStakeHistory1111111111111111111111111']`
    /// Stake history sysvar that carries stake warmup/cooldown history
    #[inline(always)]
    pub fn stake_history(&mut self, stake_history: solana_pubkey::Pubkey) -> &mut Self {
        self.stake_history = Some(stake_history);
        self
    }
    /// Validator whitelist entry PDA for vote account
    #[inline(always)]
    pub fn whitelist_entry(&mut self, whitelist_entry: solana_pubkey::Pubkey) -> &mut Self {
        self.whitelist_entry = Some(whitelist_entry);
        self
    }
    /// Unused account, formerly the stake config
    #[inline(always)]
    pub fn unused(&mut self, unused: solana_pubkey::Pubkey) -> &mut Self {
        self.unused = Some(unused);
        self
    }
    /// Stake authority
    #[inline(always)]
    pub fn stake_authority(&mut self, stake_authority: solana_pubkey::Pubkey) -> &mut Self {
        self.stake_authority = Some(stake_authority);
        self
    }
    /// Add an additional account to the instruction.
    #[inline(always)]
    pub fn add_remaining_account(&mut self, account: solana_instruction::AccountMeta) -> &mut Self {
        self.__remaining_accounts.push(account);
        self
    }
    /// Add additional accounts to the instruction.
    #[inline(always)]
    pub fn add_remaining_accounts(
        &mut self,
        accounts: &[solana_instruction::AccountMeta],
    ) -> &mut Self {
        self.__remaining_accounts.extend_from_slice(accounts);
        self
    }
    #[allow(clippy::clone_on_copy)]
    pub fn instruction(&self) -> solana_instruction::Instruction {
        let accounts = DelegateStake {
            stake: self.stake.expect("stake is not set"),
            vote: self.vote.expect("vote is not set"),
            clock_sysvar: self.clock_sysvar.unwrap_or(solana_pubkey::pubkey!(
                "SysvarC1ock11111111111111111111111111111111"
            )),
            stake_history: self.stake_history.unwrap_or(solana_pubkey::pubkey!(
                "SysvarStakeHistory1111111111111111111111111"
            )),
            whitelist_entry: self.whitelist_entry.expect("whitelist_entry is not set"),
            unused: self.unused.expect("unused is not set"),
            stake_authority: self.stake_authority.expect("stake_authority is not set"),
        };

        accounts.instruction_with_remaining_accounts(&self.__remaining_accounts)
    }
}

/// `delegate_stake` CPI accounts.
pub struct DelegateStakeCpiAccounts<'a, 'b> {
    /// Initialized stake account to be delegated
    pub stake: &'b solana_account_info::AccountInfo<'a>,
    /// Vote account to which this stake will be delegated
    pub vote: &'b solana_account_info::AccountInfo<'a>,
    /// Clock sysvar
    pub clock_sysvar: &'b solana_account_info::AccountInfo<'a>,
    /// Stake history sysvar that carries stake warmup/cooldown history
    pub stake_history: &'b solana_account_info::AccountInfo<'a>,
    /// Validator whitelist entry PDA for vote account
    pub whitelist_entry: &'b solana_account_info::AccountInfo<'a>,
    /// Unused account, formerly the stake config
    pub unused: &'b solana_account_info::AccountInfo<'a>,
    /// Stake authority
    pub stake_authority: &'b solana_account_info::AccountInfo<'a>,
}

/// `delegate_stake` CPI instruction.
pub struct DelegateStakeCpi<'a, 'b> {
    /// The program to invoke.
    pub __program: &'b solana_account_info::AccountInfo<'a>,
    /// Initialized stake account to be delegated
    pub stake: &'b solana_account_info::AccountInfo<'a>,
    /// Vote account to which this stake will be delegated
    pub vote: &'b solana_account_info::AccountInfo<'a>,
    /// Clock sysvar
    pub clock_sysvar: &'b solana_account_info::AccountInfo<'a>,
    /// Stake history sysvar that carries stake warmup/cooldown history
    pub stake_history: &'b solana_account_info::AccountInfo<'a>,
    /// Validator whitelist entry PDA for vote account
    pub whitelist_entry: &'b solana_account_info::AccountInfo<'a>,
    /// Unused account, formerly the stake config
    pub unused: &'b solana_account_info::AccountInfo<'a>,
    /// Stake authority
    pub stake_authority: &'b solana_account_info::AccountInfo<'a>,
}

impl<'a, 'b> DelegateStakeCpi<'a, 'b> {
    pub fn new(
        program: &'b solana_account_info::AccountInfo<'a>,
        accounts: DelegateStakeCpiAccounts<'a, 'b>,
    ) -> Self {
        Self {
            __program: program,
            stake: accounts.stake,
            vote: accounts.vote,
            clock_sysvar: accounts.clock_sysvar,
            stake_history: accounts.stake_history,
            whitelist_entry: accounts.whitelist_entry,
            unused: accounts.unused,
            stake_authority: accounts.stake_authority,
        }
    }
    #[inline(always)]
    pub fn invoke(&self) -> solana_program_error::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], &[])
    }
    #[inline(always)]
    pub fn invoke_with_remaining_accounts(
        &self,
        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> solana_program_error::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
    }
    #[inline(always)]
    pub fn invoke_signed(&self, signers_seeds: &[&[&[u8]]]) -> solana_program_error::ProgramResult {
        self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
    }
    #[allow(clippy::arithmetic_side_effects)]
    #[allow(clippy::clone_on_copy)]
    #[allow(clippy::vec_init_then_push)]
    pub fn invoke_signed_with_remaining_accounts(
        &self,
        signers_seeds: &[&[&[u8]]],
        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> solana_program_error::ProgramResult {
        let mut accounts = Vec::with_capacity(7 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new(*self.stake.key, false));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.vote.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.clock_sysvar.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.stake_history.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.whitelist_entry.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.unused.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.stake_authority.key,
            true,
        ));
        remaining_accounts.iter().for_each(|remaining_account| {
            accounts.push(solana_instruction::AccountMeta {
                pubkey: *remaining_account.0.key,
                is_signer: remaining_account.1,
                is_writable: remaining_account.2,
            })
        });
        let data = DelegateStakeInstructionData::new().try_to_vec().unwrap();

        let instruction = solana_instruction::Instruction {
            program_id: crate::SPHERENET_STAKE_INTERFACE_ID,
            accounts,
            data,
        };
        let mut account_infos = Vec::with_capacity(8 + remaining_accounts.len());
        account_infos.push(self.__program.clone());
        account_infos.push(self.stake.clone());
        account_infos.push(self.vote.clone());
        account_infos.push(self.clock_sysvar.clone());
        account_infos.push(self.stake_history.clone());
        account_infos.push(self.whitelist_entry.clone());
        account_infos.push(self.unused.clone());
        account_infos.push(self.stake_authority.clone());
        remaining_accounts
            .iter()
            .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));

        if signers_seeds.is_empty() {
            solana_cpi::invoke(&instruction, &account_infos)
        } else {
            solana_cpi::invoke_signed(&instruction, &account_infos, signers_seeds)
        }
    }
}

/// Instruction builder for `DelegateStake` via CPI.
///
/// ### Accounts:
///
///   0. `[writable]` stake
///   1. `[]` vote
///   2. `[]` clock_sysvar
///   3. `[]` stake_history
///   4. `[]` whitelist_entry
///   5. `[]` unused
///   6. `[signer]` stake_authority
#[derive(Clone, Debug)]
pub struct DelegateStakeCpiBuilder<'a, 'b> {
    instruction: Box<DelegateStakeCpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> DelegateStakeCpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(DelegateStakeCpiBuilderInstruction {
            __program: program,
            stake: None,
            vote: None,
            clock_sysvar: None,
            stake_history: None,
            whitelist_entry: None,
            unused: None,
            stake_authority: None,
            __remaining_accounts: Vec::new(),
        });
        Self { instruction }
    }
    /// Initialized stake account to be delegated
    #[inline(always)]
    pub fn stake(&mut self, stake: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.stake = Some(stake);
        self
    }
    /// Vote account to which this stake will be delegated
    #[inline(always)]
    pub fn vote(&mut self, vote: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.vote = Some(vote);
        self
    }
    /// Clock sysvar
    #[inline(always)]
    pub fn clock_sysvar(
        &mut self,
        clock_sysvar: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.clock_sysvar = Some(clock_sysvar);
        self
    }
    /// Stake history sysvar that carries stake warmup/cooldown history
    #[inline(always)]
    pub fn stake_history(
        &mut self,
        stake_history: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.stake_history = Some(stake_history);
        self
    }
    /// Validator whitelist entry PDA for vote account
    #[inline(always)]
    pub fn whitelist_entry(
        &mut self,
        whitelist_entry: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.whitelist_entry = Some(whitelist_entry);
        self
    }
    /// Unused account, formerly the stake config
    #[inline(always)]
    pub fn unused(&mut self, unused: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.unused = Some(unused);
        self
    }
    /// Stake authority
    #[inline(always)]
    pub fn stake_authority(
        &mut self,
        stake_authority: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.stake_authority = Some(stake_authority);
        self
    }
    /// Add an additional account to the instruction.
    #[inline(always)]
    pub fn add_remaining_account(
        &mut self,
        account: &'b solana_account_info::AccountInfo<'a>,
        is_writable: bool,
        is_signer: bool,
    ) -> &mut Self {
        self.instruction
            .__remaining_accounts
            .push((account, is_writable, is_signer));
        self
    }
    /// Add additional accounts to the instruction.
    ///
    /// Each account is represented by a tuple of the `AccountInfo`, a `bool` indicating whether the account is writable or not,
    /// and a `bool` indicating whether the account is a signer or not.
    #[inline(always)]
    pub fn add_remaining_accounts(
        &mut self,
        accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> &mut Self {
        self.instruction
            .__remaining_accounts
            .extend_from_slice(accounts);
        self
    }
    #[inline(always)]
    pub fn invoke(&self) -> solana_program_error::ProgramResult {
        self.invoke_signed(&[])
    }
    #[allow(clippy::clone_on_copy)]
    #[allow(clippy::vec_init_then_push)]
    pub fn invoke_signed(&self, signers_seeds: &[&[&[u8]]]) -> solana_program_error::ProgramResult {
        let instruction = DelegateStakeCpi {
            __program: self.instruction.__program,

            stake: self.instruction.stake.expect("stake is not set"),

            vote: self.instruction.vote.expect("vote is not set"),

            clock_sysvar: self
                .instruction
                .clock_sysvar
                .expect("clock_sysvar is not set"),

            stake_history: self
                .instruction
                .stake_history
                .expect("stake_history is not set"),

            whitelist_entry: self
                .instruction
                .whitelist_entry
                .expect("whitelist_entry is not set"),

            unused: self.instruction.unused.expect("unused is not set"),

            stake_authority: self
                .instruction
                .stake_authority
                .expect("stake_authority is not set"),
        };
        instruction.invoke_signed_with_remaining_accounts(
            signers_seeds,
            &self.instruction.__remaining_accounts,
        )
    }
}

#[derive(Clone, Debug)]
struct DelegateStakeCpiBuilderInstruction<'a, 'b> {
    __program: &'b solana_account_info::AccountInfo<'a>,
    stake: Option<&'b solana_account_info::AccountInfo<'a>>,
    vote: Option<&'b solana_account_info::AccountInfo<'a>>,
    clock_sysvar: Option<&'b solana_account_info::AccountInfo<'a>>,
    stake_history: Option<&'b solana_account_info::AccountInfo<'a>>,
    whitelist_entry: Option<&'b solana_account_info::AccountInfo<'a>>,
    unused: Option<&'b solana_account_info::AccountInfo<'a>>,
    stake_authority: Option<&'b solana_account_info::AccountInfo<'a>>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}
