use super::*;
use frame_support::{assert_noop, assert_ok, weights::Weight};
use mock::{
    new_test_ext, Logger, LoggerCall, RuntimeCall, RuntimeEvent as TestEvent, RuntimeOrigin, Sudo,
    SudoCall, System, Test,
};

#[test]
fn test_setup_works() {
    new_test_ext(1).execute_with(|| {
        assert_eq!(Key::<Test>::get(), Some(1u64));
        assert!(Logger::i32_log().is_empty());
        assert!(Logger::account_log().is_empty());
    });
}

#[docify::export]
#[test]
fn sudo_basics() {
    new_test_ext(1).execute_with(|| {
        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
            i: 42,
            weight: Weight::from_parts(1_000, 0),
        }));
        assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call));
        assert_eq!(Logger::i32_log(), vec![42i32]);

        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
            i: 42,
            weight: Weight::from_parts(1_000, 0),
        }));
        assert_noop!(
            Sudo::sudo(RuntimeOrigin::signed(2), call),
            Error::<Test>::RequireSudo
        );
    });
}

#[test]
fn sudo_emits_events_correctly() {
    new_test_ext(1).execute_with(|| {
        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
            i: 42,
            weight: Weight::from_parts(1, 0),
        }));
        assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call));
        System::assert_has_event(TestEvent::Sudo(Event::Sudid {
            sudo_result: Ok(()),
        }));
    })
}

#[test]
fn sudo_unchecked_weight_basics() {
    new_test_ext(1).execute_with(|| {
        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
            i: 42,
            weight: Weight::from_parts(1_000, 0),
        }));
        assert_ok!(Sudo::sudo_unchecked_weight(
            RuntimeOrigin::signed(1),
            call,
            Weight::from_parts(1_000, 0)
        ));
        assert_eq!(Logger::i32_log(), vec![42i32]);

        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
            i: 42,
            weight: Weight::from_parts(1_000, 0),
        }));
        assert_noop!(
            Sudo::sudo_unchecked_weight(
                RuntimeOrigin::signed(2),
                call,
                Weight::from_parts(1_000, 0)
            ),
            Error::<Test>::RequireSudo,
        );
        assert_eq!(Logger::i32_log(), vec![42i32]);

        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
            i: 42,
            weight: Weight::from_parts(1, 0),
        }));
        let sudo_unchecked_weight_call = SudoCall::sudo_unchecked_weight {
            call,
            weight: Weight::from_parts(1_000, 0),
        };
        let info = sudo_unchecked_weight_call.get_dispatch_info();
        assert_eq!(info.weight, Weight::from_parts(1_000, 0));
    });
}

#[test]
fn sudo_unchecked_weight_emits_events_correctly() {
    new_test_ext(1).execute_with(|| {
        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
            i: 42,
            weight: Weight::from_parts(1, 0),
        }));
        assert_ok!(Sudo::sudo_unchecked_weight(
            RuntimeOrigin::signed(1),
            call,
            Weight::from_parts(1_000, 0)
        ));
        System::assert_has_event(TestEvent::Sudo(Event::Sudid {
            sudo_result: Ok(()),
        }));
    })
}

#[docify::export]
#[test]
fn set_key_basics() {
    new_test_ext(1).execute_with(|| {
        assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2));
        assert_eq!(Key::<Test>::get(), Some(2u64));
    });

    new_test_ext(1).execute_with(|| {
        assert_noop!(
            Sudo::set_key(RuntimeOrigin::signed(2), 3),
            Error::<Test>::RequireSudo
        );
    });
}

#[test]
fn set_key_emits_events_correctly() {
    new_test_ext(1).execute_with(|| {
        assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2));
        System::assert_has_event(TestEvent::Sudo(Event::KeyChanged {
            old: Some(1),
            new: 2,
        }));
        assert_ok!(Sudo::set_key(RuntimeOrigin::signed(2), 4));
        System::assert_has_event(TestEvent::Sudo(Event::KeyChanged {
            old: Some(2),
            new: 4,
        }));
    });
}

#[test]
fn remove_key_works() {
    new_test_ext(1).execute_with(|| {
        assert_ok!(Sudo::remove_key(RuntimeOrigin::signed(1)));
        assert!(Key::<Test>::get().is_none());
        System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {}));

        assert_noop!(
            Sudo::remove_key(RuntimeOrigin::signed(1)),
            Error::<Test>::RequireSudo
        );
        assert_noop!(
            Sudo::set_key(RuntimeOrigin::signed(1), 1),
            Error::<Test>::RequireSudo
        );
    });
}

#[test]
fn using_root_origin_works() {
    new_test_ext(1).execute_with(|| {
        assert_ok!(Sudo::remove_key(RuntimeOrigin::root()));
        assert!(Key::<Test>::get().is_none());
        System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {}));

        assert_ok!(Sudo::set_key(RuntimeOrigin::root(), 1));
        assert_eq!(Some(1), Key::<Test>::get());
    });
}

#[test]
fn sudo_as_basics() {
    new_test_ext(1).execute_with(|| {
        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
            i: 42,
            weight: Weight::from_parts(1_000, 0),
        }));
        assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
        assert!(Logger::i32_log().is_empty());
        assert!(Logger::account_log().is_empty());

        let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
            i: 42,
            weight: Weight::from_parts(1, 0),
        }));
        assert_noop!(
            Sudo::sudo_as(RuntimeOrigin::signed(3), 2, call),
            Error::<Test>::RequireSudo
        );

        let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
            i: 42,
            weight: Weight::from_parts(1, 0),
        }));
        assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
        assert_eq!(Logger::i32_log(), vec![42i32]);
        assert_eq!(Logger::account_log(), vec![2]);
    });
}

#[docify::export]
#[test]
fn sudo_as_emits_events_correctly() {
    new_test_ext(1).execute_with(|| {
        let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
            i: 42,
            weight: Weight::from_parts(1, 0),
        }));
        assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
        System::assert_has_event(TestEvent::Sudo(Event::SudoAsDone {
            sudo_result: Ok(()),
        }));
    });
}
