extends Node2D

enum INTENSITY { weakest, weak, normal, heavy, heaviest }


var camera = null

export var is_raining = false
export var is_snowing = false
export var is_lava = false
export var is_lightfog = false
export var is_lavarock = false

var rock_timer = Timer.new()


func set_camera(_camera):
	self.camera = _camera


func pause(paused):
	if paused:
		Utils.interpolate($SnowParticle, "speed_scale", 1, 0.005)
		Utils.interpolate($RainParticle, "speed_scale", 1, 0.005)
		Utils.interpolate($LavaGroundParticle, "speed_scale", 1, 0.005)
		Utils.interpolate($LightFogParticle, "speed_scale", 1, 0.005)
	else: 
		Utils.interpolate($SnowParticle, "speed_scale", 0, 1)
		Utils.interpolate($RainParticle, "speed_scale", 0, 1)
		Utils.interpolate($LavaGroundParticle, "speed_scale", 0, 1)
		Utils.interpolate($LightFogParticle, "speed_scale", 0, 1)


# --- Rock Lava Environment ---




# --- Snow Environment ---

func is_snow():
	return $SnowParticle.emitting

func start_snow():
	is_snowing = true
	Utils.show_object($SnowParticle)
	$SnowParticle.show()
	$SnowParticle.restart()
	$SnowParticle.emitting = true

func stop_snow():
	is_snowing = false
	Utils.hide_object($SnowParticle)
	$SnowParticle.show()
	$SnowParticle.emitting = false

func hide_snow():
	is_snowing = false
	Utils.hide_object($SnowParticle)
	$SnowParticle.hide()
	$SnowParticle.restart()
	$SnowParticle.emitting = false

func set_snow_strength(strength = 0.5):
	$SnowParticle.amount = 20000 * strength



# --- Rain Environment ---
func is_rain():
	return $RainParticle.emitting
	
func start_rain():
	is_raining = true
	Utils.show_object($RainParticle)
	$RainParticle.show()

func stop_rain():
	is_raining = false
	Utils.hide_object($RainParticle)
	$RainParticle.show()

func hide_rain():
	is_raining = false
	Utils.hide_object($RainParticle)
	$RainParticle.show()



#--- Lava Ground Particles

func is_lavaground():
	return $LavaGroundParticle.emitting
	
func start_lavaground():
	is_lava = true
	Utils.show_object($LavaGroundParticle)
	$LavaGroundParticle.show()
	$LavaGroundParticle.restart()

func stop_lavaground():
	is_lava = false
	Utils.hide_object($LavaGroundParticle)
	$LavaGroundParticle.show()

func hide_lavaground():
	is_lava = false
	Utils.hide_object($LavaGroundParticle)
	$LavaGroundParticle.hide()
	$LavaGroundParticle.restart()



# ---- Light fog (can be used as clouds) ---

func is_fog():
	return $LightFogParticle.emitting
	
func start_lightfog():
	is_lightfog = true
	Utils.show_object($LightFogParticle)
	$LightFogParticle.show()
	$LightFogParticle.restart()
	$LightFogParticle.emitting = true

func stop_lightfog():
	is_lightfog = false
	Utils.hide_object($LightFogParticle)
	$LightFogParticle.show()
	$LightFogParticle.emitting = true

func hide_lightfog():
	is_lightfog = false
	Utils.hide_object($LightFogParticle)
	$LightFogParticle.restart()
	$LightFogParticle.emitting = true

func set_fog_strength(strength = 0.2):
	$LightFogParticle.amount = 1000 * strength

func set_fog_speed(speed = 1.0):
	$LightFogParticle.speed_scale = speed


# --------- Meditation
# -- If the player doesn't move on a certain area for 30 seconds, he'll start meditate, and a different effect will appear.

var is_meditating = false
var alpha_meditating = 0.0

func hide_meditation():
	is_meditating = false

func show_meditation():
	is_meditating = true
	

# --------- Fade in / Fade out effects

func hide_fade():
	$FadeRect.hide()

func show_black():
	$FadeRect.show()
	$FadeRect.color = Color(0,0,0,1)
	
func show_white():
	$FadeRect.show()
	$FadeRect.color = Color(1,1,1,1)

func fade_out_black(timer):
	fade_out(timer, Color(0,0,0,1))

func fade_in_black(timer):
	fade_in(timer, Color(0,0,0,1))

func fade_out_white(timer):
	fade_out(timer, Color(1,1,1,1))

func fade_in_white(timer):
	fade_in(timer, Color(1,1,1,1))

func fade_in(timer, color):
	$FadeRect.color = color
	var colorAlpha = color
	colorAlpha.a = 0
	$FadeRect.modulate.a = 0
	$FadeRect.show()
	$Tween.interpolate_property($FadeRect, "modulate", colorAlpha, color, timer, Tween.TRANS_LINEAR, Tween.EASE_IN)
	$Tween.start()

func fade_out(timer, color):
	$FadeRect.color = color
	var colorAlpha = color
	colorAlpha.a = 0
	$FadeRect.modulate.a = 1
	$FadeRect.show()
	$Tween.interpolate_property($FadeRect, "modulate", color, colorAlpha, timer, Tween.TRANS_LINEAR, Tween.EASE_IN)
	$Tween.start()
	


func stop_all_effects():
	stop_lavaground()
	stop_lightfog()
	stop_rain()
	stop_snow()
	hide_meditation()


#---  At start, hide everything, stop particles, do nothin'! ---
func _ready():
	if not is_snowing: hide_snow()
	else: start_snow()
	if not is_raining: hide_rain()
	else: start_rain()
	if not is_lavaground(): hide_lavaground()
	else: start_lavaground()
	if not is_lightfog: hide_lightfog()
	else: start_lightfog()
	
	$RainParticle.emitting = true
	$LavaGroundParticle.emitting = true
	$SnowParticle.emitting = true
	
	hide_fade()
	
	add_child(rock_timer)
	
	rock_timer.connect("timeout", self, "falling_rock")
	rock_timer.start(3.0)


func falling_rock():
	if not is_lavarock: return
	
	var lavarock = Utils.load_file("res://resources/environment/FallingLavaRock.tscn").instance()
	var positionX = Utils.random(1500) - 500
	lavarock.position = Vector2(position.x + positionX, position.y - 430) 
	Game.playground.add_child(lavarock)


func change_pos(vector_pos):
	position = vector_pos

# --- The EnvironmentNode must be ALWAYS on the top of every other child. Update its position!
func _process(_delta):

	if get_position_in_parent() < get_parent().get_child_count() - 1:
		get_parent().move_child(self, get_parent().get_child_count())
	
	#... but if there's an object with group "InFront", he must stay in front!
	var obj_list = get_parent().get_tree().get_nodes_in_group("InFront")
	if obj_list:
		for obj2 in obj_list:
			get_parent().move_child(obj2, get_parent().get_child_count())
