use color_eyre::eyre::{Context, ContextCompat};
use color_eyre::owo_colors::OwoColorize;
use serde_json::{Value, json};

use crate::common::CallResultExt;
use crate::common::JsonRpcClientExt;

use super::send_ft::input_ft_contract_account_id;

mod amount_ft;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::TokensCommandsContext)]
#[interactive_clap(output_context = FtContractContext)]
pub struct FtContract {
    #[interactive_clap(skip_default_input_arg)]
    /// What is the ft-contract account ID?
    ft_contract_account_id: crate::types::account_id::AccountId,
    #[interactive_clap(subargs)]
    /// Specify sending FTCall command parameters:
    amount_ft: self::amount_ft::AmountFt,
}

#[derive(Debug, Clone)]
pub struct FtContractContext {
    global_context: crate::GlobalContext,
    signer_account_id: near_primitives::types::AccountId,
    ft_contract: crate::types::ft_properties::FtContract,
}

impl FtContractContext {
    pub fn from_previous_context(
        previous_context: super::TokensCommandsContext,
        scope: &<FtContract as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
    ) -> color_eyre::eyre::Result<Self> {
        let ft_contract_account_id: near_primitives::types::AccountId =
            scope.ft_contract_account_id.clone().into();

        if previous_context.global_context.offline {
            let ft_contract =
                crate::common::get_used_ft_contract_account_list(&previous_context.global_context.config.credentials_home_dir)
                    .into_iter()
                    .find(|ft_contract| ft_contract.ft_contract_account_id == ft_contract_account_id)
                    .wrap_err_with(|| {
                        format!("Contract <{}> was not found in the list of ft-contract accounts used.\nYou are currently using offline mode.\nTherefore, connect to the internet in online mode and use this command again to add ft-contract account to the list of ft-contract accounts used.",
                            ft_contract_account_id
                        )
                    })?;
            return Ok(Self {
                global_context: previous_context.global_context,
                signer_account_id: previous_context.owner_account_id,
                ft_contract,
            });
        }

        let ft_metadata = {
            let network_config = crate::common::find_network_where_account_exist(
                &previous_context.global_context,
                ft_contract_account_id.clone(),
            )?
            .wrap_err_with(|| {
                format!(
                    "Contract <{}> does not exist in networks",
                    ft_contract_account_id
                )
            })?;

            crate::types::ft_properties::params_ft_metadata(
                ft_contract_account_id.clone(),
                &network_config,
                near_primitives::types::Finality::Final.into(),
            )?
        };

        let ft_contract = crate::types::ft_properties::FtContract {
            ft_metadata,
            ft_contract_account_id,
        };

        crate::common::update_used_ft_contract_account_list(
            &previous_context.global_context.config.credentials_home_dir,
            &ft_contract,
        );

        Ok(Self {
            global_context: previous_context.global_context,
            signer_account_id: previous_context.owner_account_id,
            ft_contract,
        })
    }
}

impl FtContract {
    pub fn input_ft_contract_account_id(
        context: &super::TokensCommandsContext,
    ) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
        input_ft_contract_account_id(&context.global_context.config.credentials_home_dir)
    }
}

#[allow(clippy::too_many_arguments)]
#[tracing::instrument(
    name = "Creating a pre-populated transaction for signature ...",
    skip_all
)]
pub fn get_prepopulated_transaction(
    offline_mode: bool,
    network_config: &crate::config::NetworkConfig,
    ft_contract_account_id: &near_primitives::types::AccountId,
    receiver_account_id: &near_primitives::types::AccountId,
    signer_id: &near_primitives::types::AccountId,
    amount_ft: &crate::types::ft_properties::FungibleToken,
    memo: &str,
    msg: &str,
    deposit: crate::types::near_token::NearToken,
    gas: crate::common::NearGas,
) -> color_eyre::eyre::Result<crate::commands::PrepopulatedTransaction> {
    tracing::info!(target: "near_teach_me", "Creating a pre-populated transaction for signature ...");
    let args_ft_transfer_call = serde_json::to_vec(&crate::types::ft_properties::FtTransferCall {
        receiver_id: receiver_account_id.clone(),
        amount: amount_ft.amount(),
        memo: if memo.is_empty() {
            None
        } else {
            Some(memo.to_string())
        },
        msg: msg.to_string(),
    })?;

    let action_ft_transfer_call = near_primitives::transaction::Action::FunctionCall(Box::new(
        near_primitives::transaction::FunctionCallAction {
            method_name: "ft_transfer_call".to_string(),
            args: args_ft_transfer_call,
            gas: near_primitives::gas::Gas::from_gas(gas.as_gas()),
            deposit: deposit.into(),
        },
    ));

    if offline_mode {
        tracing::warn!(
            target: "near_teach_me",
            "{}{}",
            "Offline mode is enabled.".red(),
            crate::common::indent_payload(&format!("\n{}",
                format!("The transaction will be created with only the ft_transfer_call action\nwithout checking whether the ft-contrat account is registered on the <{}> network.\nMake sure that you have enough funds in your account to cover both the ft_transfer_call action and\nthe deposit if the recipient account is not registered on ft-contract <{}>.",
                    network_config.network_name,
                    ft_contract_account_id
                ).yellow()
            ))
        );
        return Ok(crate::commands::PrepopulatedTransaction {
            signer_id: signer_id.clone(),
            receiver_id: ft_contract_account_id.clone(),
            actions: vec![action_ft_transfer_call.clone()],
        });
    };

    let args = serde_json::to_vec(&json!({"account_id": receiver_account_id}))?;

    let call_result = network_config
        .json_rpc_client()
        .blocking_call_view_function(
            ft_contract_account_id,
            "storage_balance_of",
            args.clone(),
            near_primitives::types::Finality::Final.into(),
        )
        .wrap_err_with(|| {
            format!(
                "Failed to fetch query for view method: 'storage_balance_of' (contract <{}> on network <{}>)",
                ft_contract_account_id, network_config.network_name
            )
        })?;

    if call_result.parse_result_from_json::<Value>()?.is_null() {
        let action_storage_deposit = near_primitives::transaction::Action::FunctionCall(Box::new(
            near_primitives::transaction::FunctionCallAction {
                method_name: "storage_deposit".to_string(),
                args,
                gas: near_primitives::gas::Gas::from_gas(gas.as_gas()),
                deposit: near_token::NearToken::from_millinear(100),
            },
        ));
        return Ok(crate::commands::PrepopulatedTransaction {
            signer_id: signer_id.clone(),
            receiver_id: ft_contract_account_id.clone(),
            actions: vec![action_storage_deposit, action_ft_transfer_call.clone()],
        });
    }

    Ok(crate::commands::PrepopulatedTransaction {
        signer_id: signer_id.clone(),
        receiver_id: ft_contract_account_id.clone(),
        actions: vec![action_ft_transfer_call.clone()],
    })
}
