use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use super::{InputContext, InputResult};
use crate::commands::AppCommand;
use crate::engine::AudioCommand;
use crate::state::SidePanel;
use cagire_ratatui::TreeLineKind;

pub(super) fn handle_panel_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
    let state = match &mut ctx.app.panel.side {
        Some(SidePanel::SampleBrowser(s)) => s,
        None => return InputResult::Continue,
    };

    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);

    if state.search_active {
        match key.code {
            KeyCode::Esc => {
                state.clear_search();
            }
            KeyCode::Backspace => {
                state.search_query.pop();
                state.update_search();
            }
            KeyCode::Enter => {
                state.search_active = false;
            }
            KeyCode::Char(c) => {
                state.search_query.push(c);
                state.update_search();
            }
            _ => {}
        }
    } else if ctrl {
        match key.code {
            KeyCode::Up => {
                for _ in 0..10 {
                    state.move_up();
                }
            }
            KeyCode::Down => {
                for _ in 0..10 {
                    state.move_down(30);
                }
            }
            _ => {}
        }
    } else {
        match key.code {
            KeyCode::Up | KeyCode::Char('k') => state.move_up(),
            KeyCode::Down | KeyCode::Char('j') => state.move_down(30),
            KeyCode::PageUp => {
                for _ in 0..20 {
                    state.move_up();
                }
            }
            KeyCode::PageDown => {
                for _ in 0..20 {
                    state.move_down(30);
                }
            }
            KeyCode::Enter | KeyCode::Right => {
                if let Some(entry) = state.current_entry() {
                    match entry.kind {
                        TreeLineKind::File => {
                            let folder = &entry.folder;
                            let idx = entry.index;
                            let cmd = format!("/sound/{folder}/n/{idx}/gain/1.00/dur/1");
                            let _ = ctx
                                .audio_tx
                                .load()
                                .send(AudioCommand::Evaluate { cmd, time: None });
                        }
                        _ => state.toggle_expand(),
                    }
                }
            }
            KeyCode::Left => state.collapse_at_cursor(),
            KeyCode::Char('/') => state.activate_search(),
            KeyCode::Esc => {
                if state.has_filter() {
                    state.clear_filter();
                } else {
                    ctx.dispatch(AppCommand::ClosePanel);
                }
            }
            KeyCode::Tab => {
                ctx.dispatch(AppCommand::ClosePanel);
            }
            _ => {}
        }
    }
    InputResult::Continue
}
