class_name ActionBox extends GridContainer

enum Action {
  ATTACK,
  MAGIC,
  ITEM,
  BACK
}

@export var btnAttack:Button
@export var btnMagic:Button
@export var btnItem:Button
@export var btnBack:Button

signal action(action:Action)

func _ready() -> void:
  btnAttack.pressed.connect(func(): action.emit(Action.ATTACK))
  btnMagic.pressed.connect(func(): action.emit(Action.MAGIC))
  btnItem.pressed.connect(func(): action.emit(Action.ITEM))
  btnBack.pressed.connect(func(): action.emit(Action.BACK))
  self.visible = false

func _unhandled_input(event:InputEvent) -> void:
  if !visible:
    return
  if event.is_action_pressed("ui_cancel"):
    action.emit(Action.BACK)
    get_viewport().set_input_as_handled()

func getAction(fighter:BattleFighter) -> Action:
  btnAttack.disabled = fighter.movePrimary == null || !fighter.movePrimary.canFighterUse(fighter)
  btnMagic.disabled = fighter.movesMagical.size() == 0
  btnItem.disabled = false

  # Focus the first available button so the controller can navigate immediately.
  if !btnAttack.disabled:
    btnAttack.grab_focus()
  elif !btnMagic.disabled:
    btnMagic.grab_focus()
  elif !btnItem.disabled:
    btnItem.grab_focus()
  else:
    btnBack.grab_focus()

  var act = await action
  return act
