//! 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 crate::generated::types::RemainingAccountsInfo;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

/// Accounts.
#[derive(Debug)]
pub struct CollectRewardV2 {
    pub whirlpool: solana_pubkey::Pubkey,

    pub position_authority: solana_pubkey::Pubkey,

    pub position: solana_pubkey::Pubkey,

    pub position_token_account: solana_pubkey::Pubkey,

    pub reward_owner_account: solana_pubkey::Pubkey,

    pub reward_mint: solana_pubkey::Pubkey,

    pub reward_vault: solana_pubkey::Pubkey,

    pub reward_token_program: solana_pubkey::Pubkey,

    pub memo_program: solana_pubkey::Pubkey,
}

impl CollectRewardV2 {
    pub fn instruction(
        &self,
        args: CollectRewardV2InstructionArgs,
    ) -> solana_instruction::Instruction {
        self.instruction_with_remaining_accounts(args, &[])
    }
    #[allow(clippy::arithmetic_side_effects)]
    #[allow(clippy::vec_init_then_push)]
    pub fn instruction_with_remaining_accounts(
        &self,
        args: CollectRewardV2InstructionArgs,
        remaining_accounts: &[solana_instruction::AccountMeta],
    ) -> solana_instruction::Instruction {
        let mut accounts = Vec::with_capacity(9 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.whirlpool,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.position_authority,
            true,
        ));
        accounts.push(solana_instruction::AccountMeta::new(self.position, false));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.position_token_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.reward_owner_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.reward_mint,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.reward_vault,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.reward_token_program,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.memo_program,
            false,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let mut data = borsh::to_vec(&CollectRewardV2InstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&args).unwrap();
        data.append(&mut args);

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

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CollectRewardV2InstructionData {
    discriminator: [u8; 8],
}

impl CollectRewardV2InstructionData {
    pub fn new() -> Self {
        Self {
            discriminator: [177, 107, 37, 180, 160, 19, 49, 209],
        }
    }
}

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

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CollectRewardV2InstructionArgs {
    pub reward_index: u8,
    pub remaining_accounts_info: Option<RemainingAccountsInfo>,
}

/// Instruction builder for `CollectRewardV2`.
///
/// ### Accounts:
///
///   0. `[]` whirlpool
///   1. `[signer]` position_authority
///   2. `[writable]` position
///   3. `[]` position_token_account
///   4. `[writable]` reward_owner_account
///   5. `[]` reward_mint
///   6. `[writable]` reward_vault
///   7. `[]` reward_token_program
///   8. `[]` memo_program
#[derive(Clone, Debug, Default)]
pub struct CollectRewardV2Builder {
    whirlpool: Option<solana_pubkey::Pubkey>,
    position_authority: Option<solana_pubkey::Pubkey>,
    position: Option<solana_pubkey::Pubkey>,
    position_token_account: Option<solana_pubkey::Pubkey>,
    reward_owner_account: Option<solana_pubkey::Pubkey>,
    reward_mint: Option<solana_pubkey::Pubkey>,
    reward_vault: Option<solana_pubkey::Pubkey>,
    reward_token_program: Option<solana_pubkey::Pubkey>,
    memo_program: Option<solana_pubkey::Pubkey>,
    reward_index: Option<u8>,
    remaining_accounts_info: Option<RemainingAccountsInfo>,
    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
}

impl CollectRewardV2Builder {
    pub fn new() -> Self {
        Self::default()
    }
    #[inline(always)]
    pub fn whirlpool(&mut self, whirlpool: solana_pubkey::Pubkey) -> &mut Self {
        self.whirlpool = Some(whirlpool);
        self
    }
    #[inline(always)]
    pub fn position_authority(&mut self, position_authority: solana_pubkey::Pubkey) -> &mut Self {
        self.position_authority = Some(position_authority);
        self
    }
    #[inline(always)]
    pub fn position(&mut self, position: solana_pubkey::Pubkey) -> &mut Self {
        self.position = Some(position);
        self
    }
    #[inline(always)]
    pub fn position_token_account(
        &mut self,
        position_token_account: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.position_token_account = Some(position_token_account);
        self
    }
    #[inline(always)]
    pub fn reward_owner_account(
        &mut self,
        reward_owner_account: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.reward_owner_account = Some(reward_owner_account);
        self
    }
    #[inline(always)]
    pub fn reward_mint(&mut self, reward_mint: solana_pubkey::Pubkey) -> &mut Self {
        self.reward_mint = Some(reward_mint);
        self
    }
    #[inline(always)]
    pub fn reward_vault(&mut self, reward_vault: solana_pubkey::Pubkey) -> &mut Self {
        self.reward_vault = Some(reward_vault);
        self
    }
    #[inline(always)]
    pub fn reward_token_program(
        &mut self,
        reward_token_program: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.reward_token_program = Some(reward_token_program);
        self
    }
    #[inline(always)]
    pub fn memo_program(&mut self, memo_program: solana_pubkey::Pubkey) -> &mut Self {
        self.memo_program = Some(memo_program);
        self
    }
    #[inline(always)]
    pub fn reward_index(&mut self, reward_index: u8) -> &mut Self {
        self.reward_index = Some(reward_index);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn remaining_accounts_info(
        &mut self,
        remaining_accounts_info: RemainingAccountsInfo,
    ) -> &mut Self {
        self.remaining_accounts_info = Some(remaining_accounts_info);
        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 = CollectRewardV2 {
            whirlpool: self.whirlpool.expect("whirlpool is not set"),
            position_authority: self
                .position_authority
                .expect("position_authority is not set"),
            position: self.position.expect("position is not set"),
            position_token_account: self
                .position_token_account
                .expect("position_token_account is not set"),
            reward_owner_account: self
                .reward_owner_account
                .expect("reward_owner_account is not set"),
            reward_mint: self.reward_mint.expect("reward_mint is not set"),
            reward_vault: self.reward_vault.expect("reward_vault is not set"),
            reward_token_program: self
                .reward_token_program
                .expect("reward_token_program is not set"),
            memo_program: self.memo_program.expect("memo_program is not set"),
        };
        let args = CollectRewardV2InstructionArgs {
            reward_index: self.reward_index.clone().expect("reward_index is not set"),
            remaining_accounts_info: self.remaining_accounts_info.clone(),
        };

        accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
    }
}

/// `collect_reward_v2` CPI accounts.
pub struct CollectRewardV2CpiAccounts<'a, 'b> {
    pub whirlpool: &'b solana_account_info::AccountInfo<'a>,

