class_name GameMenu extends Control

enum Tab { PARTY, ITEMS }

@export var SIDEBAR:ItemList
@export var PARTY_TAB:GameMenuPartyTab
@export var ITEMS_TAB:GameMenuItemsTab

var _currentTab:Tab = Tab.PARTY

func _ready() -> void:
  visible = false
  SIDEBAR.item_selected.connect(_onTabSelected)

func open() -> void:
  visible = true
  _selectTab(_currentTab)
  SIDEBAR.select(_currentTab)
  SIDEBAR.grab_focus()

func close() -> void:
  visible = false

func isOpen() -> bool:
  return visible

func _onTabSelected(index:int) -> void:
  _selectTab(index as Tab)

func _selectTab(tab:Tab) -> void:
  _currentTab = tab
  PARTY_TAB.visible = (tab == Tab.PARTY)
  ITEMS_TAB.visible = (tab == Tab.ITEMS)
  match tab:
    Tab.PARTY:
      PARTY_TAB.refresh()
    Tab.ITEMS:
      ITEMS_TAB.refresh()

func _unhandled_input(event:InputEvent) -> void:
  if event.is_action_pressed("menu"):
    if visible:
      close()
    elif !UI.dialogueActive && UI.TEXTBOX.isClosed:
      open()
    get_viewport().set_input_as_handled()
    return
  if !visible:
    return
  if event.is_action_pressed("ui_cancel"):
    close()
    get_viewport().set_input_as_handled()
