extends "res://entities/base_movable.gd"

export var color = ""
onready var timer = get_node("Timer")

enum STATE {ACTIVE, INACTIVE, FINISHED, IN_FINISH_AREA}
var current_state = STATE.INACTIVE


func set_state(state):
	current_state = state
	var glow_state = 0
	if state == STATE.ACTIVE:
		glow_state = 1
	if mesh_instance.get_surface_material(0).has_method("set_shader_param"):
		mesh_instance.get_surface_material(0).set_shader_param("Glowing", glow_state)

func _ready():
	pass
	
func get_color():
	return color

func _process(delta):
	handle_player_input()
	

#if a short input comes, it keeps the direction until the timer is finished 
# and makes sure that the player moves after moving a box when input is too short
#else the player still detects the box and stops moving
func start_timer_after_impulse():
	if  timer.is_stopped():
			timer.start()

func get_keys_pressed_amount():
	var keys = 0
	if Input.is_action_pressed("ui_right"):
			keys += 1
	if Input.is_action_pressed("ui_left"):
			keys += 1
	if Input.is_action_pressed("ui_up"):
			keys += 1
	if Input.is_action_pressed("ui_down"):
			keys += 1
	return keys

func handle_player_input():
	var just_moved = false
	
	if current_state == STATE.ACTIVE:
		
		if Input.is_action_just_pressed("ui_right"):
			direction = DIRECTIONS.RIGHT
			just_moved = true
			start_timer_after_impulse()
		elif Input.is_action_just_pressed("ui_left"):
			direction = DIRECTIONS.LEFT
			just_moved = true
			start_timer_after_impulse()
		elif Input.is_action_just_pressed("ui_up"):
			direction = DIRECTIONS.UP
			just_moved = true
			start_timer_after_impulse()
		elif Input.is_action_just_pressed("ui_down"):
			direction = DIRECTIONS.DOWN
			just_moved = true
			start_timer_after_impulse()
			
	
		elif Input.is_action_pressed("ui_right") and direction == DIRECTIONS.RIGHT:
			direction = DIRECTIONS.RIGHT
		elif Input.is_action_pressed("ui_left") and direction == DIRECTIONS.LEFT:
			direction = DIRECTIONS.LEFT
		elif Input.is_action_pressed("ui_up") and direction == DIRECTIONS.UP:
			direction = DIRECTIONS.UP
		elif Input.is_action_pressed("ui_down") and direction == DIRECTIONS.DOWN:
			direction = DIRECTIONS.DOWN
		
		elif Input.is_action_pressed("ui_right"):
			direction = DIRECTIONS.RIGHT
		elif Input.is_action_pressed("ui_left"):
			direction = DIRECTIONS.LEFT
		elif Input.is_action_pressed("ui_up"):
			direction = DIRECTIONS.UP
		elif Input.is_action_pressed("ui_down"):
			direction = DIRECTIONS.DOWN
		
		
		
	
		else:
			if timer.get_time_left() == 0:
				direction = DIRECTIONS.NONE
		
		
		
		if direction != DIRECTIONS.NONE and ray_side.is_colliding():
			
			#print (target_position)
			var collidingObject = ray_side.get_collider()
			#var keys_pressed = get_keys_pressed_amount()
			#print (keys_pressed)
			if collidingObject.is_in_group("group_box") and target_position == collidingObject.position_without_height:
				#print ("moving and colliding!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
				if collidingObject.color == "none" or color == collidingObject.color:
					#print ("push!!!!!!!!!!!!!!!!!")
					
					if direction == DIRECTIONS.UP or direction == DIRECTIONS.DOWN:
						if abs(translation.x - collidingObject.translation.x) < 0.1:
							#print ("pushing up or down")
							collidingObject.push(direction)
						#else:
						#	print ("error in pushing")

					if direction == DIRECTIONS.LEFT or direction == DIRECTIONS.RIGHT:
						#if position_without_height.distance_to(Vector3(last_position.x, 0, last_position.z)) >= tile_size - speed * delta:
						if abs(translation.z - collidingObject.translation.z) < 0.1:
							#print ("pushing left or right")
							collidingObject.push(direction)
						#else:
						#	print ("error in pushing")
		if translation.y < -1.9:
			#FIXME: add a timer before restarting the level
			get_tree().reload_current_scene()
			


	else:
		direction = DIRECTIONS.NONE