    pub position_authority: &'b solana_account_info::AccountInfo<'a>,

    pub position: &'b solana_account_info::AccountInfo<'a>,

    pub position_token_account: &'b solana_account_info::AccountInfo<'a>,

    pub reward_owner_account: &'b solana_account_info::AccountInfo<'a>,

    pub reward_mint: &'b solana_account_info::AccountInfo<'a>,

    pub reward_vault: &'b solana_account_info::AccountInfo<'a>,

    pub reward_token_program: &'b solana_account_info::AccountInfo<'a>,

    pub memo_program: &'b solana_account_info::AccountInfo<'a>,
}

/// `collect_reward_v2` CPI instruction.
pub struct CollectRewardV2Cpi<'a, 'b> {
    /// The program to invoke.
    pub __program: &'b solana_account_info::AccountInfo<'a>,

    pub whirlpool: &'b solana_account_info::AccountInfo<'a>,

    pub position_authority: &'b solana_account_info::AccountInfo<'a>,

    pub position: &'b solana_account_info::AccountInfo<'a>,

    pub position_token_account: &'b solana_account_info::AccountInfo<'a>,

    pub reward_owner_account: &'b solana_account_info::AccountInfo<'a>,

    pub reward_mint: &'b solana_account_info::AccountInfo<'a>,

    pub reward_vault: &'b solana_account_info::AccountInfo<'a>,

    pub reward_token_program: &'b solana_account_info::AccountInfo<'a>,

    pub memo_program: &'b solana_account_info::AccountInfo<'a>,
    /// The arguments for the instruction.
    pub __args: CollectRewardV2InstructionArgs,
}

impl<'a, 'b> CollectRewardV2Cpi<'a, 'b> {
    pub fn new(
        program: &'b solana_account_info::AccountInfo<'a>,
        accounts: CollectRewardV2CpiAccounts<'a, 'b>,
        args: CollectRewardV2InstructionArgs,
    ) -> Self {
        Self {
            __program: program,
            whirlpool: accounts.whirlpool,
            position_authority: accounts.position_authority,
            position: accounts.position,
            position_token_account: accounts.position_token_account,
            reward_owner_account: accounts.reward_owner_account,
            reward_mint: accounts.reward_mint,
            reward_vault: accounts.reward_vault,
            reward_token_program: accounts.reward_token_program,
            memo_program: accounts.memo_program,
            __args: args,
        }
    }
    #[inline(always)]
    pub fn invoke(&self) -> solana_program_entrypoint::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_entrypoint::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
    }
    #[inline(always)]
    pub fn invoke_signed(
        &self,
        signers_seeds: &[&[&[u8]]],
    ) -> solana_program_entrypoint::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_entrypoint::ProgramResult {
        let mut accounts = Vec::with_capacity(9 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.whirlpool.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.position_authority.key,
            true,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.position.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.position_token_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.reward_owner_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.reward_mint.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.reward_vault.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.reward_token_program.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.memo_program.key,
            false,
        ));
        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 mut data = borsh::to_vec(&CollectRewardV2InstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&self.__args).unwrap();
        data.append(&mut args);

