extends Control

# list of "random" words to choose from. I made them vaguely government related
var word1 = ["Great", "Amazing", "Robust", "Cool", "100 years of", "21st Century", "Emergency Service"]
var word2 = ["Public Defense", "Gerrymanerding", "Welfare", "Affordable Care", "War", "Shutdown", "Protection", "Union", "Anti-Union", "Anti-Trust", "Disposal"]
var endingWord = ["Bill", "Proposal", "Budget", "Act", "Precedent"]

var voteOptions={
	"yay":0,
	"nay":1, 
	"abstain":2
}
var activeBill = {
	"title": "",
	"bribes": [[], [], []]	# 0 for yes, 1 for no, 2 for abstain. should be a 2d array for total bribes per
}
var nextBill = {
	"title": "",
	"bribes": [[], [], []]	# 0 for yes, 1 for no, 2 for abstain. should be a 2d array for total bribes per
}
var choice = null
@onready var rand = RandomNumberGenerator.new()

func makeBill():
	var bill = ""
	bill += word1[rand.randi_range(0, word1.size()-1)] if rand.randi_range(0, 10) != 4 else ""
	bill += " "
	bill += word2[rand.randi_range(0, word2.size()-1)]
	bill += " "
	bill += endingWord[rand.randi_range(0, endingWord.size()-1)]
	if bill.substr(bill.length() - 1, 1) == ' ':
		bill = bill.left(bill.length() - 1)
	return bill

func addBribe(vote, amt):
	nextBill["bribes"][vote].append(amt)
	pass

func updateScene():
	$HBoxContainer/VBoxContainer/BillTitle.text = "The " + activeBill["title"]
	pass

func _on_yay_btn_pressed() -> void:
	choice = "yay"
	pushPastVote(activeBill, "yay")
	checkForBribes(activeBill, "yay")
	changeVoteBtnsStatus(true)
	pass

func _on_nay_btn_pressed() -> void:
	choice = "nay"
	pushPastVote(activeBill, "nay")
	checkForBribes(activeBill, "nay")
	changeVoteBtnsStatus(true)
	pass

func _on_abstain_pressed() -> void:
	choice = "abstain"
	pushPastVote(activeBill, "abstain")
	checkForBribes(activeBill, "abstain")
	changeVoteBtnsStatus(true)
	pass

func checkForBribes(bill, vote):
	var bribeList = bill["bribes"]
	if bribeList.size() != 0 and bribeList[voteOptions[vote]].size() != 0:
		for eachBribe in bribeList[voteOptions[vote]]:
			get_parent().get_node("PaymentPortal").addTransaction("For " + "voting yay " if voteOptions[vote] == 0 else "voting nay " if voteOptions[vote] == 1 else "abstaining " + "on " + bill["title"], eachBribe)
	pass

func pushPastVote(bill, vote):
	var lbl = Label.new()
	lbl.text = bill["title"] + ", " + vote
	lbl.add_theme_color_override("font_color", Color.BLACK)
	lbl.name = bill["title"]
	
	$HBoxContainer/VBoxContainer2/ScrollContainer/VBoxContainer.add_child(lbl)
	$HBoxContainer/VBoxContainer2/ScrollContainer/VBoxContainer.move_child(lbl, 0)
	pass
	
# send true to deactivate btns send false to activate them
func changeVoteBtnsStatus(value):
	for eachBtn in $HBoxContainer/VBoxContainer/HBoxContainer.get_children():
		eachBtn.disabled = value
	pass
	
func pushPreMadeVote(line):
	var lbl = Label.new()
	lbl.text = line
	lbl.add_theme_color_override("font_color", "black")
	
	$HBoxContainer/VBoxContainer2/ScrollContainer/VBoxContainer.add_child(lbl)
	pass

func startOfDay():
	activeBill["title"] = nextBill["title"]
	activeBill["bribes"] = nextBill["bribes"]
	
	# reset and make next bill
	nextBill["bribes"] = [[], [], []]
	nextBill["title"] = makeBill()
	
	choice = null
	changeVoteBtnsStatus(false)
	pass

func endOfDay():
	if choice == "" or choice == null:
		pushPastVote(activeBill, "abstain")
		activeBill["bribes"] = [[], [], []]
		activeBill["title"] = ""
	pass
	
func save():
	var pastVotes = []
	var holder = {
		"pastVotes": [],
		"nextBill": ""
	}
	for eachLabel in $HBoxContainer/VBoxContainer2/ScrollContainer/VBoxContainer.get_children():
		var data = eachLabel.text
		pastVotes.push_back(data)
		
	holder["pastVotes"] = pastVotes
	holder["nextBill"] = nextBill
	return holder

func loadData(totalPiece):
	if totalPiece != null:
		var dataPiece = totalPiece["VotingPortal"]
		if dataPiece["pastVotes"] != null : #and $HBoxContainer/VBoxContainer2/ScrollContainer/VBoxContainer.get_children() == null
			var pastVotes = dataPiece["pastVotes"]
			for eachLine in pastVotes:
				pushPreMadeVote(eachLine)
		activeBill = totalPiece["VotingPortal"]["nextBill"]
	else:
		activeBill["title"] = makeBill()
	pass

func _on_back_btn_pressed() -> void:
	var root = get_parent().get_node("Desktop")
	root.visible = true
	get_node(".").visible = false
	pass
