extends Control



@onready var first_edit : TextEdit = %FirstNameEdit
@onready var last_edit : TextEdit = %LastNameEdit
@onready var sign_button : Button = %SignButton
@onready var portrait_button : TextureButton = %PortraitButton
@onready var signature : TextureRect = %Signature
@onready var write_sound : AudioStreamPlayer = $AudioStreamPlayer
@onready var portrait_customizer = $PortraitCustomizer
@onready var portrait : AdventurerPortrait = %HeroPortrait
@onready var portrait_container = %PortraitContainer
var first_set : bool = false
var last_set : bool = false
var portrait_set : bool = false

func validate_signature() -> void:
	sign_button.disabled = !(first_set and last_set and portrait_set)

func _on_portrait_customized() -> void:
	Game.player_data.appearance = portrait_customizer.appearance
	portrait.set_appearance(portrait_customizer.appearance)
	portrait_container.visible = true
	portrait_customizer.visible = false
	portrait_set = true
	validate_signature()

	
func _on_portrait_button_pressed() -> void:
	#Open the Portrait Screen
	portrait_customizer.visible = true


func _on_first_name_edit_text_changed() -> void:
	if first_edit.text != "":
		first_set = true
	else:
		last_set = true
	validate_signature()


func _on_last_name_edit_text_changed() -> void:
	if last_edit.text != "":
		last_set = true
	else:
		last_set = false
	validate_signature()

func update_signature_threshold(value : float) -> void:
	signature.set_instance_shader_parameter("threshold", value)

func _on_sign_button_pressed() -> void:
	sign_button.visible = false
	var tween = create_tween()
	Game.player_data.name = %FirstNameEdit.text
	Game.player_data.surname = %LastNameEdit.text
	write_sound.play()
	tween.tween_method(update_signature_threshold, -0.1, 1.1, .5)
	tween.tween_interval(1)
	tween.tween_callback(Game.switch_dialogue.bind("game_start", "Start Schedule"))
	#Sign the card.
	#Dialogic.paused = false
	#Dialogic.Jump.jump_to_label("")
	pass # Replace with function body.
