extends Node2D

var fishes = [Fishin.FishType.GAR, Fishin.FishType.SALMON, Fishin.FishType.BASS, Fishin.FishType.HUMUHUMUNUKUNUKUAPUA_A, Fishin.FishType.CATFSIH, Fishin.FishType.DOGFISH, Fishin.FishType.ANGLERFISH, Fishin.FishType.MOBY_DICK, Fishin.FishType.LEAPING_SMACKEREL]
var fish_seen: Array

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass
	

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	# The most important functionality
	if Input.is_action_just_pressed("G"):
		$Meow.play()


func _on_microgame_spawn_timer_timeout() -> void:
	# Spawn a microgame window
	var window_instance = null
	if (randf() < 0.25):
		var window_scene = preload("res://scenes/microgames/microgame_window.tscn")
		window_instance = window_scene.instantiate()
		
		var fishin_scene = preload("res://scenes/microgames/fishin/node_2d.tscn")
		var fishin_instance = fishin_scene.instantiate()
		
		var stonk_scene = preload("res://scenes/stockticker/Stock Ticker.tscn")
		var stonk_instance = stonk_scene.instantiate()
		
		stonk_instance.buy.connect($"Score window (real)/Score window"._on_stock_ticker_buy)
		stonk_instance.sell.connect($"Score window (real)/Score window"._on_stock_ticker_sell)
		
		fishin_instance.fish_chosen.connect(stonk_instance._on_fishin_fish_chosen)
		fishin_instance.win.connect(on_other_fishin_win.bind(fishin_instance, stonk_instance, window_instance))
		
		window_instance.add_child(stonk_instance)
		window_instance.add_child(fishin_instance)
	else:
		var window_scene = preload("res://scenes/microgames/ad/ad_window.tscn")
		window_instance = window_scene.instantiate()
	
	add_child(window_instance)
	
	# Randomize window position
	var screen_size = DisplayServer.screen_get_size()
	var window_x = randi_range(0, screen_size.x - window_instance.size.x)
	var window_y = randi_range(0, screen_size.y - window_instance.size.y)
	window_instance.position = Vector2(window_x, window_y)
	
	# Connect signals
	window_instance.win.connect(_on_microgame_win)
	

func _on_microgame_win(window: MicrogameWindow):
	print("yay")


func _on_fishin_win(fish_caught: Fishin.FishType) -> void:
	$Fishin.queue_free()
	$"Stock ticker".start()
	$Clippette.visible = true
	$Clippette.start_dialogue()
	$"Score window (real)".visible = true
	fish_seen.push_back(fish_caught)
	

func _on_score_window_donezo() -> void:
	for child in get_children():
		if child is Window:
			child.queue_free()
	$"Microgame spawn timer".stop()
	$"Stock ticker".queue_free()
	get_window().position = Vector2i(10000, 10000)
	
	var new_window = Window.new()
	new_window.size = Vector2i(1280, 720)
	new_window.initial_position = Window.WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS
	new_window.unresizable = true
	new_window.borderless = true
	var gameover_scene = preload("res://scenes/gameover.tscn")
	var gameover_scene_instance = gameover_scene.instantiate()
	new_window.add_child(gameover_scene_instance)
	add_child(new_window)
	

# This is the one for the fishing microgame that appears after the first one
func on_other_fishin_win(fish_caught: Fishin.FishType, fishin: Fishin, stonk, window: Window):
	if not (fish_caught in fish_seen):
		fish_seen.push_back(fish_caught)
		fishin.queue_free()
		stonk.start()
	else:
		window.queue_free()


func _on_clippette_intro_finished() -> void:
	_on_microgame_spawn_timer_timeout()
	$"Microgame spawn timer".start()
