extends Control

@onready var discard_texture_rect: TextureRect = $MarginContainer/VBoxContainer/HBoxContainer/DiscardTextureRect
@onready var rewind_texture_rect: TextureRect = $MarginContainer/VBoxContainer/HBoxContainer/RewindTextureRect
@onready var attack_texture_rect: TextureRect = $MarginContainer/VBoxContainer/HBoxContainer2/AttackTextureRect
@onready var set_remnant_texture_rect: TextureRect = $MarginContainer/VBoxContainer/HBoxContainer2/SetRemnantTextureRect
@onready var time_shift_texture_rect: TextureRect = $MarginContainer/VBoxContainer/TimeShiftTextureRect
@onready var timeline_up_texture_rect: TextureRect = $MarginContainer/VBoxContainer/HBoxContainer3/TimelineUpTextureRect
@onready var timeline_down_texture_rect: TextureRect = $MarginContainer/VBoxContainer/HBoxContainer3/TimelineDownTextureRect

var discard_keyboard_texture
var discard_joycon_texture
var discard_ps_texture
var rewind_keyboard_texture
var rewind_joycon_texture
var rewind_ps_texture
var attack_keyboard_texture
var attack_joycon_texture
var attack_ps_texture
var set_remnant_keyboard_texture
var set_remnant_joycon_texture
var set_remnant_ps_texture
var time_shift_keyboard_texture
var time_shift_joycon_texture
var time_shift_ps_texture
var timeline_up_keyboard_texture
var timeline_up_joycon_texture
var timeline_up_ps_texture
var timeline_down_keyboard_texture
var timeline_down_joycon_texture
var timeline_down_ps_texture

var discard_enabled : bool = false

func _ready() -> void:
	disable_discard()
	if PlayerVariables.max_number_of_remnants <= 1:
		timeline_up_texture_rect.hide()
		timeline_down_texture_rect.hide()
	
	SignalBus.changed_input_type.connect(replace_sprites_by_type)
	discard_keyboard_texture = load(Utils.get_button_icon(Utils.KEYBOARD_BUTTONS.V))
	discard_joycon_texture = load(Utils.get_button_icon(Utils.XBOX_BUTTONS.RB))
	discard_ps_texture = load(Utils.get_button_icon(Utils.PS_BUTTONS.R1))
	
	rewind_keyboard_texture = load(Utils.get_button_icon(Utils.KEYBOARD_BUTTONS.C))
	rewind_joycon_texture = load(Utils.get_button_icon(Utils.XBOX_BUTTONS.LB))
	rewind_ps_texture = load(Utils.get_button_icon(Utils.PS_BUTTONS.L1))
	
	attack_keyboard_texture = load(Utils.get_button_icon(Utils.KEYBOARD_BUTTONS.MOUSE_LEFT))
	attack_joycon_texture = load(Utils.get_button_icon(Utils.XBOX_BUTTONS.X))
	attack_ps_texture = load(Utils.get_button_icon(Utils.PS_BUTTONS.SQUARE))
	
	set_remnant_keyboard_texture = load(Utils.get_button_icon(Utils.KEYBOARD_BUTTONS.E))
	set_remnant_joycon_texture = load(Utils.get_button_icon(Utils.XBOX_BUTTONS.B))
	set_remnant_ps_texture = load(Utils.get_button_icon(Utils.PS_BUTTONS.CIRCLE))
	
	time_shift_keyboard_texture = load(Utils.get_button_icon(Utils.KEYBOARD_BUTTONS.Q))
	time_shift_joycon_texture = load(Utils.get_button_icon(Utils.XBOX_BUTTONS.A))
	time_shift_ps_texture = load(Utils.get_button_icon(Utils.PS_BUTTONS.CROSS))
	
	timeline_up_keyboard_texture = load(Utils.get_button_icon(Utils.KEYBOARD_BUTTONS.R))
	timeline_up_joycon_texture = load(Utils.get_button_icon(Utils.XBOX_BUTTONS.LT))
	timeline_up_ps_texture = load(Utils.get_button_icon(Utils.PS_BUTTONS.L2))
	
	timeline_down_keyboard_texture = load(Utils.get_button_icon(Utils.KEYBOARD_BUTTONS.F))
	timeline_down_joycon_texture = load(Utils.get_button_icon(Utils.XBOX_BUTTONS.RT))
	timeline_down_ps_texture = load(Utils.get_button_icon(Utils.PS_BUTTONS.R2))
	
	replace_sprites_by_type(Globals.current_input_type)
	
	SignalBus.set_anchor.connect(enable_discard)
	SignalBus.anchor_ran_out.connect(disable_discard)
	
	#SignalBus.changed_remnant_timeline.connect(_discard)
	#discard_texture_rect.modulate = Color(0.3,0.3,0.3,0.7)

