extends Panel

signal selection_made(result)

var prev_focus

var selected_action
var action_with_same_event
var new_event_for_selected_action
var old_event_for_selected_action

func open(selected_action: String, action_with_same_event: String,
 old_event_for_selected_action: InputEvent, new_event_for_selected_action: InputEvent) -> void:
	prev_focus = get_focus_owner()
	self.selected_action = selected_action
	self.action_with_same_event = action_with_same_event
	self.new_event_for_selected_action = new_event_for_selected_action
	self.old_event_for_selected_action = old_event_for_selected_action
	
	$"%Action1".text = selected_action
	var event_path1 = ControllerIcons._convert_event_to_path(old_event_for_selected_action)
	$"%ControllerButton1".path = event_path1
	
	$"%Action2".text = action_with_same_event
	var event_path2 = ControllerIcons._convert_event_to_path(new_event_for_selected_action)
	$"%ControllerButton2".path = event_path2
	
	visible = true
	mitigate_ui_navigation_spill_over(true)
	$"%Yes".grab_focus()

func mitigate_ui_navigation_spill_over(var value: bool):
	var mode = Control.FOCUS_NONE if value else Control.FOCUS_ALL
	var inverse_mode = Control.FOCUS_ALL if value else Control.FOCUS_NONE
	$"%No".focus_mode = inverse_mode
	$"%Yes".focus_mode = inverse_mode
	for line in $"%ActionKeyList".get_children():
		line.get_node("ChangeButton").focus_mode = mode

func close() -> void:
	visible = false
	mitigate_ui_navigation_spill_over(false)
	prev_focus.set_focus_mode(Control.FOCUS_ALL)
	prev_focus.grab_focus()

func swap() -> void:
	close()
	emit_signal("selection_made", true)

func do_not_swap() -> void:
	close()
	emit_signal("selection_made", false)

