extends Control

@export var adScene: PackedScene
@export var clickerScene: PackedScene
@export var bannerSprites: Array[Texture2D]

# Game balancing

const winCost = 1000000

const loseCount = 200

var killMoney = 50

var minSpawns = 1

var maxSpawns = 6

var difficultyRate = 1.02

var difficultyCount = 0.5

#Money
var money:int = 0

#Upgrades
var earnMultiplier = 1

@onready var moneyIndicator = $UIContainer/Notch/Money
@onready var mouseCldnIndicator = $UIContainer/CooldownIndicator
@onready var spawnUpCldnIndicator = $UIContainer/Store/SpawnUpCldnIndicator
@onready var cooldown = $Cooldown
@onready var ads = $Ads
@onready var spawnTimer = $SpawnRate
@onready var SpawnUpCldn = $SpawnUpgradeCooldown
@onready var checkWin = $CheckWin
@onready var clickers = $Clickers

func _ready():
	SpawnAd(randi_range(5,10))
	spawnTimer.start()
	
func _process(delta):	
	if isWin() and ads != null:
		ads.queue_free()
	updateCooldown()
	updateSpawnCooldown()

func SpawnAd(count):
	for i in range(count):
		if ads == null:
			return
		if isMax():
			return
		var ad = adScene.instantiate()
		ad.texture = bannerSprites.pick_random()
		var max_x = get_viewport_rect().size.x - ad.size.x -30
		var max_y = get_viewport_rect().size.y - ad.size.y - 30
		ad.position.x = randi_range(30,max_x)
		ad.position.y = randi_range(90,max_y)
		ads.add_child(ad)
	Audio.windowSound.play()

func startCooldown():
	cooldown.start()
	
func isCooldown():
	return cooldown.is_stopped()

func isWin():
	if ads != null:
		return money >= winCost or ads.get_child_count() == 0
	
func isLose():
	if ads != null:
		return ads.get_child_count() > loseCount
	
func isMax():
	if ads != null:
		return ads.get_child_count() > loseCount

func upgradeSpawnRate(modifier):
	spawnTimer.wait_time *= modifier
	SpawnUpCldn.start()
	
func upgradeCooldown(modifier):
	cooldown.wait_time /= modifier
	
func upgradeAutoclicker(modifier):
	for i in clickers.get_children():
		i.wait_time /= modifier
	
func addClicker():
	var clicker = clickerScene.instantiate()
	clicker.timeout.connect(_on_autoclicker_time_timeout)
	clickers.add_child(clicker)

func updateMoney():
	moneyIndicator.text = "$"+str(money)
		
func increaseDifficulty():
	var option = randi_range(0,1)
	if option == 0:	
		spawnTimer.wait_time /= difficultyRate
	else:
		maxSpawns += difficultyCount
		
func updateCooldown():
	mouseCldnIndicator.value = (cooldown.time_left / cooldown.wait_time) * 100
		
func updateSpawnCooldown():
	spawnUpCldnIndicator.value = (SpawnUpCldn.time_left / SpawnUpCldn.wait_time) * 100

func isSpawnCooldown():
	return not SpawnUpCldn.is_stopped()
		
func _on_timer_timeout() -> void:
	if ads != null:
		SpawnAd(randi_range(minSpawns,int(maxSpawns)))
		if (randi_range(0,2)):
			increaseDifficulty()

func _on_autoclicker_time_timeout() -> void:
	if ads != null:
		ads.get_children().pick_random().close()
		money += earnMultiplier * (killMoney / 2)
		updateMoney()

func _on_check_win_timeout() -> void:
	if ads == null:
		checkWin.stop()
		Fade.fade()
		await Fade.has_faded
		get_tree().change_scene_to_file("res://win.tscn")
		queue_free()
	elif isLose():
		checkWin.stop()
		Audio.stopPlayMusic()
		Audio.playStoryMusic()
		Fade.fade()
		await Fade.has_faded
		get_tree().change_scene_to_file("res://lose.tscn")
		queue_free()
