extends Node

var cooldown:int = 0
static var SCREAM_ARRAY:Array = [0,1]

func _explode():
	%SCREAM.stream = load("res://assets/sounds/scream_%s.wav" % SCREAM_ARRAY.pick_random())
	%SCREAM.play()
	%RULE_ENFORCEMENT.explode()
	%QUESTION.visible = false
	%ANSWER.visible = false
	
	cooldown = 180
	if GLOBALVARS.selected_player == 0:
		%WINLOSE.visible = true
		%WINLOSE.self_modulate = Color(1,0,0)
		%WINLOSE.text = ":("
	elif GLOBALVARS.players_remaining == [0]:
		%WINLOSE.visible = true
		%WINLOSE.self_modulate = Color(0,1,0)
		%WINLOSE.text = "You Win! :)"
	else:
		cooldown = 60

func _determine_seconds_left() -> float:
	var difficulty_calc:float = 20-float(GLOBALVARS.difficulty)
	difficulty_calc = difficulty_calc if difficulty_calc >= 3.0 else 3.0
	GLOBALVARS.start_seconds = difficulty_calc
	return difficulty_calc

func _on_answer_text_submitted(_new_text: String) -> void:
	if GLOBALVARS.current_solution == (%ANSWER.text).to_int() and (%ANSWER.text).is_valid_int():
		GLOBALVARS.selected_player = (GLOBALVARS.selected_player+1)%4
		while GLOBALVARS.selected_player not in GLOBALVARS.players_remaining:
			GLOBALVARS.selected_player = (GLOBALVARS.selected_player+1)%4
			
		GLOBALVARS.difficulty+=.5
		GLOBALVARS.seconds_left = _determine_seconds_left()
		_generate_question()
		
	else:
		GLOBALVARS.seconds_left = _determine_seconds_left()
		GLOBALVARS.rotation_paused = true
		GLOBALVARS.players_remaining.erase(GLOBALVARS.selected_player)
		get_node("players/player_%s" % GLOBALVARS.selected_player).queue_free()
		_explode()
	
	%ANSWER.text = ""
	
func _generate_question() -> void:
	# 0 - add
	# 1 - subtract
	# 2 - multiply
	var rand_int:int = randi_range(0,2)
	
	var x:int
	var y:int
	
	if rand_int == 0:
		x = randi_range(1,50)
		y = randi_range(1,20)
		GLOBALVARS.current_solution = x+y
		GLOBALVARS.current_operand = "+"
	elif rand_int == 1:
		x = randi_range(1,50)
		y = randi_range(1,20)
		GLOBALVARS.current_solution = x-y
		GLOBALVARS.current_operand = "-"
	elif rand_int == 2:
		x = randi_range(0,12) # simplify multiplication
		y = randi_range(0,12)
		GLOBALVARS.current_solution = x*y
		GLOBALVARS.current_operand = "*"
		
	%QUESTION.text = "%s %s %s = " % [x,GLOBALVARS.current_operand,y]
	%QUESTION.visible = true
	%ANSWER.visible = true

func _ready() -> void:
	%QUESTION.visible = true
	%ANSWER.visible = true
	%WINLOSE.visible = false
	%WINLOSE.text = ""
	GLOBALVARS.seconds_left = _determine_seconds_left()
	_generate_question()

func _physics_process(_delta: float) -> void:
	if not GLOBALVARS.rotation_paused:
		GLOBALVARS.seconds_left -= 0.01666666667
	if GLOBALVARS.selected_player == 0:
		%ANSWER.editable = true
		%ANSWER.grab_focus()
		%ANSWER.edit()
	else:
		%ANSWER.release_focus()
		%ANSWER.editable = false
		
	if GLOBALVARS.seconds_left <= 0:
		GLOBALVARS.seconds_left = _determine_seconds_left()
		GLOBALVARS.rotation_paused = true
		if GLOBALVARS.selected_player in GLOBALVARS.players_remaining:
			GLOBALVARS.players_remaining.erase(GLOBALVARS.selected_player)
			get_node("players/player_%s" % GLOBALVARS.selected_player).queue_free()
		_explode()
		
	if cooldown <= 0 and GLOBALVARS.rotation_paused:
		if len(GLOBALVARS.players_remaining) > 1 and 0 in GLOBALVARS.players_remaining:
			while GLOBALVARS.selected_player not in GLOBALVARS.players_remaining:
				GLOBALVARS.selected_player = (GLOBALVARS.selected_player+1)%4
			
			_generate_question()
			%RULE_ENFORCEMENT.reset_rule_enforcement_device()
			GLOBALVARS.rotation_paused = false
		else:
			get_tree().change_scene_to_file("res://main_scenes/menu.tscn")
				
		
	elif GLOBALVARS.rotation_paused:
		cooldown -= 1
