extends Control

var time_bonus = 50000
var secret_bonus = 0
var final_score = 0
var time = 0
var label_number = 0
onready var main_label = $Center/Results/Label
onready var score_label = $Center/Results/Score

func _ready():
	set_process(false)
	$MonoBackground/Back.color = Color(randf(),randf(),randf())
	time = global.hud.seconds + global.hud.minutes*60
	if global.secrets_found == 0:
		secret_bonus = 0 
	elif global.secrets_found > 0 and global.secrets_found <= 6:
		secret_bonus = pow(22,global.secrets_found)
	else:
		secret_bonus = 113379904 + pow(11,global.secrets_found)
	match global.current_level:
		0:
			if time <= 60: 
				time_bonus = 99999999900
			elif time <= 70:
				time_bonus = 7500000000
			elif time <= 80: 
				time_bonus = 250000000
			elif time <= 90: #Doable by exploiting quirks in the design (Sandro, 01:24)
				time_bonus = 50000000
			elif time <= 100: #My record (01:35)
				time_bonus = 1000000
			elif time <= 120:
				time_bonus = 50000
			else:
				time_bonus = 100
	
	final_score = (time_bonus + secret_bonus)
	$Timer.start()

func _process(delta):
	if Input.is_action_just_pressed("shoot") or Input.is_action_just_pressed("special") :
		if label_number != -1:
			$MonoBackground/Back.color = Color(randf(),randf(),randf())
			show_results()
		else:
			set_process(false)
			#global.score += final_score
			global.reset_everything()
			global.game_root.change_scene("res://src/Main/MainMenu.tscn",true,"fade_out",global.STATE.CUTSCENE)

func _on_Timer_timeout():
	if label_number != -1:
		$MonoBackground/Back.color = Color(randf(),randf(),randf())
	match label_number:
		0:
			set_process(true)
			$AudioStreamPlayer.play()
			score_label.visible = true
			score_label.text = str(time_bonus)+"\n("+("%02d" % global.hud.minutes) + ":" +("%02d" % global.hud.seconds)+ ")"
			main_label.text = "Time bonus"
			label_number += 1
		1:
			$AudioStreamPlayer.play()
			score_label.text = str(secret_bonus)
			main_label.text = str(global.secrets_found)+" secret"+ ("s" if global.secrets_found > 1 else "") + " found" if global.secrets_found > 0 else "No secrets found"
			label_number += 1
		2:
			show_results()

func _on_BlinkTimer_timeout():
	$PressShoot.visible = !$PressShoot.visible

func show_results():
	$AudioStreamPlayer.play()
	label_number = -1
	main_label.text = "Final score"
	score_label.text = str(global.score + final_score)+"\n("+("%02d" % global.hud.minutes) + ":" +("%02d" % global.hud.seconds)+ ")"
	$BlinkTimer.start()
