extends Node2D
class_name CrystalGlow

var glow_colour : String :
	set(value):
		glow_colour = value.to_lower()
var orientation : String:
	set(value):
		orientation = value.to_lower()

var animation_start_timer : float
var animation_start_offset : float = randf_range(0, 3)

var internals_updated : bool = false

@onready var animation_player : AnimationPlayer = $CrystalGlowAnimation
@onready var glow_light : Sprite2D = $CrystalGlowLight
@onready var glow_area : Sprite2D = $CrystalAreaLight
@onready var glow_area_large : Sprite2D = $CrystalAreaLargeLight
@onready var crystal_particles : CPUParticles2D = $CrystalParticles

func _process(delta : float) -> void:

	# The first time this is processed in the scene we need to update the orientation and colour
	if !internals_updated:
		update()
		internals_updated = true

	animation_start_timer += delta
	if animation_start_timer >= animation_start_offset and !animation_player.is_playing():
		animation_player.play('CrystalGlow')

func update() -> void:
	update_orientation()
	update_colour()

# Update the orientation of the crystal glow so its pointing in the right direction
func update_orientation() -> void:
	if orientation == 'up':
		rotation = 0
	elif orientation == 'down':
		rotation = PI
	elif orientation == 'left':
		rotation = -PI/2
	elif orientation == 'right':
		rotation = PI/2

# Update the colour of the crystal glow
func update_colour() -> void:

	var base_blue : String = '5ffffe'
	var base_yellow : String = 'F2F230'
	var base_red : String = 'FF3333'

	if glow_colour == 'blue':
		glow_light.self_modulate = base_blue + '15'
		glow_area.self_modulate = base_blue + '04'
		glow_area_large.self_modulate = base_blue + '04'
		crystal_particles.self_modulate = '00ffff62'
	elif glow_colour == 'yellow':
		glow_light.self_modulate = base_yellow + '15'
		glow_area.self_modulate = base_yellow + '04'
		glow_area_large.self_modulate = base_yellow + '04'
		crystal_particles.self_modulate = base_yellow + '62'
	elif glow_colour == 'red':
		glow_light.self_modulate = base_red + '15'
		glow_area.self_modulate = base_red + '04'
		glow_area_large.self_modulate = base_red + '04'
		crystal_particles.self_modulate = base_red + '62'
	else:
		pass
