extends CanvasLayer



var fade_in_duration: float = 2.0
var fade_out_duration: float = 2.0
var is_fading: bool = false
var is_loading: bool = false


@onready var interactive_loader: InteractiveLoader = $InteractiveLoader
@onready var color_rect: ColorRect = $ColorRect
@onready var loading_hint_animations: AnimationPlayer = $HintMargin/Control/LoadingHint/LoadingHintAnimations
@onready var loading_hint: TextureRect = $HintMargin/Control/LoadingHint


func _ready() -> void:
	color_rect.hide()


func load_to_path(path: String, unpause_game: bool = false) -> void:
	if is_loading:
		return
	
	is_loading = true

	await fade_in()
	
	var scene: PackedScene = await interactive_loader.load_threaded(path)
	
	assert(is_instance_valid(scene))
	
	get_tree().change_scene_to_packed(scene)
	
	if unpause_game:
		get_tree().paused = false
	
	fade_out()
	is_loading = false


func fade_in() -> void:
	color_rect.show()
	is_fading = true
	
	var tween: Tween = create_tween()
	tween.tween_property(color_rect, ^"color:a", 1.0, fade_in_duration)
	await tween.finished
	
	is_fading = false

	loading_hint_animations.play(&"hover")
	loading_hint.show()


func fade_out() -> void:
	loading_hint_animations.stop()
	loading_hint.hide()
	
	is_fading = true
	
	var tween: Tween = create_tween()
	tween.tween_property(color_rect, ^"color:a", 0.0, fade_out_duration)
	await tween.finished
	
	color_rect.hide()
	is_fading = false