func _exit_tree() -> void:
	SignalBus.changed_input_type.disconnect(replace_sprites_by_type)
	SignalBus.changed_remnant_timeline.disconnect(_discard)

func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("discard_remnant") and discard_enabled:
		discard_texture_rect.modulate = Color(0.5,0.5,0.5,0.7)
	if Input.is_action_just_pressed("rewind_to_anchor"):
		rewind_texture_rect.modulate = Color(0.5,0.5,0.5,0.7)
	if Input.is_action_just_pressed("attack"):
		attack_texture_rect.modulate = Color(0.5,0.5,0.5,0.7)
	if Input.is_action_just_pressed("set_anchored_remnant"):
		set_remnant_texture_rect.modulate = Color(0.5,0.5,0.5,0.7)
	if Input.is_action_just_pressed("time_shift"):
		time_shift_texture_rect.modulate = Color(0.5,0.5,0.5,0.7)
	if Input.is_action_just_pressed("switch_remnant_timeline_up"):
		timeline_up_texture_rect.modulate = Color(0.5,0.5,0.5,0.7)
	if Input.is_action_just_pressed("switch_remnant_timeline_down"):
		timeline_down_texture_rect.modulate = Color(0.5,0.5,0.5,0.7)
	
	if Input.is_action_just_released("discard_remnant") and discard_enabled:
		discard_texture_rect.modulate = Color(1.0, 1.0, 1.0, 1.0)
	if Input.is_action_just_released("rewind_to_anchor"):
		rewind_texture_rect.modulate = Color(1.0, 1.0, 1.0, 1.0)
	if Input.is_action_just_released("attack"):
		attack_texture_rect.modulate = Color(1.0, 1.0, 1.0, 1.0)
	if Input.is_action_just_released("set_anchored_remnant"):
		set_remnant_texture_rect.modulate = Color(1.0, 1.0, 1.0, 1.0)
	if Input.is_action_just_released("time_shift"):
		time_shift_texture_rect.modulate = Color(1.0, 1.0, 1.0, 1.0)
	if Input.is_action_just_released("switch_remnant_timeline_up"):
		timeline_up_texture_rect.modulate = Color(1.0, 1.0, 1.0, 1.0)
	if Input.is_action_just_released("switch_remnant_timeline_down"):
		timeline_down_texture_rect.modulate = Color(1.0, 1.0, 1.0, 1.0)

func replace_sprites_by_type(input_type : Utils.INPUT_TYPE):
	match input_type:
		Utils.INPUT_TYPE.KEYBOARD:
			discard_texture_rect.texture = discard_keyboard_texture
			rewind_texture_rect.texture = rewind_keyboard_texture
			attack_texture_rect.texture = attack_keyboard_texture
			set_remnant_texture_rect.texture = set_remnant_keyboard_texture
			time_shift_texture_rect.texture = time_shift_keyboard_texture
			timeline_up_texture_rect.texture = timeline_up_keyboard_texture
			timeline_down_texture_rect.texture = timeline_down_keyboard_texture
		Utils.INPUT_TYPE.PLAYSTATION:
			discard_texture_rect.texture = discard_ps_texture
			rewind_texture_rect.texture = rewind_ps_texture
			attack_texture_rect.texture = attack_ps_texture
			set_remnant_texture_rect.texture = set_remnant_ps_texture
			time_shift_texture_rect.texture = time_shift_ps_texture
			timeline_up_texture_rect.texture = timeline_up_ps_texture
			timeline_down_texture_rect.texture = timeline_down_ps_texture
		_:
			discard_texture_rect.texture = discard_joycon_texture
			rewind_texture_rect.texture = rewind_joycon_texture
			attack_texture_rect.texture = attack_joycon_texture
			set_remnant_texture_rect.texture = set_remnant_joycon_texture
			time_shift_texture_rect.texture = time_shift_joycon_texture
			timeline_up_texture_rect.texture = timeline_up_joycon_texture
			timeline_down_texture_rect.texture = timeline_down_joycon_texture

func enable_discard():
	discard_enabled = true
	discard_texture_rect.modulate = Color(1.0, 1.0, 1.0, 1.0)

func disable_discard():
	discard_enabled = false
	discard_texture_rect.modulate = Color(0.3,0.3,0.3,0.7)

func _discard(current_timeline : int):
	if current_timeline == 0:
		disable_discard()
	else:
		enable_discard()
