#![cfg(feature = "runtime-benchmarks")]

use super::*;

use crate::Pallet as GhostNetworks;
use frame_benchmarking::v1::{benchmarks, BenchmarkError};
use sp_runtime::Saturating;

const MAX_NAME_LEN: u32 = 20;
const MAX_ENDPOINT_LEN: u32 = 150;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
    frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

fn prepare_network<T: Config>(
    n: u32, m: u32,
) -> (<T as module::Config>::NetworkId, NetworkData) {
    let chain_id: <T as module::Config>::NetworkId = Default::default();
    let chain_id = chain_id.saturating_add((n + m).into());

    let mut gatekeeper = b"0x".to_vec();
    for i in 0..40 { gatekeeper.push(i); }

    let mut topic_name = b"0x".to_vec();
    for i in 0..64 { topic_name.push(i); }

    let network = NetworkData {
        chain_name: sp_std::vec![0x69; n as usize],
        default_endpoint: sp_std::vec![0x69; m as usize],
        gatekeeper,
        topic_name,
        finality_delay: Some(69),
        release_delay: Some(69),
        network_type: NetworkType::Evm,
        incoming_fee: 0,
        outgoing_fee: 0,
    };

    (chain_id, network)
}

fn create_network<T: Config>(
    chain_id: <T as module::Config>::NetworkId,
    network: NetworkData,
) -> Result<Option<NetworkData>, BenchmarkError> {
    let prev_network = match GhostNetworks::<T>::get(&chain_id) {
        Some(net) => net,
        None => {
            let authority = T::RegisterOrigin::try_successful_origin()
                .map_err(|_| BenchmarkError::Weightless)?;
            GhostNetworks::<T>::register_network(
                authority.clone(), chain_id.clone(), network.clone()
            ).map_err(|_| BenchmarkError::Weightless)?;
            network
        }
    };

    Ok(Some(prev_network))
}

benchmarks! {
    register_network {
        let i in 1 .. MAX_NAME_LEN;
        let j in 1 .. MAX_ENDPOINT_LEN;

        let (chain_id, network) = prepare_network::<T>(i, j);
        let authority = T::RegisterOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = GhostNetworks::<T>::networks(chain_id.clone());
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), network.clone())
    verify {
        assert_last_event::<T>(Event::NetworkRegistered {
            chain_id: chain_id.clone(), network,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_network_name {
        let n in 1 .. MAX_NAME_LEN;
        let name = sp_std::vec![0x42; n as usize];
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), name.clone())
    verify {
        assert_last_event::<T>(Event::NetworkNameUpdated {
            chain_id: chain_id.clone(), chain_name: name,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_network_endpoint {
        let n in 1 .. MAX_ENDPOINT_LEN;
        let endpoint = sp_std::vec![0x42; n as usize];
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), endpoint.clone())
    verify {
        assert_last_event::<T>(Event::NetworkEndpointUpdated {
            chain_id: chain_id.clone(), default_endpoint: endpoint,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_network_finality_delay {
        let delay = Some(1337);
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), delay)
    verify {
        assert_last_event::<T>(Event::NetworkFinalityDelayUpdated {
            chain_id: chain_id.clone(), finality_delay: delay,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_network_release_delay {
        let delay = Some(1337);
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), delay)
    verify {
        assert_last_event::<T>(Event::NetworkReleaseDelayUpdated {
            chain_id: chain_id.clone(), release_delay: delay,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_network_type {
        let network_type = NetworkType::Utxo;
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), network_type.clone())
    verify {
        assert_last_event::<T>(Event::NetworkTypeUpdated {
            chain_id: chain_id.clone(), network_type: network_type.clone(),
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_network_gatekeeper {
        let mut gatekeeper = b"0x".to_vec();
        for i in 0..40 { gatekeeper.push(i + 1); }
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), gatekeeper.clone())
    verify {
        assert_last_event::<T>(Event::NetworkGatekeeperUpdated {
            chain_id: chain_id.clone(), gatekeeper,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_network_topic_name {
        let topic_name = b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec();
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), topic_name.clone())
    verify {
        assert_last_event::<T>(Event::NetworkTopicNameUpdated {
            chain_id: chain_id.clone(), topic_name,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_incoming_network_fee {
        let incoming_fee = 1337;
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), incoming_fee)
    verify {
        assert_last_event::<T>(Event::NetworkIncomingFeeUpdated {
            chain_id: chain_id.clone(), incoming_fee,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    update_outgoing_network_fee {
        let outgoing_fee = 1337;
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::UpdateOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone(), outgoing_fee)
    verify {
        assert_last_event::<T>(Event::NetworkOutgoingFeeUpdated {
            chain_id: chain_id.clone(), outgoing_fee,
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    remove_network {
        let (chain_id, network) = prepare_network::<T>(1, 1);
        let authority = T::RemoveOrigin::try_successful_origin()
            .map_err(|_| BenchmarkError::Weightless)?;
        let prev_network = create_network::<T>(chain_id.clone(), network.clone())?;
    }: _<T::RuntimeOrigin>(authority, chain_id.clone())
    verify {
        assert_last_event::<T>(Event::NetworkRemoved {
            chain_id: chain_id.clone(),
        }.into());
        assert_ne!(GhostNetworks::<T>::networks(chain_id.clone()), prev_network);
    }

    impl_benchmark_test_suite!(GhostNetworks, crate::mock::ExtBuilder::build(), crate::mock::Test);
}
