/*
 * sh_input.c -- Turn-based input handling for Space Hulk
 *
 * Copyright (C) 2026 fish fvch studios
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "quakedef.h"
#include "sh_game.h"

/* ============================================================
 * CONSOLE COMMAND WRAPPERS
 * ============================================================ */

static void SH_Cmd_Move (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_MOVE_FORWARD);
}

static void SH_Cmd_TurnLeft (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_TURN_LEFT);
}

static void SH_Cmd_TurnRight (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_TURN_RIGHT);
}

static void SH_Cmd_Turn180 (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_TURN_180);
}

static void SH_Cmd_Shoot (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_SHOOT);
}

static void SH_Cmd_Overwatch (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_OVERWATCH);
}

static void SH_Cmd_EndTurn (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_END_TURN);
}

static void SH_Cmd_NextMarine (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_SelectNext();
}

static void SH_Cmd_PrevMarine (void)
{
	if (SH_Active() && sh_game.phase == SH_PHASE_PLAYER)
		SH_Marine_SelectPrev();
}

/* ============================================================
 * INIT -- register console commands
 * ============================================================ */

void SH_Input_Init (void)
{
	Cmd_AddCommand("sh_move", SH_Cmd_Move);
	Cmd_AddCommand("sh_turn_left", SH_Cmd_TurnLeft);
	Cmd_AddCommand("sh_turn_right", SH_Cmd_TurnRight);
	Cmd_AddCommand("sh_turn_180", SH_Cmd_Turn180);
	Cmd_AddCommand("sh_shoot", SH_Cmd_Shoot);
	Cmd_AddCommand("sh_overwatch", SH_Cmd_Overwatch);
	Cmd_AddCommand("sh_end_turn", SH_Cmd_EndTurn);
	Cmd_AddCommand("sh_next_marine", SH_Cmd_NextMarine);
	Cmd_AddCommand("sh_prev_marine", SH_Cmd_PrevMarine);
}

/* ============================================================
 * KEY EVENT -- intercept keys during game
 *
 * Called from Key_EventWithKeycode() before normal key handling.
 * Returns true if we consumed the key.
 * ============================================================ */

qboolean SH_Input_KeyEvent (int key, qboolean down)
{
	if (!down)
		return false;	/* only handle key-down */

	if (sh_game.phase != SH_PHASE_PLAYER)
		return false;	/* only intercept during player phase */

	/* swallow input while animations play */
	if (SH_AnimBusy())
		return true;

	switch (key) {
	case 'w':
	case K_UPARROW:
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_MOVE_FORWARD);
		return true;

	case 'a':
	case K_LEFTARROW:
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_TURN_LEFT);
		return true;

	case 'd':
	case K_RIGHTARROW:
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_TURN_RIGHT);
		return true;

	case 's':
	case K_DOWNARROW:
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_TURN_180);
		return true;

	case 'f':
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_SHOOT);
		return true;

	case 'o':
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_OVERWATCH);
		return true;

	case 'e':
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_DOOR);
		return true;

	case K_TAB:
		SH_Marine_SelectNext();
		return true;

	case K_SPACE:
		SH_Marine_TryAction(sh_game.selected_marine, SH_ACTION_END_TURN);
		return true;
	}

	return false;
}
