use {
    reovim_driver_input::{
        KeyCode, KeyEvent, KeyLookupState, KeySequence, KeymapQuery, ModeState, ResolveResult,
    },
    reovim_kernel::api::v1::ModeId,
};

use super::*;

fn key(c: char) -> KeyEvent {
    KeyEvent::new(KeyCode::Char(c))
}

fn test_state() -> ModeState {
    ModeState::new(DiagnosticsPanelMode::PANEL_ID)
}

/// Mock keymap that always returns `NotFound`.
struct NotFoundKeymap;

#[cfg_attr(coverage_nightly, coverage(off))]
impl KeymapQuery for NotFoundKeymap {
    fn query(&self, _mode: &ModeId, _keys: &KeySequence) -> KeyLookupState {
        KeyLookupState::NotFound
    }
}

fn resolve_input(keymap: &impl KeymapQuery) -> ResolveInput<'_> {
    static EMPTY_KEYS: KeySequence = KeySequence::new();
    static MODE: ModeId = DiagnosticsPanelMode::PANEL_ID;
    ResolveInput::new(&EMPTY_KEYS, &MODE, keymap)
}

#[test]
fn resolver_new() {
    let resolver = DiagnosticsPanelResolver::new();
    assert_eq!(resolver.mode_id(), &DiagnosticsPanelMode::PANEL_ID);
}

#[test]
fn resolver_default() {
    let resolver = DiagnosticsPanelResolver::default();
    assert_eq!(resolver.mode_id(), &DiagnosticsPanelMode::PANEL_ID);
}

#[test]
fn char_not_handled_without_binding() {
    let resolver = DiagnosticsPanelResolver::new();
    let mut state = test_state();
    let keymap = NotFoundKeymap;
    let input = resolve_input(&keymap);

    // Unlike microscope, chars go to keymap lookup, not InsertChar
    let result = resolver.resolve_with_keymap(&key('j'), &mut state, &input);
    assert!(matches!(result, ResolveResult::NotHandled));
}

#[test]
fn escape_not_handled_without_binding() {
    let resolver = DiagnosticsPanelResolver::new();
    let mut state = test_state();
    let keymap = NotFoundKeymap;
    let input = resolve_input(&keymap);

    let k = KeyEvent::new(KeyCode::Escape);
    let result = resolver.resolve_with_keymap(&k, &mut state, &input);
    assert!(matches!(result, ResolveResult::NotHandled));
}

#[test]
fn no_inheritance() {
    let resolver = DiagnosticsPanelResolver::new();
    assert!(resolver.inherits_from().is_none());
}

#[test]
fn reset_is_noop() {
    let mut resolver = DiagnosticsPanelResolver::new();
    resolver.reset();
    assert_eq!(resolver.mode_id(), &DiagnosticsPanelMode::PANEL_ID);
}

/// Mock keymap that returns `ExactOnly` for 'j'.
struct JKeymap;

#[cfg_attr(coverage_nightly, coverage(off))]
impl KeymapQuery for JKeymap {
    fn query(&self, _mode: &ModeId, keys: &KeySequence) -> KeyLookupState {
        if keys.len() == 1 && matches!(keys.as_slice()[0].code, KeyCode::Char('j')) {
            KeyLookupState::ExactOnly(crate::ids::TROUBLE_NEXT)
        } else {
            KeyLookupState::NotFound
        }
    }
}

#[test]
fn bound_key_executes() {
    let resolver = DiagnosticsPanelResolver::new();
    let mut state = test_state();
    let keymap = JKeymap;
    let input = resolve_input(&keymap);

    let result = resolver.resolve_with_keymap(&key('j'), &mut state, &input);
    assert!(matches!(result, ResolveResult::Execute(..)));
}

/// Mock keymap that returns `PrefixOnly`.
struct PrefixKeymap;

#[cfg_attr(coverage_nightly, coverage(off))]
impl KeymapQuery for PrefixKeymap {
    fn query(&self, _mode: &ModeId, _keys: &KeySequence) -> KeyLookupState {
        KeyLookupState::PrefixOnly
    }
}

#[test]
fn prefix_returns_pending() {
    let resolver = DiagnosticsPanelResolver::new();
    let mut state = test_state();
    let keymap = PrefixKeymap;
    let input = resolve_input(&keymap);

    let k = KeyEvent::new(KeyCode::Escape);
    let result = resolver.resolve_with_keymap(&k, &mut state, &input);
    assert!(matches!(result, ResolveResult::Pending));
}

/// Mock keymap that returns `ExactWithLonger`.
struct ExactWithLongerKeymap;

#[cfg_attr(coverage_nightly, coverage(off))]
impl KeymapQuery for ExactWithLongerKeymap {
    fn query(&self, _mode: &ModeId, _keys: &KeySequence) -> KeyLookupState {
        KeyLookupState::ExactWithLonger {
            exact: crate::ids::TROUBLE_CLOSE,
        }
    }
}

#[test]
fn exact_with_longer_executes() {
    let resolver = DiagnosticsPanelResolver::new();
    let mut state = test_state();
    let keymap = ExactWithLongerKeymap;
    let input = resolve_input(&keymap);

    let k = KeyEvent::new(KeyCode::Enter);
    let result = resolver.resolve_with_keymap(&k, &mut state, &input);
    assert!(matches!(result, ResolveResult::Execute(..)));
}
