extends Node2D

@onready var game_state = $GameState
@onready var explosion_generator = $ExplosionGenerator
@onready var battle_ui = $CanvasLayer/BattleUI
@onready var player: PlayableCharacter = $HiroPlayer
@onready var hiro_ai: AICharacter = $ShadowClone
@onready var reed_ai : AICharacter = $ReedAI
@onready var nem_npc = $NemNPC
@onready var cutscene_manager = $CutsceneManager
@onready var sound_manager = $SoundManager
@onready var fader_animation = $Fades/Control/FaderRect/AnimationPlayer
@onready var barrier = $Barrier
@onready var credits = $Fades/Control/Credits
@onready var reward_nsfw = $Fades/Control/NSFWReward
@onready var reward_sfw = $Fades/Control/SFWReward

@export var stage = 1

func _ready():
	battle_ui.initialize(player, hiro_ai)
	battle_ui.set_enemy_health_visible(false)
	hiro_ai.set_melee_box_enabled(false)
	fader_animation.play("fade_in_white")
	if cutscene_manager.cutscene != null and cutscene_manager.cutscene.auto_start_on_load:
		cutscene_manager.step()

func _on_player_defeated():
	if stage == 1:
		cutscene_manager.load_cutscene(load("res://Cutscenes/prototype_clone_defeated.tres"))
	elif stage == 2:
		if Globals.nsfw == true:
			sound_manager.play_sound("effect_smoke")
			explosion_generator.spawn(player.position)
		var variation = "nsfw" if Globals.nsfw else "normal"
		var outcome = "defeated" if reed_ai.health > 0 else "draw"
		var cutscene = "res://Cutscenes/prototype_reed_{0}_{1}.tres".format([outcome, variation])
		cutscene_manager.load_cutscene(load(cutscene))
		
func _on_cutscene_started(_cutscene_name):
	print("Cutscene " + _cutscene_name + " started")
	player.in_cutscene = true
	player.velocity = Vector2.ZERO
	game_state.can_pause = false
	reed_ai.velocity = Vector2.ZERO
	hiro_ai.velocity = Vector2.ZERO
	reed_ai.set_ai_enabled(false)
	hiro_ai.set_ai_enabled(false)

func _on_cutscene_finished(cutscene_name: String):
	print("Cutscene " + cutscene_name + " finished")
	player.in_cutscene = false
	game_state.can_pause = true
	if cutscene_name == "talking_to_nem":
		nem_npc.interaction_cutscene = null
		hiro_ai.set_ai_enabled(true)
		battle_ui.set_enemy_health_visible(true)
		barrier.process_mode = Node.PROCESS_MODE_INHERIT
	elif cutscene_name == "prototype_clone_defeated":
		reed_ai.set_ai_enabled(true)
		battle_ui.set_enemy_health_visible(true)
		barrier.process_mode = Node.PROCESS_MODE_INHERIT
	elif cutscene_name == "reed_loses":
		credits.visible = true
		credits.reveal(player.health == player.max_health)
		player.in_cutscene = true
	
func _on_credits_requested_reward():
	reward_nsfw.visible = Globals.nsfw
	reward_sfw.visible = not Globals.nsfw
	fader_animation.play("fade_in_black")

func _on_cutscene_manager_issued_command(command: CutsceneCommand, manager: CutsceneManager):
	if command is TurnCharacterCommand or command is LookAtTargetCommand:
		var character: CharacterBase = manager.root.get_node(command.character)
		var face_direction: Vector2 = command.get_direction(manager)
		assert(character is CharacterBase)
		character.set_animation_direction(face_direction)
		manager.step()
	elif command is MoveCameraCommand:
		var camera: LerpableCamera = manager.root.get_node(command.target_camera)
		var target_position = camera.focus_starting_position if command.to_initial else command.target
		var relative = true if command.to_initial else command.relative
		if command.teleport:
			camera.teleport_to(target_position, relative)
			manager.step()
		else:
			camera.lerp_to(target_position, relative, command.speed)
			if command.wait_until_completion:
				camera.move_completed.connect(manager.step, CONNECT_ONE_SHOT)
			else:
				manager.step()
	elif command is EmitMessageCommand:
		if command.message == "show_clone":
			hiro_ai.global_position = $PositionMarkers/Marker3.global_position
			explosion_generator.spawn(hiro_ai.global_position)
			hiro_ai.set_melee_box_enabled(true)
			manager.step()
		if command.message == "hide_clone":
			sound_manager.play_sound("effect_smoke")
			explosion_generator.spawn(hiro_ai.global_position)
			hiro_ai.global_position = Vector2(1000, 1000)
			hiro_ai.set_ai_enabled(false)
			hiro_ai.set_melee_box_enabled(false)
			battle_ui.set_enemy_health_visible(false)
			manager.step()
		elif command.message == "show_reed":
			reed_ai.global_position = Vector2(0, 100)
			reed_ai.get_node("MovementCollider").disabled = true
			reed_ai.set_melee_box_enabled(false)
			sound_manager.play_sound("door_open")
			manager.step()
		elif command.message == "fade_out":
			fader_animation.play("fade_out_black")
			manager.step()
		elif command.message == "fade_in":
			fader_animation.play("fade_in_black")
			manager.step()
		elif command.message == "scene_2":
			stage = 2
			player.health = player.max_health
			battle_ui.initialize(player, reed_ai)
			reed_ai.global_position = $PositionMarkers/Marker3.global_position
			player.global_position = $PositionMarkers/Marker2.global_position
			player.set_animation_direction(Vector2.RIGHT)
			player.has_nsfw_animations = Globals.nsfw
			reed_ai.set_animation_direction(Vector2.LEFT)
			$NPCs.visible = true
			reed_ai.set_melee_box_enabled(true)
			reed_ai.get_node("MovementCollider").disabled = false
			manager.step()

func _on_player_interacted_with_object(object: Node, interaction: Cutscene):
	cutscene_manager.load_cutscene(interaction, object)

func _on_cutscene_loaded(_cutscene_name: String, auto_start: bool):
	if auto_start:
		cutscene_manager.step()

func _on_shadow_clone_defeated():
	barrier.process_mode = Node.PROCESS_MODE_DISABLED
	cutscene_manager.load_cutscene(load("res://Cutscenes/prototype_clone_victory.tres"))

func _on_reed_ai_defeated():
	if player.health > 0:
		cutscene_manager.load_cutscene(load("res://Cutscenes/prototype_reed_victory.tres"))
