extends Control

onready var grid = find_node("grid")
onready var key_dialog = get_node("key_dialog")

const INPUT_TYPE_KEY = 0
const INPUT_TYPE_JOYPAD = 1

var last_action:String
var last_event:InputEvent

var new_inputs = {}


func _ready():
	init_mapping()


func apply():
	config.set_value("controls","mapping",new_inputs)


func init_mapping():
	new_inputs = {}
	for action in config.CONTROL_ACTIONS:
		var action_label = Label.new()
		action_label.set_name(action + "_label")
		action_label.set_text(action)
		grid.add_child(action_label)

		var key_action_button = Button.new()
		key_action_button.set_h_size_flags(SIZE_SHRINK_CENTER)
		key_action_button.set_flat(true)
		key_action_button.set_meta("action",action)
		key_action_button.set_button_icon(get_node("icons/key").get_button_icon())

		var joypad_action_button = Button.new()
		joypad_action_button.set_h_size_flags(SIZE_SHRINK_CENTER)
		joypad_action_button.set_flat(true)
		joypad_action_button.set_meta("action",action)

		for event in InputMap.get_action_list(action):
			if event is InputEventKey:
				key_action_button.set_text(event.as_text())
				key_action_button.set_tooltip(event.as_text())
			elif event is InputEventJoypadButton:
				joypad_action_button.set_tooltip(str(event.button_index))
				if has_node("icons/joypad/" + str(event.button_index)):
					joypad_action_button.set_button_icon(get_node("icons/joypad/" + str(event.button_index)).get_button_icon())
				else:
					joypad_action_button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
					joypad_action_button.set_text(str(event.button_index))

		grid.add_child(key_action_button)
		key_action_button.connect("pressed",self,"_on_button_pressed",[key_action_button, INPUT_TYPE_KEY])

		grid.add_child(joypad_action_button)
		joypad_action_button.connect("pressed",self,"_on_button_pressed",[joypad_action_button,  INPUT_TYPE_JOYPAD])


func _unhandled_input(event):
	if key_dialog.is_visible_in_tree():
		var action_button = key_dialog.find_node("action_button")
		var check_usage = false
		if key_dialog.get_meta("type") == INPUT_TYPE_KEY && event is InputEventKey:
			check_usage = true
			last_event = event
			action_button.set_button_icon(get_node("icons/key").get_button_icon())
			action_button.set_text(event.as_text())
			action_button.set_tooltip(event.as_text())
		elif key_dialog.get_meta("type") == INPUT_TYPE_JOYPAD && event is InputEventJoypadButton:
			check_usage = true
			last_event = event
			action_button.set_tooltip(str(event.button_index))
			if has_node("icons/joypad/" + str(event.button_index)):
				action_button.set_text("")
				action_button.set_button_icon(get_node("icons/joypad/" + str(event.button_index)).get_button_icon())
			else:
				action_button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
				action_button.set_text(str(event.button_index))

		if check_usage:
			var has_event = ""
			for action in config.CONTROL_ACTIONS:
				if InputMap.event_is_action(event, action):
					has_event = action

			if not has_event == "" && not has_event == last_action:
				key_dialog.set_text(tr("KEY_ALREADY_TAKEN") + '\n\'' +  tr(has_event) + '\'')
				key_dialog.get_ok().set_disabled(true)
			else:
				key_dialog.set_text(tr("PRESS_KEY"))
				key_dialog.get_ok().set_disabled(false)


func _on_button_pressed(button, type):
	last_action = button.get_meta("action")
	last_event = null

	var action_button = key_dialog.find_node("action_button")
	action_button.set_text("")
	action_button.set_button_icon(null)
	key_dialog.set_text(tr("PRESS_KEY"))
	key_dialog.set_meta("type", type)
	key_dialog.connect("confirmed",self,"_on_dialog_confirmed",[button])
	key_dialog.get_ok().set_disabled(true)
	key_dialog.get_label().set_align(HALIGN_CENTER)
	key_dialog.popup_centered()


func _on_dialog_confirmed(button):
	if not new_inputs.has(last_action):
		new_inputs[last_action] = {}
	match key_dialog.get_meta("type"):
		INPUT_TYPE_KEY:
			if last_event is InputEventKey:
				new_inputs[last_action]["key"] = OS.get_scancode_string(last_event.scancode)
				button.set_text(last_event.as_text())
				button.set_tooltip(last_event.as_text())
		INPUT_TYPE_JOYPAD:
			if last_event is InputEventJoypadButton:
				new_inputs[last_action]["joypad"] = last_event.button_index
				button.set_tooltip(str(last_event.button_index))
				if has_node("icons/joypad/" + str(last_event.button_index)):
					button.set_button_icon(get_node("icons/joypad/" + str(last_event.button_index)).get_button_icon())
				else:
					button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
					button.set_text(str(last_event.button_index))