extends Node

signal profile_changed(new_profile)

var current_profile_id = 0

var profiles = {
	0: 'keyboard',
	1: 'controller'
}

var profile_order = ["move_left", "alt move_left", "move_right", "alt move_right", "jump", "run", "duck",
					 "alt duck", "up", "alt up",  "interact", "pause"]

var keyboard = {}
var controller = {}

func change_profile(id: int) -> Dictionary:
	current_profile_id = id
	var profile = get(profiles[id])
	
	for action_name in profile.keys():
		change_action_key(action_name, profile[action_name])
	
	emit_signal('profile_changed', profile)
	return profile

func commit_to_changes() -> void:
	for profile_name in profiles.values():
		var profile = get(profile_name)
		for action_name in profile.keys():
			if(action_name.ends_with("_old")):
				continue
			erase_old_action_event(action_name)
			profile[action_name + "_old"] = profile[action_name]
			var a_name = action_name
			# Assign the alternative event to the same action as the regular event
			if(action_name.begins_with("alt")):
				action_name = action_name.trim_prefix("alt ")
			InputMap.action_add_event(action_name, profile[a_name])

func change_action_key(action_name, event, old_event = null) -> bool:
	if(old_event != null):
		if(event.as_text().match("*Joy*") != old_event.as_text().match("*Joy*")):
			return false
		get_selected_profile()[action_name+"_old"] = old_event
	get_selected_profile()[action_name] = event
	return true

func erase_old_action_event(action_name: String) -> void:
	if(get_selected_profile().has(action_name+"_old")):
		var old_event = get_selected_profile()[action_name+"_old"]
		var event = get_selected_profile()[action_name]
		if(old_event != event):
			if(action_name.begins_with("alt")):
				action_name = action_name.trim_prefix("alt ")
			InputMap.action_erase_event(action_name, old_event)

func initialize_profiles() -> void:
	var actions: Array = InputMap.get_actions()
	actions.sort()
	for action in actions:
		var input_events: Array = InputMap.get_action_list(action)
		var controller_events: Array = []
		var keyboard_events: Array = []

		for input_event in input_events:
			if input_event.as_text().match("*Joypad*"):
				controller_events.append(input_event)
			if !input_event.as_text().match("*Joy*"):
				keyboard_events.append(input_event)

		if controller_events.size() > 0:
			controller[action] = controller_events[0]
			controller[action+"_old"] = controller_events[0]
		if controller_events.size() > 1:
			controller["alt " + action] = controller_events[1]
			controller["alt " + action +"_old"] = controller_events[1]
		
		if keyboard_events.size() > 0:
			keyboard[action] = keyboard_events[0]
			keyboard[action + "_old"] = keyboard_events[0]
		if keyboard_events.size() > 1:
			keyboard["alt " + action] = keyboard_events[1]
			keyboard["alt " + action + "_old"] = keyboard_events[1]
		
	change_profile(current_profile_id)

func get_selected_profile():
	return get(profiles[current_profile_id])

func _on_ProfilesMenu_item_selected(ID):
	change_profile(ID)