        let instruction = solana_instruction::Instruction {
            program_id: crate::WHIRLPOOL_ID,
            accounts,
            data,
        };
        let mut account_infos = Vec::with_capacity(10 + remaining_accounts.len());
        account_infos.push(self.__program.clone());
        account_infos.push(self.whirlpool.clone());
        account_infos.push(self.position_authority.clone());
        account_infos.push(self.position.clone());
        account_infos.push(self.position_token_account.clone());
        account_infos.push(self.reward_owner_account.clone());
        account_infos.push(self.reward_mint.clone());
        account_infos.push(self.reward_vault.clone());
        account_infos.push(self.reward_token_program.clone());
        account_infos.push(self.memo_program.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 `CollectRewardV2` via CPI.
///
/// ### Accounts:
///
///   0. `[]` whirlpool
///   1. `[signer]` position_authority
///   2. `[writable]` position
///   3. `[]` position_token_account
///   4. `[writable]` reward_owner_account
///   5. `[]` reward_mint
///   6. `[writable]` reward_vault
///   7. `[]` reward_token_program
///   8. `[]` memo_program
#[derive(Clone, Debug)]
pub struct CollectRewardV2CpiBuilder<'a, 'b> {
    instruction: Box<CollectRewardV2CpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> CollectRewardV2CpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(CollectRewardV2CpiBuilderInstruction {
            __program: program,
            whirlpool: None,
            position_authority: None,
            position: None,
            position_token_account: None,
            reward_owner_account: None,
            reward_mint: None,
            reward_vault: None,
            reward_token_program: None,
            memo_program: None,
            reward_index: None,
            remaining_accounts_info: None,
            __remaining_accounts: Vec::new(),
        });
        Self { instruction }
    }
    #[inline(always)]
    pub fn whirlpool(&mut self, whirlpool: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.whirlpool = Some(whirlpool);
        self
    }
    #[inline(always)]
    pub fn position_authority(
        &mut self,
        position_authority: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.position_authority = Some(position_authority);
        self
    }
    #[inline(always)]
    pub fn position(&mut self, position: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.position = Some(position);
        self
    }
    #[inline(always)]
    pub fn position_token_account(
        &mut self,
        position_token_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.position_token_account = Some(position_token_account);
        self
    }
    #[inline(always)]
    pub fn reward_owner_account(
        &mut self,
        reward_owner_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.reward_owner_account = Some(reward_owner_account);
        self
    }
    #[inline(always)]
    pub fn reward_mint(
        &mut self,
        reward_mint: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.reward_mint = Some(reward_mint);
        self
    }
    #[inline(always)]
    pub fn reward_vault(
        &mut self,
        reward_vault: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.reward_vault = Some(reward_vault);
        self
    }
    #[inline(always)]
    pub fn reward_token_program(
        &mut self,
        reward_token_program: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.reward_token_program = Some(reward_token_program);
        self
    }
    #[inline(always)]
    pub fn memo_program(
        &mut self,
        memo_program: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.memo_program = Some(memo_program);
        self
    }
    #[inline(always)]
    pub fn reward_index(&mut self, reward_index: u8) -> &mut Self {
        self.instruction.reward_index = Some(reward_index);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn remaining_accounts_info(
        &mut self,
        remaining_accounts_info: RemainingAccountsInfo,
    ) -> &mut Self {
        self.instruction.remaining_accounts_info = Some(remaining_accounts_info);
        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_entrypoint::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_entrypoint::ProgramResult {
        let args = CollectRewardV2InstructionArgs {
            reward_index: self
                .instruction
                .reward_index
                .clone()
                .expect("reward_index is not set"),
            remaining_accounts_info: self.instruction.remaining_accounts_info.clone(),
        };
        let instruction = CollectRewardV2Cpi {
            __program: self.instruction.__program,

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

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

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

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

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

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

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

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

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

#[derive(Clone, Debug)]
struct CollectRewardV2CpiBuilderInstruction<'a, 'b> {
    __program: &'b solana_account_info::AccountInfo<'a>,
    whirlpool: Option<&'b solana_account_info::AccountInfo<'a>>,
    position_authority: Option<&'b solana_account_info::AccountInfo<'a>>,
    position: Option<&'b solana_account_info::AccountInfo<'a>>,
    position_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    reward_owner_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    reward_mint: Option<&'b solana_account_info::AccountInfo<'a>>,
    reward_vault: Option<&'b solana_account_info::AccountInfo<'a>>,
    reward_token_program: Option<&'b solana_account_info::AccountInfo<'a>>,
    memo_program: Option<&'b solana_account_info::AccountInfo<'a>>,
    reward_index: Option<u8>,
    remaining_accounts_info: Option<RemainingAccountsInfo>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}
