use crate::app::state::AppState;
use crate::game::GamePhase;
use crossterm::event::KeyCode;
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppAction {
Quit,
ToggleDebug,
StartGame,
ShowRules,
ShowDifficultySelect,
SelectEasyDifficulty,
SelectMediumDifficulty,
SelectHardDifficulty,
ReturnToMenu,
SelectNextCard,
SelectPrevCard,
ToggleMultiSelect,
ToggleCardSelection,
PlaySelectedCard,
PassTurn,
TakeCards,
StartNewGame,
AcknowledgeDraw,
}
pub fn handle_key_input(
app_state: &AppState,
game_phase: &GamePhase,
key: KeyCode,
) -> Option<AppAction> {
match key {
KeyCode::Char('q') | KeyCode::Char('Q') => return Some(AppAction::Quit),
_ => {}
}
match app_state {
AppState::MainMenu => match key {
KeyCode::Char('s') | KeyCode::Char('S') => Some(AppAction::StartGame),
KeyCode::Char('r') | KeyCode::Char('R') => Some(AppAction::ShowRules),
KeyCode::Char('a') | KeyCode::Char('A') => Some(AppAction::ShowDifficultySelect),
KeyCode::Char('d') | KeyCode::Char('D') => Some(AppAction::ToggleDebug),
_ => None,
},
AppState::DifficultySelect => match key {
KeyCode::Char('1') => Some(AppAction::SelectEasyDifficulty),
KeyCode::Char('2') => Some(AppAction::SelectMediumDifficulty),
KeyCode::Char('3') => Some(AppAction::SelectHardDifficulty),
KeyCode::Char('b') | KeyCode::Char('B') | KeyCode::Esc => Some(AppAction::ReturnToMenu),
_ => None,
},
AppState::RulesPage => match key {
KeyCode::Char('b') | KeyCode::Char('B') | KeyCode::Esc => Some(AppAction::ReturnToMenu),
_ => None,
},
AppState::Playing => {
match game_phase {
GamePhase::Drawing => {
Some(AppAction::AcknowledgeDraw)
}
GamePhase::Attack | GamePhase::Defense => match key {
KeyCode::Char('k') | KeyCode::Char('h') => Some(AppAction::SelectPrevCard),
KeyCode::Char('j') | KeyCode::Char('l') => Some(AppAction::SelectNextCard),
KeyCode::Up | KeyCode::Left => Some(AppAction::SelectPrevCard),
KeyCode::Down | KeyCode::Right => Some(AppAction::SelectNextCard),
KeyCode::Char('d') | KeyCode::Char('D') => Some(AppAction::ToggleDebug),
KeyCode::Char('m') | KeyCode::Char('M') => Some(AppAction::ToggleMultiSelect),
KeyCode::Char(' ') => Some(AppAction::ToggleCardSelection),
KeyCode::Enter => Some(AppAction::PlaySelectedCard),
KeyCode::Char('p') | KeyCode::Char('P') if *game_phase == GamePhase::Attack => {
Some(AppAction::PassTurn)
}
KeyCode::Char('t') | KeyCode::Char('T')
if *game_phase == GamePhase::Defense =>
{
Some(AppAction::TakeCards)
}
_ => None,
},
GamePhase::GameOver => match key {
KeyCode::Char('n') | KeyCode::Char('N') => Some(AppAction::StartNewGame),
_ => None,
},
_ => None, }
}
AppState::GameOver => match key {
KeyCode::Char('n') | KeyCode::Char('N') => Some(AppAction::StartNewGame),
_ => None,
},
}
}