extends Node2D

@onready var next_dialogue: MarginContainer = %NextDialogue
@onready var bubbles: HBoxContainer = $Bubbles
@onready var bubbles_back: TileMapLayer = $BubblesBack
@onready var bubbles_interior: TileMapLayer = $BubblesInterior
var active_camera: SuperCamera
@onready var opening_fade_to_black: ColorRect = $OpeningFadeToBlack

@onready var opening_cutscene: AnimationPlayer = $OpeningCutscene
@onready var dance_cutscene: AnimationPlayer = $DanceCutscene

@onready var background_far: TileMapLayer = $BackgroundFar
@onready var background_middle: TileMapLayer = $BackgroundMiddle
@onready var background_close: TileMapLayer = $BackgroundClose
@onready var foreground_far: TileMapLayer = $ForegroundFar

@export_group("Parallax")
@export var parallax_far = 0.5
@export var parallax_middle = 0.4
@export var parallax_close = 0.3
@export var foreground = 0.2

@onready var choices_container: MarginContainer = %ChoicesContainer
@onready var red_hood_cutscene: AnimationPlayer = $RedHoodCutscene
@onready var final_cutscene: AnimationPlayer = $FinalCutscene

@onready var ambiance: AudioStreamPlayer = $Ambiance
var ambiance_stream: AudioStreamInteractive

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	for child in get_children():
		if is_instance_of(child, SuperCamera):
			child.became_active.connect(on_camera_became_active)
	
	ambiance.get_stream_playback().switch_to_clip_by_name("Start")
	opening_cutscene.play("opening_cutscene")
	opening_fade_to_black.visible = true

func on_camera_became_active(camera: SuperCamera):
	active_camera = camera
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	bubbles.global_position.x = active_camera.global_position.x - 270.0
	next_dialogue.global_position.x = active_camera.global_position.x + 133.0
	bubbles_back.global_position.x = active_camera.global_position.x
	bubbles_interior.global_position.x = active_camera.global_position.x
	choices_container.global_position.x = active_camera.global_position.x - 100
	opening_fade_to_black.global_position.x = active_camera.global_position.x - 320.0
	
	background_far.global_position.x = active_camera.global_position.x * parallax_far
	background_middle.global_position.x = active_camera.global_position.x * parallax_middle
	background_close.global_position.x = active_camera.global_position.x * parallax_close
	# foreground_far.global_position.x = active_camera.global_position.x * foreground
	
@onready var dance_music: AudioStreamPlayer = $DanceMusic

func _on_start_dancing() -> void:
	dance_cutscene.play("dance")
	ambiance.stop()
	
func restart_at_first_scene():
	get_tree().change_scene_to_file("res://main.tscn")
	
func to_main_menu():
	pass
	
func quit_game():
	pass


func _on_trigger_second_npc_dialogue_body_entered(body: Node2D) -> void:
	red_hood_cutscene.play("cutscene")


func _on_red_hood_sword_dialogue_dialogue_ended() -> void:
	red_hood_cutscene.play("flee")


func _on_final_armor_dialogue_ended() -> void:
	red_hood_cutscene.play("final")


func _on_final_cinematic_body_entered(body: Node2D) -> void:
	final_cutscene.play("cutscene")


func _on_balade_ambiance_body_entered(body: Node2D) -> void:
	ambiance.get_stream_playback().switch_to_clip_by_name("Forest")


func _on_squelettes_ambiance_body_entered(body: Node2D) -> void:
	ambiance.get_stream_playback().switch_to_clip_by_name("Squelettes")


func _on_cave_ambiance_body_entered(body: Node2D) -> void:
	ambiance.get_stream_playback().switch_to_clip_by_name("Cave")
