//! Relay chain runtime mock.

mod xcm_config;
use pezframe_support::{
    construct_runtime, derive_impl, parameter_types,
    traits::{ConstU128, Disabled, Everything, Nothing, ProcessMessage, ProcessMessageError},
    weights::{Weight, WeightMeter},
};
use pezframe_system::EnsureRoot;
use pezkuwi_runtime_teyrchains::{
    configuration,
    inclusion::{AggregateMessageOrigin, UmpQueueId},
    origin, shared,
};
use pezsp_core::ConstU32;
use pezsp_runtime::{traits::IdentityLookup, AccountId32};
use xcm::latest::prelude::*;
use xcm_builder::{IsConcrete, SignedToAccountId32};
pub use xcm_config::*;
use xcm_executor::XcmExecutor;

pub type AccountId = AccountId32;
pub type Balance = u128;

parameter_types! {
    pub const BlockHashCount: u64 = 250;
}

#[derive_impl(pezframe_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Runtime {
    type AccountData = pezpallet_balances::AccountData<Balance>;
    type AccountId = AccountId;
    type Block = Block;
    type Lookup = IdentityLookup<Self::AccountId>;
}

#[derive_impl(pezpallet_balances::config_preludes::TestDefaultConfig)]
impl pezpallet_balances::Config for Runtime {
    type AccountStore = System;
    type Balance = Balance;
    type ExistentialDeposit = ConstU128<1>;
}

impl shared::Config for Runtime {
    type DisabledValidators = ();
}

impl configuration::Config for Runtime {
    type WeightInfo = configuration::TestWeightInfo;
}

pub type LocalOriginToLocation =
    SignedToAccountId32<RuntimeOrigin, AccountId, constants::RelayNetwork>;

impl pezpallet_xcm::Config for Runtime {
    type AdminOrigin = EnsureRoot<AccountId>;
    type AdvertisedXcmVersion = pezpallet_xcm::CurrentXcmVersion;
    // Aliasing is disabled: xcm_executor::Config::Aliasers is set to `Nothing`.
    type AuthorizedAliasConsideration = Disabled;
    type Currency = Balances;
    type CurrencyMatcher = IsConcrete<constants::TokenLocation>;
    // Anyone can execute XCM messages locally...
    type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
    type MaxLockers = ConstU32<8>;
    type MaxRemoteLockConsumers = ConstU32<0>;
    type RemoteLockConsumerIdentifier = ();
    type RuntimeCall = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type RuntimeOrigin = RuntimeOrigin;
    type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
    type SovereignAccountOf = location_converter::LocationConverter;
    type TrustedLockers = ();
    type UniversalLocation = constants::UniversalLocation;
    type Weigher = weigher::Weigher;
    type WeightInfo = pezpallet_xcm::TestWeightInfo;
    type XcmExecuteFilter = Nothing;
    type XcmExecutor = XcmExecutor<XcmConfig>;
    type XcmReserveTransferFilter = Everything;
    type XcmRouter = XcmRouter;
    type XcmTeleportFilter = Everything;

    const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
}

impl origin::Config for Runtime {}

type Block = pezframe_system::mocking::MockBlock<Runtime>;

parameter_types! {
    /// Amount of weight that can be spent per block to service messages.
    pub MessageQueueServiceWeight: Weight = Weight::from_parts(1_000_000_000, 1_000_000);
    pub const MessageQueueHeapSize: u32 = 65_536;
    pub const MessageQueueMaxStale: u32 = 16;
}

/// Message processor to handle any messages that were enqueued into the `MessageQueue` pezpallet.
pub struct MessageProcessor;
impl ProcessMessage for MessageProcessor {
    type Origin = AggregateMessageOrigin;

    fn process_message(
        message: &[u8],
        origin: Self::Origin,
        meter: &mut WeightMeter,
        id: &mut [u8; 32],
    ) -> Result<bool, ProcessMessageError> {
        let para = match origin {
            AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para,
        };
        xcm_builder::ProcessXcmMessage::<
			Junction,
			xcm_executor::XcmExecutor<XcmConfig>,
			RuntimeCall,
		>::process_message(message, Junction::TeyrChain(para.into()), meter, id)
    }
}

impl pezpallet_message_queue::Config for Runtime {
    type HeapSize = MessageQueueHeapSize;
    type IdleMaxServiceWeight = ();
    type MaxStale = MessageQueueMaxStale;
    type MessageProcessor = MessageProcessor;
    type QueueChangeHandler = ();
    type QueuePausedQuery = ();
    type RuntimeEvent = RuntimeEvent;
    type ServiceWeight = MessageQueueServiceWeight;
    type Size = u32;
    type WeightInfo = ();
}

construct_runtime!(
    pub enum Runtime
    {
        System: pezframe_system,
        Balances: pezpallet_balances,
        ParasOrigin: origin,
        XcmPezpallet: pezpallet_xcm,
        MessageQueue: pezpallet_message_queue,
    }
);
