extends Spatial

onready var player_green = get_tree().get_root().find_node("PlayerGreen", true, false)
onready var player_blue = get_tree().get_root().find_node("PlayerBlue", true, false)

onready var goal_green = get_tree().get_root().find_node("GoalGreen", true, false)
onready var goal_blue = get_tree().get_root().find_node("GoalBlue", true, false)


var active_player = ""
var box_buttons_colored = null
var box_buttons_green = null
var box_buttons_blue = null
var has_bb_green = false
var has_bb_blue = false
var all_bb_green_triggered = false
var all_bb_blue_triggered = false


func _ready():
	player_green.set_state(player_green.STATE.ACTIVE)
	active_player = "green"
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
	get_box_buttons()
	if (has_bb_green):
		change_goal_status("green", all_bb_green_triggered)
	if (has_bb_blue):
		change_goal_status("blue", all_bb_blue_triggered)
	#if (has_bb_green):
	#	print ("has green bb")
	#if (has_bb_blue):
	#	print ("has blue bb")

func _process(delta):
	
	if player_green.current_state == player_green.STATE.FINISHED and player_blue.current_state == player_blue.STATE.FINISHED:
		get_tree().change_scene("res://UI/LevelDoneScreen.tscn")
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	
	elif player_green.current_state == player_green.STATE.FINISHED and player_blue.current_state != player_blue.STATE.IN_FINISH_AREA:
		player_blue.set_state(player_blue.STATE.ACTIVE)
	elif player_blue.current_state == player_blue.STATE.FINISHED and player_green.current_state != player_green.STATE.IN_FINISH_AREA:
			player_green.set_state(player_green.STATE.ACTIVE)
	
	if Input.is_action_just_pressed("switch_player"):
		
		if !(player_green.current_state == player_green.STATE.FINISHED or player_blue.current_state == player_blue.STATE.FINISHED):
		
			if active_player == "green":
				active_player = "blue"
				player_green.set_state(player_green.STATE.INACTIVE)
				player_blue.set_state(player_blue.STATE.ACTIVE)

			elif active_player == "blue":
				active_player = "green"
				player_green.set_state(player_green.STATE.ACTIVE)
				player_blue.set_state(player_blue.STATE.INACTIVE)
		elif player_green.current_state == player_green.STATE.FINISHED and player_blue.current_state == player_blue.STATE.FINISHED:
			pass
		elif player_green.current_state == player_green.STATE.FINISHED:
			player_blue.set_state(player_blue.STATE.ACTIVE)
		elif player_blue.current_state == player_blue.STATE.FINISHED:
			player_green.set_state(player_green.STATE.ACTIVE)
		else:
			print ("what the heck happened here???")
				
	if Input.is_action_just_pressed("restart"):
		get_tree().reload_current_scene()

func get_box_buttons():
	box_buttons_colored = get_tree().get_nodes_in_group("group_box_button")
	box_buttons_green = get_tree().get_nodes_in_group("group_box_button_green")
	box_buttons_blue = get_tree().get_nodes_in_group("group_box_button_blue")
	has_bb_green = !box_buttons_green.empty()
	has_bb_blue = !box_buttons_blue.empty()
	for box in box_buttons_colored:
		box.connect("box_button_status_changed", self, "_on_box_button_changed")
	
func _on_box_button_changed():
	all_bb_green_triggered = true
	all_bb_blue_triggered = true
	
	for box_green in box_buttons_green:
		if !box_green.triggered:
			all_bb_green_triggered = false
	for box_blue in box_buttons_blue:
		if !box_blue.triggered:
			all_bb_blue_triggered = false
			
	#print ("all green triggered?: " + str(all_bb_green_triggered))
	#print ("all blue triggered?: " + str(all_bb_blue_triggered))
	
	change_goal_status("green", all_bb_green_triggered)
	change_goal_status("blue", all_bb_blue_triggered)

#FIXME: check if status changed
func change_goal_status(color, status):
	#print ("change goal status: " + str(color) + " = " + str(status))
	if (color == "green"):
		goal_green.enable_goal(status)
	if (color == "blue"):
		goal_blue.enable_goal(status)