extends Control

var currentPlayer : int
var playerName : Label
var one		   : OptionButton
var two		   : OptionButton
var three	   : OptionButton
var four	   : OptionButton
var five	   : OptionButton
var six		   : OptionButton
var three_of_a_kind	   : OptionButton
var four_of_a_kind	   : OptionButton
var full_house	   : OptionButton
var small_straight	   : OptionButton
var large_straight	   : OptionButton
var yahtzee	   : Button
var chance	   : OptionButton
var bonus	   : Label
var score 	   : Label

# Called when the node enters the scene tree for the first time.
func _ready():
	currentPlayer = 0
	playerName = get_node("ScrollContainer/VBoxContainer/Panel/HBoxContainer/Label")
	var players = Player_Manger.get_players()
	if players.size() > 0:
		playerName.text = players[currentPlayer].name
	one = get_node("ScrollContainer/VBoxContainer/One/HBoxContainer/OptionButton")
	two = get_node("ScrollContainer/VBoxContainer/Two/HBoxContainer/OptionButton")
	three = get_node("ScrollContainer/VBoxContainer/Three/HBoxContainer/OptionButton")
	four = get_node("ScrollContainer/VBoxContainer/Four/HBoxContainer/OptionButton")
	five = get_node("ScrollContainer/VBoxContainer/Five/HBoxContainer/OptionButton")
	six = get_node("ScrollContainer/VBoxContainer/Six/HBoxContainer/OptionButton")
	three_of_a_kind = get_node("ScrollContainer/VBoxContainer/Three_of_a_kind/HBoxContainer/OptionButton")
	add_items(three_of_a_kind)
	four_of_a_kind = get_node("ScrollContainer/VBoxContainer/Four_of_a_Kind/HBoxContainer/OptionButton")
	add_items(four_of_a_kind)
	full_house = get_node("ScrollContainer/VBoxContainer/Full_House/HBoxContainer/OptionButton")
	full_house.add_item("")
	full_house.add_item("-")
	full_house.add_item("25")
	small_straight = get_node("ScrollContainer/VBoxContainer/Small_Straight/HBoxContainer/OptionButton")
	add_items_to_small_straight(small_straight)
	large_straight = get_node("ScrollContainer/VBoxContainer/Large_Straight/HBoxContainer/OptionButton")
	add_items_to_large_straight(large_straight)
	yahtzee = get_node("ScrollContainer/VBoxContainer/yahtzee/HBoxContainer/ButtonYahtzee")
	bonus = get_node("ScrollContainer/VBoxContainer/Bonus/HBoxContainer/LabelScore")
	chance = get_node("ScrollContainer/VBoxContainer/chance/HBoxContainer/OptionButton")
	add_items(chance)
	score = get_node("ScrollContainer/VBoxContainer/Score/HBoxContainer/LabelScore")
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	calc_bonus()
	calc_score()
	pass

func _on_button_right_pressed():
	save()
	var players = Player_Manger.get_players()
	currentPlayer += 1
	if currentPlayer >= players.size():
		currentPlayer = 0
	playerName.text = players[currentPlayer].name
	load_stats()
	pass # Replace with function body.
	
func calc_bonus():
	var players = Player_Manger.get_players()
	var total = 0
	total += int(int(one.get_item_text(one.get_selected_id())) + 
				 int(two.get_item_text(two.get_selected_id())) +
				 int(three.get_item_text(three.get_selected_id())) +
				 int(four.get_item_text(four.get_selected_id())) +
				 int(five.get_item_text(five.get_selected_id())) +
				 int(six.get_item_text(six.get_selected_id())))
	if total >= 63:
		players[currentPlayer].bonus = 35
		bonus.text = "35"
	else:
		players[currentPlayer].bonus = 0
		bonus.text = "0"
	
func calc_score():
	var players = Player_Manger.get_players()
	players[currentPlayer].score = (int(one.get_item_text(one.get_selected_id())) + 
								   int(two.get_item_text(two.get_selected_id())) +
								   int(three.get_item_text(three.get_selected_id())) +
								   int(four.get_item_text(four.get_selected_id())) +
								   int(five.get_item_text(five.get_selected_id())) +
								   int(six.get_item_text(six.get_selected_id())) +
								   int(three_of_a_kind.get_item_text(three_of_a_kind.get_selected_id())) +
								   int(four_of_a_kind.get_item_text(four_of_a_kind.get_selected_id())) +
								   int(full_house.get_item_text(full_house.get_selected_id())) +
								   int(small_straight.get_item_text(small_straight.get_selected_id())) +
								   int(large_straight.get_item_text(large_straight.get_selected_id())) +
								   int(yahtzee.text) +
								   int(chance.get_item_text(chance.get_selected_id())) +
								   int(bonus.text))
	score.text = str(players[currentPlayer].score)
	
func load_stats():
	var players = Player_Manger.get_players()
	one.select(players[currentPlayer].one)
	two.select(players[currentPlayer].two)
	three.select(players[currentPlayer].three)
	four.select(players[currentPlayer].four)
	five.select(players[currentPlayer].five)
	six.select(players[currentPlayer].six)
	three_of_a_kind.select(players[currentPlayer].three_of_a_kind)
	four_of_a_kind.select(players[currentPlayer].four_of_a_kind)
	full_house.select(players[currentPlayer].full_house)
	small_straight.select(players[currentPlayer].small_straight)
	large_straight.select(players[currentPlayer].large_straight)
	yahtzee.text = str(players[currentPlayer].yahtzee)
	chance.select(players[currentPlayer].chance)
	bonus.text = str(players[currentPlayer].bonus)
	pass

