extends Control


var time_hour = 1
var time_minutes = 0

var number_keys = 0
var keyring_radius = 35


func _ready():
	update_keys()
	$PauseMenu.visible = false


func _on_Timer_timeout():
	time_minutes += 10
	if time_minutes >= 60:
		time_hour += 1
		time_minutes = 0
		
	var hudtime = ""
	if time_minutes == 0:
		hudtime = "00"
	if time_minutes < 10:
		hudtime = "0"+str(time_minutes)
	else:
		hudtime = str(time_minutes)
		
		
	$Margin/Time/ClockBack/ClockMargin/Label.text = str(time_hour)+":"+hudtime
	
	if time_hour >= 6:
		yield(get_tree().create_timer(2), "timeout")
		get_tree().reload_current_scene()


func _input(_event):
	if Input.is_action_just_pressed("add_key"):
		number_keys += 1
		update_keys()
		
	#Pause the game	
	if Input.is_action_just_pressed("ui_cancel"):
		pause()


func update_keys():
	for key in $KeyRing.get_children():
		key.queue_free()
	
	for key in number_keys:
		var new_key = TextureRect.new()
		new_key.texture = load("res://UI/HUD/key.png")
		$KeyRing.add_child(new_key)
	
	#var spacing = TAU / number_keys
	if number_keys != 1:
		var spacing = (PI/2.5) / (number_keys - 1)
		for key in $KeyRing.get_children():
			var a = spacing * key.get_position_in_parent() - PI/2
			var dest = key.rect_position + Vector2(keyring_radius, 0).rotated(a) + Vector2(70,70)
			key.rect_position = dest
			key.set_rotation(a+deg2rad(15))
	else:
		for key in $KeyRing.get_children():
			var temp_pos = key.rect_position + Vector2(keyring_radius, 0) + Vector2(75,70)
			key.rect_position = temp_pos
			key.set_rotation(deg2rad(25))



func pause():
	if get_tree().paused == false:
		get_tree().paused = true
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		$PauseMenu.visible = true
	else:
		get_tree().paused = false
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		$PauseMenu.visible = false


func _on_Resume_pressed():
	pause()
