extends Control

var score = 0
var clickAdd = 1
var scorePerSec = 1
var scoreMultiplier = 1

#Click Upgrades Costs
var clickUpgradeReq1 = 10
var clickUpgradeReq10 = 100
var clickUpgradeReq20 = 1000
var clickUpgradeReq40 = 10000
var clickUpgradeReq60 = 100000
var clickUpgradeReq80 = 1000000
var clickUpgradeReq100 = 10000000
var clickUpgradeReq200 = 100000000
var clickUpgradeReq400 = 1000000000


# Click Upgrades Amount
var clickUpgradeAmt1 = 1
var clickUpgradeAmt10 = 10
var clickUpgradeAmt20 = 20
var clickUpgradeAmt40 = 40
var clickUpgradeAmt60 = 60
var clickUpgradeAmt80 = 80
var clickUpgradeAmt100 = 100
var clickUpgradeAmt200 = 200
var clickUpgradeAmt400 = 400


# Passive Upgrades Costs
var passiveUpgradeReq2 = 100
var passiveUpgradeReq10 = 1000
var passiveUpgradeReq20 = 10000
var passiveUpgradeReq50 = 100000
var passiveUpgradeReq100 = 1000000
var passiveUpgradeReq200 = 10000000
var passiveUpgradeReq500 = 100000000
var passiveUpgradeReq1000 = 1000000000
var passiveUpgradeReq10000 = 10000000000

# Passive Upgrades Amount
var passiveUpgradeAmt2 = 2
var passiveUpgradeAmt10 = 10
var passiveUpgradeAmt20 = 20
var passiveUpgradeAmt50 = 50
var passiveUpgradeAmt100 = 100
var passiveUpgradeAmt200 = 200
var passiveUpgradeAmt500 = 500
var passiveUpgradeAmt1000 = 1000
var passiveUpgradeAmt10000 = 10000



# Called when the node enters the scene tree for the first time.
func _ready():
	$ClickUpgrades.get_popup().connect("id_pressed",clickUpgrade)
	$PassiveUpgrades.get_popup().connect("id_pressed",passiveUpgrade)
	$Timer.connect("timeout", _on_Timer_timeout)
	#Code for the Click Me Button (deprecated)
	#$Click.pressed.connect(self._on_button_pressed)

#Prints all input events, only for debugging
#func _input(event):
	#print(event.as_text())

func _on_Timer_timeout():
		score += scorePerSec * scoreMultiplier

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	#Displays normal numbers until commas are needed
	if score > 999:
		$Score.text = str(scoreFormat(score))
	else: 
		$Score.text = str(score) #Changes the score number

#This checks any input that comes in
func _input(event):
	#Function to record every left click as a "Click" to add to the score
	if event.is_action_pressed("left_mouse"):
		_on_button_pressed()

#Formats the score string so commas are inserted
func scoreFormat(score):
	# Convert value to string.
	var str_value: String = str(score)

	# Loop backward starting at the last 3 digits,
	# add comma then, repeat every 3rd step.
	for i in range(str_value.length()-3, 0, -3):
		str_value = str_value.insert(i, ",")
	score = str_value
	return score

func _on_button_pressed():
	score += clickAdd * scoreMultiplier

func clickUpgrade(id):
	match id:
		0:
			if score >= clickUpgradeReq1:
				print ("Score sufficient")
				score -=clickUpgradeReq1
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt1
			else:
				print("not enough score")
		1:
			if score >= clickUpgradeReq10:
				print ("Score sufficient")
				score -=clickUpgradeReq10
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt10
			else:
				print("not enough score")
		2:
			if score >= clickUpgradeReq20:
				print ("Score sufficient")
				score -=clickUpgradeReq20
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt20
			else:
				print("not enough score")
		3:
			if score >= clickUpgradeReq40:
				print ("Score sufficient")
				score -=clickUpgradeReq40
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt40
			else:
				print("not enough score")
		4:
			if score >= clickUpgradeReq60:
				print ("Score sufficient")
				score -=clickUpgradeReq60
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt60
			else:
				print("not enough score")
		5:
			if score >= clickUpgradeReq80:
				print ("Score sufficient")
				score -=clickUpgradeReq80
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt80
			else:
				print("not enough score")
		6:
			if score >= clickUpgradeReq100:
				print ("Score sufficient")
				score -=clickUpgradeReq100
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt100
			else:
				print("not enough score")
		7:
			if score >= clickUpgradeReq200:
				print ("Score sufficient")
				score -=clickUpgradeReq200
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt200
			else:
				print("not enough score")
		8:
			if score >= clickUpgradeReq400:
				print ("Score sufficient")
				score -=clickUpgradeReq400
				$ClickUpgrades.get_popup().set_item_disabled(id, true)
				clickAdd += clickUpgradeAmt400
			else:
				print("not enough score")

func passiveUpgrade(id):
	match id:
		0:
			if score >= passiveUpgradeReq2:
				print ("Score sufficient")
				score -=passiveUpgradeReq2
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt2
			else:
				print("not enough score")
		1:
			if score >= passiveUpgradeReq10:
				print ("Score sufficient")
				score -=passiveUpgradeReq10
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt10
			else:
				print("not enough score")
		2:
			if score >= passiveUpgradeReq20:
				print ("Score sufficient")
				score -=passiveUpgradeReq20
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt20
			else:
				print("not enough score")
		3:
			if score >= passiveUpgradeReq50:
				print ("Score sufficient")
				score -=passiveUpgradeReq50
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt50
			else:
				print("not enough score")
		4:
			if score >= passiveUpgradeReq100:
				print ("Score sufficient")
				score -=passiveUpgradeReq100
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt100
			else:
				print("not enough score")
		5:
			if score >= passiveUpgradeReq200:
				print ("Score sufficient")
				score -=passiveUpgradeReq200
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt200
			else:
				print("not enough score")
		6:
			if score >= passiveUpgradeReq500:
				print ("Score sufficient")
				score -=passiveUpgradeReq500
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt500
			else:
				print("not enough score")
		7:
			if score >= passiveUpgradeReq1000:
				print ("Score sufficient")
				score -=passiveUpgradeReq1000
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt1000
			else:
				print("not enough score")
		8:
			if score >= passiveUpgradeReq10000:
				print ("Score sufficient")
				score -=passiveUpgradeReq10000
				$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				scorePerSec = passiveUpgradeAmt10000
			else:
				print("not enough score")
