extends Control

@onready var yonic_credits = $VBoxContainer/HBoxContainer/Yonic
@onready var reward_hint = $VBoxContainer/RewardHint
@onready var reward_hint_label = $VBoxContainer/RewardHint/Label

signal reward_requested
signal given_way

var t = 0.0
var opacity_from = 0.0
var opacity_to = 0.0

var reward_unlocked = false

func _ready():
	yonic_credits.visible = !Globals.nsfw
	modulate.a = opacity_from
	
func _process(delta):
	if t < 1.0 and opacity_from != opacity_to:
		t += delta
		modulate.a = lerp(opacity_from, opacity_to, t)
		if t >= 1.0 and opacity_to == 0.0 and reward_unlocked:
			reward_requested.emit()

func reveal(reward_applicable: bool):
	reward_unlocked = reward_applicable
	if reward_unlocked:
		reward_hint_label.text = "You performed commendably!\nSpecial reward unlocked! Press X to check it."
	t = 0.0
	opacity_to = 1.0
	
func give_way():
	opacity_from = 1.0
	opacity_to = 0.0
	t = 0.0

func _input(event: InputEvent):
	if event.is_action_pressed("ui_accept") and t >= 1.0:
		give_way()
		given_way.emit()
		get_viewport().set_input_as_handled()
