use frame_support::{pallet_prelude::*, storage::PrefixIterator};
use sp_runtime::{
    traits::{AtLeast32BitUnsigned, Member},
    DispatchResult,
};

pub trait NetworkDataBasicHandler {
    type NetworkId: Parameter
        + Member
        + AtLeast32BitUnsigned
        + Default
        + Copy
        + TypeInfo
        + MaybeSerializeDeserialize
        + MaxEncodedLen;
}

pub trait NetworkDataInspectHandler<Network>: NetworkDataBasicHandler {
    fn count() -> u32;
    fn contains_key(n: &Self::NetworkId) -> bool;
    fn network_for_block(b: impl Into<usize>) -> Option<(Self::NetworkId, Network)>;
    fn get(n: &Self::NetworkId) -> Option<Network>;
    fn iter() -> PrefixIterator<(Self::NetworkId, Network)>;
    fn iter_indexes() -> impl Iterator<Item = Self::NetworkId>;
}

pub trait NetworkDataMutateHandler<Network, Balance>: NetworkDataInspectHandler<Network> {
    fn register(chain_id: Self::NetworkId, network: Network) -> DispatchResult;
    fn remove(chain_id: Self::NetworkId) -> DispatchResult;

    fn increase_gatekeeper_amount(
        chain_id: &Self::NetworkId,
        amount: &Balance,
    ) -> Result<Balance, ()>;
    fn decrease_gatekeeper_amount(
        chain_id: &Self::NetworkId,
        amount: &Balance,
    ) -> Result<Balance, ()>;

    fn accumulate_outgoing_imbalance(amount: &Balance) -> Result<Balance, ()>;
    fn accumulate_incoming_imbalance(amount: &Balance) -> Result<Balance, ()>;
    fn accumulate_commission(commission: &Balance) -> Result<Balance, ()>;
}
