extends Node2D

# 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:
	print("WAAAHAHAHA MICROGAMES")
	
	# Spawn a microgame window
	var window_scene = preload("res://scenes/microgames/ad/ad_window.tscn")
	var window_instance = window_scene.instantiate()
	
	print(type_string(typeof(window_instance)))
	
	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)
	window_instance.lose.connect(_on_microgame_lose)
	window_instance.ad_open.connect(_on_ad_open)
	
	$"Score window (real)/Score window".net_worth.timesEquals(0.9)
	
func _on_ad_open(window: MicrogameWindow):
	$"Score window (real)/Score window".net_worth.timesEquals(0.95)

func _on_microgame_win(window: MicrogameWindow):
	print("yay")
	
func _on_microgame_lose(window: MicrogameWindow):
	print("noooooo")
	$LosingSoundEffect.play()
	$"Score window (real)/Score window".net_worth.timesEquals(0.8)


func _on_fishin_win() -> void:
	$Fishin.queue_free()
	$"Stock ticker".start()
	$"Microgame spawn timer".start()
	$Clippette.visible = true
	$Clippette.start_dialogue()
	$"Score window (real)".visible = true
	$Music.play()


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)
	
	$Music.volume_db = 10
	
	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)