func save():
	var players = Player_Manger.get_players()
	players[currentPlayer].one = one.get_selected_id()
	players[currentPlayer].two = two.get_selected_id()
	players[currentPlayer].three = three.get_selected_id()
	players[currentPlayer].four = four.get_selected_id()
	players[currentPlayer].five = five.get_selected_id()
	players[currentPlayer].six = six.get_selected_id()
	players[currentPlayer].three_of_a_kind = int(three_of_a_kind.get_selected_id())
	players[currentPlayer].four_of_a_kind = int(four_of_a_kind.get_selected_id())
	players[currentPlayer].full_house = int(full_house.get_selected_id())
	players[currentPlayer].small_straight = int(small_straight.get_selected_id())
	players[currentPlayer].large_straight = int(large_straight.get_selected_id())
	players[currentPlayer].yahtzee = int(yahtzee.text)
	players[currentPlayer].bonus = int(bonus.text)
	players[currentPlayer].chance = int(chance.get_selected_id())
	players[currentPlayer].score = players[currentPlayer].one + players[currentPlayer].two + players[currentPlayer].three + players[currentPlayer].four + players[currentPlayer].five + players[currentPlayer].six + players[currentPlayer].three_of_a_kind + players[currentPlayer].four_of_a_kind + players[currentPlayer].full_house + players[currentPlayer].small_straight + players[currentPlayer].large_straight + players[currentPlayer].yahtzee + players[currentPlayer].chance + players[currentPlayer].bonus
	pass

func _on_button_left_pressed():
	save()
	var players = Player_Manger.get_players()
	currentPlayer -= 1
	if currentPlayer < 0:
		currentPlayer = players.size() - 1
	playerName.text = players[currentPlayer].name
	load_stats()
	pass # Replace with function body.
	
func add_items_to_small_straight(oButton: OptionButton):
	oButton.add_item("")
	oButton.add_item("-")
	oButton.add_item("30")
	pass # Replace with function body.
	
func add_items_to_large_straight(oButton: OptionButton):
	oButton.add_item("")
	oButton.add_item("-")
	oButton.add_item("40")
	pass # Replace with function body.
func add_items(oButton: OptionButton):
	oButton.add_item("")
	oButton.add_item("-")
	oButton.add_item("0")
	oButton.add_item("1")
	oButton.add_item("2")
	oButton.add_item("3")
	oButton.add_item("4")
	oButton.add_item("5")
	oButton.add_item("6")
	oButton.add_item("7")
	oButton.add_item("8")
	oButton.add_item("9")
	oButton.add_item("10")
	oButton.add_item("11")
	oButton.add_item("12")
	oButton.add_item("13")
	oButton.add_item("14")
	oButton.add_item("15")
	oButton.add_item("16")
	oButton.add_item("17")
	oButton.add_item("18")
	oButton.add_item("19")
	oButton.add_item("20")
	oButton.add_item("21")
	oButton.add_item("22")
	oButton.add_item("23")
	oButton.add_item("24")
	oButton.add_item("25")
	oButton.add_item("26")
	oButton.add_item("27")
	oButton.add_item("28")
	oButton.add_item("29")
	oButton.add_item("30")
	oButton.add_item("31")
	oButton.add_item("32")
	oButton.add_item("33")
	oButton.add_item("34")
	oButton.add_item("35")
	oButton.add_item("36")
	oButton.add_item("37")
	oButton.add_item("38")
	oButton.add_item("39")
	oButton.add_item("40")
	oButton.add_item("41")
	oButton.add_item("42")
	oButton.add_item("43")
	oButton.add_item("44")
	oButton.add_item("45")
	oButton.add_item("46")
	oButton.add_item("47")
	oButton.add_item("48")
	oButton.add_item("49")
	oButton.add_item("50")
	oButton.add_item("51")
	oButton.add_item("52")
	oButton.add_item("53")
	oButton.add_item("54")
	oButton.add_item("55")
	oButton.add_item("56")
	oButton.add_item("57")
	oButton.add_item("58")
	oButton.add_item("59")
	oButton.add_item("60")
	oButton.add_item("61")
	oButton.add_item("62")
	oButton.add_item("63")
	oButton.add_item("64")
	oButton.add_item("65")
	oButton.add_item("66")
	oButton.add_item("67")
	oButton.add_item("68")
	oButton.add_item("69")
	oButton.add_item("70")
	pass # Replace with function body.


func _on_button_yahtzee_pressed():
	Player_Manger.get_players()[currentPlayer].yahtzee += 50
	yahtzee.text = str(Player_Manger.get_players()[currentPlayer].yahtzee)
	pass # Replace with function body.


func _on_button_yahtzee_minus_pressed():
	Player_Manger.get_players()[currentPlayer].yahtzee -= 50
	yahtzee.text = str(Player_Manger.get_players()[currentPlayer].yahtzee)
	pass # Replace with function body.


func _on_button_end_pressed():
	get_tree().change_scene_to_file("res://MainMenu.tscn")
	pass # Replace with function body.
