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)
	

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

func _on_Timer_timeout():
		Global.globalDamage += Global.globalDamagePerSec * Global.globalDamageMultiplier

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


#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"):
		leftClick()
		#_on_button_pressed()


#Formats the score string so commas are inserted
func scoreFormat(score):
	# Convert value to string.
	var str_value: String = str(Global.globalDamage)
	
	# 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 leftClick():
	Global.globalDamage += Global.globalDamagePerClick * Global.globalDamageMultiplier


#func clickUpgrade(id):
	#match id:
		#0:
			#if Global.globalDamage >= clickUpgradeReq1:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq1
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt1
			#else:
				#print("not enough score")
		#1:
			#if Global.globalDamage >= clickUpgradeReq10:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq10
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt10
			#else:
				#print("not enough score")
		#2:
			#if Global.globalDamage >= clickUpgradeReq20:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq20
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt20
			#else:
				#print("not enough score")
		#3:
			#if Global.globalDamage >= clickUpgradeReq40:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq40
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt40
			#else:
				#print("not enough score")
		#4:
			#if Global.globalDamage >= clickUpgradeReq60:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq60
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt60
			#else:
				#print("not enough score")
		#5:
			#if Global.globalDamage >= clickUpgradeReq80:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq80
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt80
			#else:
				#print("not enough score")
		#6:
			#if Global.globalDamage >= clickUpgradeReq100:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq100
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt100
			#else:
				#print("not enough score")
		#7:
			#if Global.globalDamage >= clickUpgradeReq200:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq200
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt200
			#else:
				#print("not enough score")
		#8:
			#if Global.globalDamage >= clickUpgradeReq400:
				#print ("Score sufficient")
				#Global.globalDamage -=clickUpgradeReq400
				#$ClickUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerClick += clickUpgradeAmt400
			#else:
				#print("not enough score")
#
#func passiveUpgrade(id):
	#match id:
		#0:
			#if Global.globalDamage >= passiveUpgradeReq2:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq2
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt2
			#else:
				#print("not enough score")
		#1:
			#if Global.globalDamage >= passiveUpgradeReq10:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq10
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt10
			#else:
				#print("not enough score")
		#2:
			#if Global.globalDamage >= passiveUpgradeReq20:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq20
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt20
			#else:
				#print("not enough score")
		#3:
			#if Global.globalDamage >= passiveUpgradeReq50:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq50
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt50
			#else:
				#print("not enough score")
		#4:
			#if Global.globalDamage >= passiveUpgradeReq100:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq100
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt100
			#else:
				#print("not enough score")
		#5:
			#if Global.globalDamage >= passiveUpgradeReq200:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq200
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt200
			#else:
				#print("not enough score")
		#6:
			#if Global.globalDamage >= passiveUpgradeReq500:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq500
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt500
			#else:
				#print("not enough score")
		#7:
			#if Global.globalDamage >= passiveUpgradeReq1000:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq1000
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt1000
			#else:
				#print("not enough score")
		#8:
			#if Global.globalDamage >= passiveUpgradeReq10000:
				#print ("Score sufficient")
				#Global.globalDamage -=passiveUpgradeReq10000
				#$PassiveUpgrades.get_popup().set_item_disabled(id, true)
				#Global.globalDamagePerSec = passiveUpgradeAmt10000
			#else:
				#print("not enough score")
