extends CanvasLayer

var cldnlvl = 1
var cashlvl = 1
var clickerlvl = 0
var clickerspdlvl = 1

var spawnMax: int = 2

var spawnCount:int = spawnMax

var cldnCost:int = 100

var moreCashCost:int = 700

var clickerSpdCost:int = 750

var clickerCountCost:int = 1000

const spawnMulti = 2

const cldnMulti = 1.8

const cashMulti = 2

const clickerCountMulti = 1.8

const clickerMulti = 1.6

var spawnModifier = 1.5
var cldnModifier = 1.6
var clickerModifier = 1.4
var earnModifier = 1.4

@onready var base = get_parent().get_parent()
@onready var spawnUpCldnIndicator = $SpawnUpCldnIndicator

@onready var spawnUp = $SpnButton
@onready var spawnUpText = $SpnButton/Upgrade
@onready var spawnUpCost = $SpnButton/Cost

@onready var cldnUp = $CldnButton
@onready var cldnUpText = $CldnButton/Upgrade
@onready var cldnLevel = $CldnButton/Level
@onready var cldnUpCost = $CldnButton/Cost

@onready var cashUp = $CashButton
@onready var cashUpText = $CashButton/Upgrade
@onready var cashLevel = $CashButton/Level
@onready var cashCost = $CashButton/Cost

@onready var clickercountUp = $AutoButton
@onready var clickercountUpText = $AutoButton/Upgrade
@onready var clickercountLevel = $AutoButton/Level
@onready var clickerCountUpCost = $AutoButton/Cost

@onready var clickerSpdUp = $AutoSpeedButton
@onready var clickerUpText = $AutoSpeedButton/Upgrade
@onready var clickerLevel = $AutoSpeedButton/Level
@onready var clickerCost = $AutoSpeedButton/Cost
	
func _process(delta):
	spawnUpText.text = "Network sabotage"
	spawnUpCost.text = str(spawnCount) + "/" + str(spawnMax)
	
	cldnUpText.text = "Mouse overclock"
	cldnUpCost.text = str(cldnCost)
	cldnLevel.text = str(cldnlvl)
	
	cashUpText.text = "More $$$"
	cashCost.text = str(moreCashCost)
	cashLevel.text = str(cashlvl)
	
	clickercountUpText.text = "Autoclicker(s)"
	clickerCountUpCost.text = str(clickerCountCost)
	clickercountLevel.text = str(clickerlvl)
	
	clickerUpText.text = "Autoclicker speed"
	clickerCost.text = str(clickerSpdCost)
	clickerLevel.text = str(clickerspdlvl)
	
	spawnUp.disabled = spawnCount < 1 or base.isSpawnCooldown()
	cldnUp.disabled = base.money < cldnCost
	cashUp.disabled = base.money < moreCashCost
	clickercountUp.disabled = base.money < clickerCountCost
	clickerSpdUp.disabled = clickerlvl <= 0 or base.money < clickerSpdCost
	
func _input(event):
	if event.is_action_pressed("esc"):
		if visible:
			hide()
			get_tree().paused = false
		elif not Fade.isAnimating():
			get_tree().paused = true
			show()

func _on_spn_button_pressed() -> void:
	Audio.upgradeSound.play()
	spawnCount -= 1
	base.upgradeSpawnRate(spawnModifier)
	spawnUpCldnIndicator.value = 100

func _on_cldn_button_pressed() -> void:
	Audio.upgradeSound.play()
	base.money -= cldnCost
	cldnCost *= cldnMulti
	base.upgradeCooldown(cldnModifier)
	base.updateMoney()
	cldnlvl += 1

func _on_cash_button_pressed() -> void:
	Audio.upgradeSound.play()
	base.money -= moreCashCost
	moreCashCost *= cashMulti
	base.earnMultiplier *= earnModifier
	base.updateMoney()
	cashlvl += 1

func _on_auto_button_pressed() -> void:
	Audio.upgradeSound.play()
	base.money -= clickerCountCost
	clickerCountCost *= clickerCountMulti
	base.addClicker()
	base.updateMoney()
	clickerlvl += 1


func _on_auto_speed_button_pressed() -> void:
	Audio.upgradeSound.play()
	base.money -= clickerSpdCost
	clickerSpdCost *= clickerMulti
	base.upgradeAutoclicker(clickerModifier)
	base.updateMoney()
	clickerspdlvl+= 1

func _on_menu_pressed() -> void:
	Audio.buttonClick.play()
	get_tree().paused = false
	Audio.stopPlayMusic()
	Fade.fade()
	await Fade.has_faded
	get_tree().change_scene_to_file("res://home.tscn")
	base.queue_free()
