extends Node3D
class_name PlayerInput

@onready var status = (%Status as Status)
@onready var combo = (%Combo as Combo)
@onready var action = (%PlayerAction as PlayerAction)

var action_map = {}
var switch_action_list = ["attack_heavy","attack_light","jump","interact"]
var skill_action_list = ["skill1","skill2","skill3","skill4"]

func _ready():
	SignalManager.connect("input_action_pressed",on_input_action_pressed)
	SignalManager.connect("input_action_released",on_input_action_released)
	SignalManager.connect("input_action_move",on_input_action_move)

func on_input_action_pressed(action_name:String):
	if action_name in action_map:
		return
	action_map[action_name] = true
		
	action_name = get_switch_action(action_name)
	action_name = get_skill_action(action_name)
	Global.ui_mgr.input_action(action_name,true)
	if Global.is_control_pause:
		return
	if not action.check_action(action_name,true):
		combo.add_input_action(action_name)
	
func on_input_action_released(action_name:String):
	if not action_name in action_map:
		return
	action_map.erase(action_name)
		
	action_name = get_switch_action(action_name)
	action_name = get_skill_action(action_name)
	Global.ui_mgr.input_action(action_name,false)
	if Global.is_control_pause:
		return
	action.check_action(action_name,false)

func on_input_action_move(input_dir):
	Global.ui_mgr.input_dir(input_dir)
	if Global.is_control_pause:
		return
	status.input_dir = input_dir

func get_switch_action(action_name:String)->String:
	if not action_name in switch_action_list:
		return action_name
	if not status.is_switch:
		return action_name
	if status.target:
		return "lock_%s"%action_name
	else:
		return "free_%s"%action_name
		
func get_skill_action(action_name:String)->String:
	if not action_name in skill_action_list:
		return action_name
	var index = skill_action_list.find(action_name)
	action_name = switch_action_list[index]
	if status.target:
		return "lock_%s"%action_name
	else:
		return "free_%s"%action_name
