extends Node2D

#enum { PLAYING, GOING_IN, INSIDE, COMING_OUT}
#var status = PLAYING

enum Phase { START, CLEAN, KNIFE, HEATER, POISON, SQUID, ANGLER, NEXT }
var phase = Phase.START


func _ready() -> void:
	randomize()
	modulate = Color(0,0,0,1)
	blackout()


func _input(event: InputEvent) -> void:
#	if event.is_action_pressed("skip"):
#		blackout() #glitchy af, don't use unless I refactor code entirely
	if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
		if $Fish.dash_mutation:
			$Fish.dash()
		elif $Fish.poison_mutation:
			$Fish.fire_pellet() # Fire a pellet if appropriate and able


static func random_position(area: CollisionShape2D) -> Vector2:
	var _topleft  : Vector2 = area.global_position - area.shape.extents
	var _botright : Vector2 = area.global_position + area.shape.extents
	var _x = rand_range(_topleft.x, _botright.x)
	var _y = rand_range(_topleft.y, _botright.y)
	return Vector2(_x, _y)


func _on_HazardTimer_timeout() -> void:
	if phase == Phase.CLEAN or phase == Phase.POISON or phase == Phase.SQUID:
		var poison_scene = load("res://Hazards/Poison/Poison.tscn")
		var poison = poison_scene.instance()
		if phase == Phase.CLEAN:
			poison.modulate = Color(1,1,1,1)
		add_child(poison)
		poison.position = random_position($HazardSpawner)


func blackout() -> void:
	$Fish/ThoughtBubble.visible = false
	if not phase == Phase.START:
		yield($Fish.go_inside(), "completed")
	if not modulate == Color(0,0,0,1):
		$TweenBlackout.interpolate_property(self, "modulate", modulate, Color(0,0,0,1), 2, Tween.TRANS_LINEAR, Tween.EASE_IN)
		$TweenBlackout.start()
		yield($TweenBlackout, "tween_completed")
	next_phase()
	$House/HouseEyes.visible = false
	$House/HouseRight.z_index = $House/HouseLeft.z_index
	if phase == Phase.SQUID:
		var squid_scene = load("res://Hazards/Squid/Squid.tscn")
		var squid = squid_scene.instance()
		add_child(squid)
		var poison_scene = load("res://Hazards/Poison/Poison.tscn")
		var poison = poison_scene.instance()
		add_child(poison)
		poison.position = $House/RightEntrance.get_global_position()
		$Fish.poison_mutating = true
		$Fish.poison_mutation = true
	if phase == Phase.POISON:
		$SurviveTimer.start()
	if phase == Phase.HEATER:
		for knife in get_tree().get_nodes_in_group("Knife"):
			knife.queue_free()
		for fork in get_tree().get_nodes_in_group("Fork"):
			fork.queue_free()
		var heater_scene = load("res://Hazards/Heater/Heater.tscn")
		var heater = heater_scene.instance()
		add_child(heater)
	if phase == Phase.KNIFE:
		var fork_scene = load("res://Hazards/Fork/Fork.tscn")
		var fork = fork_scene.instance()
		add_child(fork)
	if not phase == Phase.CLEAN:
		$Fish.position = $House/RightEntrance.get_global_position() + Vector2(0,-100)
		$Fish.global_rotation_degrees = 180
		$Fish/AnimatedSprite.flip_v = true
		$Fish.visible = true
		$Fish.destination = $House/LeftEntrance.get_global_position()+Vector2(0,-100)
		$Fish/TweenSwim.interpolate_property($Fish, "position", $Fish.get_global_position(), $House/LeftEntrance.get_global_position()+Vector2(0,-100), 2, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
		$Fish/TweenSwim.start()
	$TweenBlackout.interpolate_property(self, "modulate", modulate, Color(1,1,1,1), 2, Tween.TRANS_LINEAR, Tween.EASE_IN)
	$TweenBlackout.start()
	yield($TweenBlackout, "tween_completed")
	
	if not phase == Phase.KNIFE:
		$Fish.invincible = false
	
	_think()


func next_phase() -> void:
	if phase == Phase.CLEAN: # Clean tank of pellets before poison phase
		var pellets = get_tree().get_nodes_in_group("Pellets")
		for pellet in pellets:
			pellet.queue_free()
	if phase == Phase.KNIFE: # Clean tank of poison before fork phase
		var pellets = get_tree().get_nodes_in_group("Pellets")
		for pellet in pellets:
			pellet.queue_free()
	phase += 1


func _think() -> void:
	var thought = $Fish/ThoughtBubble/Thought
	$Fish/ThoughtBubble/ThoughtTimer.start()
	if phase == Phase.CLEAN:
		$Fish/ThoughtBubble.visible = true
		thought.bbcode_text = \
			"\n[center][tornado]ooOOoo! " + \
			"Does it really rain yummy treats in here? :D"
	elif phase == Phase.POISON:
		$Fish/ThoughtBubble.visible = true
		thought.bbcode_text = \
			"\n[center]Wait, something's wrong with these flakes!\n" + \
			"[shake]Must... resist... eating...!"
	elif phase == Phase.KNIFE:
		$Fish/ThoughtBubble.visible = true
		thought.bbcode_text = \
			"[center]WTF?! That was close!! Let's turn this tragedy into " + \
			"a travesty!\n([rainbow]DASH[/rainbow] unlocked!)"
	elif phase == Phase.SQUID:
		$Fish/ThoughtBubble.visible = true
		thought.bbcode_text = \
			"[center]HOLY...! Wait a minute; I've got an idea!\n" + \
			"([rainbow]TAIL CANNON[/rainbow] unlocked!)"
	
	# Tween the $Thought.bbcode_text's percent_visible property


func _on_SurviveTimer_timeout() -> void:
	blackout()
