# Hurry Curry! - a game about cooking
# Copyright (C) 2025 Hurry Curry! contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends Node

enum EventType {
	KEYBOARD,
	JOYPAD,
	TOUCH,
	OTHER
}

func action_list() -> Array: # Array[String]
	return InputMap.get_actions().filter(func isBuiltIn(k: String): return !k.begins_with("ui_")).map(func (x): return str(x))

func update_input_map(_value, action: String):
	if not action in action_list(): return
	InputMap.action_erase_events(action)
	for event in Settings.read("input.%s" % action):
		InputMap.action_add_event(action, event)

func settings(extra) -> Array: # Array[GameSetting]
	var entries := []
	for k in action_list(): entries.append(InputSetting.new(k))
	entries.append_array(extra)
	return entries

func get_event_type(input_event: InputEvent) -> EventType:
	if input_event is InputEventKey or input_event is InputEventMouseButton:
		return EventType.KEYBOARD
	elif input_event is InputEventJoypadButton or input_event is InputEventJoypadMotion:
		return EventType.JOYPAD
	elif input_event is InputEventScreenTouch or input_event is InputEventScreenDrag:
		return EventType.TOUCH
	return EventType.OTHER

func display_input_event(input_event: InputEvent) -> String:
	if input_event is InputEventKey:
		var key_name = OS.get_keycode_string(input_event.physical_keycode if input_event.physical_keycode != 0 else input_event.keycode)
		return tr("c.settings.input.keyboard").format([key_name])
	elif input_event is InputEventMouseButton:
		return tr("c.settings.input.mouse_button").format([input_event.button_index])
	elif input_event is InputEventJoypadButton:
		return tr("c.settings.input.joypad").format([input_event.button_index])
	elif input_event is InputEventJoypadMotion:
		return tr("c.settings.input.joypad_axis").format([input_event.axis])
	else:
		return tr("c.settings.input.other_event")
