extends Control

@onready var bg = $TextureRect
@onready var title1 = $title1
@onready var title2 = $title2
@onready var title3 = $title3

@onready var playButton = $playButton
@onready var aboutButton = $aboutButton

@onready var shader = $shader

@onready var gun = $ColorRect/sprite

var timer := 0.0

var hoverPlay := false
var hoverSelect := false

var lock := false

var blurVal := 0.0
var tintVal := 0.0
var blurred := false

var currentPage := 0

func _ready():
	Globals.unlockedUpgrades = {}

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	shader.material.set_shader_parameter("lod", blurVal)
	shader.material.set_shader_parameter("color_tint", Color(0.0, 0.0, 0.0, tintVal))
	
	gun.look_at(get_global_mouse_position())
	
	if blurred:
		blurVal += (3.0 - blurVal) * 5 * delta
		tintVal += (0.5 - tintVal) * 5 * delta
		
		if currentPage == 1:
			$aboutPage.modulate.a += (1.0 - $aboutPage.modulate.a) * 5 * delta
		
			if $aboutPage.modulate.a > 0.9 and Input.is_action_just_pressed("shoot"):
				blurred = false
				currentPage = 0
				
		if currentPage == 2:
			$briefPage.modulate.a += (1.0 - $briefPage.modulate.a) * 5 * delta
		
			if $briefPage.modulate.a > 0.9 and Input.is_action_just_pressed("shoot"):
				blurred = false
				get_tree().change_scene_to_file("res://rootnode.tscn")
		
		return
		
	if currentPage == 0:
		$aboutPage.modulate.a += (- $aboutPage.modulate.a) * 10 * delta
		
	blurVal += (- blurVal) * 10 * delta
	tintVal += (- tintVal) * 5 * delta
	
	timer += delta

	#bg.position = get_local_mouse_position() / 200 - Vector2(16, 8)
	bg.position.x = fmod((timer * 16), 48) - 48
	bg.position.y = fmod((timer * 16), 48) - 48

	title1.position.y = 120 + sin(timer) * 16
	title2.position.y = 120 + sin(timer) * 16
	title3.position.y = 120 + sin(timer - 1) * 16

	if playButton.get_global_rect().has_point(get_global_mouse_position()):
		if not hoverPlay:
			$clilcksfx.play()
		
		if Input.is_action_pressed("shoot"):
			blurred = true
			currentPage = 2
		
		playButton.scale += (Vector2(1.25, 1.25) - playButton.scale) * 5 * delta
		playButton.modulate.g += (0.23 - playButton.modulate.g) * 5 * delta
		playButton.modulate.b += (0.23 - playButton.modulate.b) * 5 * delta
	
		hoverPlay = true
	else:
		playButton.scale += (Vector2(1, 1) - playButton.scale) * 5 * delta
		playButton.modulate.g += (1.0 - playButton.modulate.g) * 5 * delta
		playButton.modulate.b += (1.0 - playButton.modulate.b) * 5 * delta
		
		hoverPlay = false
		
	if aboutButton.get_global_rect().has_point(get_global_mouse_position()):
		if not hoverSelect:
			$clilcksfx.play()
		
		if Input.is_action_pressed("shoot"):
			blurred = true
			currentPage = 1
		
		aboutButton.scale += (Vector2(1.25, 1.25) - aboutButton.scale) * 5 * delta
		aboutButton.modulate.g += (0.23 - aboutButton.modulate.g) * 5 * delta
		aboutButton.modulate.b += (0.23 - aboutButton.modulate.b) * 5 * delta
	
		hoverSelect = true
	else:
		aboutButton.scale += (Vector2(1, 1) - aboutButton.scale) * 5 * delta
		aboutButton.modulate.g += (1.0 - aboutButton.modulate.g) * 5 * delta
		aboutButton.modulate.b += (1.0 - aboutButton.modulate.b) * 5 * delta

		hoverSelect = false

func _on_play_button_mouse_entered() -> void:
	pass # Replace with function body.
