extends Spatial


# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
enum {DOOR_OPEN, DOOR_CLOSED}
var door_l_state = DOOR_CLOSED
var door_r_state = DOOR_CLOSED
var act_l_delay = 0.0
var act_r_delay = 0.0
var l_act = false
var r_act = false
var hide_delay = 4.0
var hidden = false
func activate_l_door(def):
	if def && door_l_state == DOOR_CLOSED:
		$closet/closet_door_left/AnimationPlayer.play("openning")
		door_l_state = DOOR_OPEN
	if !def && door_l_state == DOOR_OPEN:
		$closet/closet_door_left/AnimationPlayer.play_backwards("openning")
		door_l_state = DOOR_CLOSED
func activate_r_door(def):
	if def && door_r_state == DOOR_CLOSED:
		$closet/closet_door_right/AnimationPlayer.play("openning_r")
		door_r_state = DOOR_OPEN
	if !def && door_r_state == DOOR_OPEN:
		$closet/closet_door_right/AnimationPlayer.play_backwards("openning_r")
		door_r_state = DOOR_CLOSED
func _ready():
	add_to_group("closet")
	add_to_group("hide_spot")
	var ret = $left.connect("body_entered", self, "body_enter")
	assert(ret == OK)
	ret = $left.connect("body_exited", self, "body_exit")
	assert(ret == OK)
	ret = $right.connect("body_entered", self, "body_enter")
	assert(ret == OK)
	ret = $right.connect("body_exited", self, "body_exit")
	assert(ret == OK)
	ret = $Timer.connect("timeout", self, "free_character")
	assert(ret == OK)

func _process(delta):
	if l_act:
		act_l_delay += delta
		if act_l_delay >= 1.5:
			if door_l_state == DOOR_CLOSED:
				door_l_state = DOOR_OPEN
				l_act = false
	if r_act:
		act_l_delay += delta
		if act_l_delay >= 1.5:
			if door_l_state == DOOR_CLOSED:
				door_l_state = DOOR_OPEN
				r_act = false
func free_character():
	var c = get_meta("hidden_character")
	c.remove_collision_exception_with($closet/closet_door_left/static_collision)
	c.remove_collision_exception_with($closet/closet_door_right/static_collision)
	c.remove_collision_exception_with($closet/static_collision)
	c.global_transform = $exit_spot.global_transform
	yield(get_tree().create_timer(0.1), "timeout")
	c.global_transform = $exit_spot.global_transform
	c.set_meta("smart_object", false)
	c.do_walk()
	hidden = false
	set_meta("hidden_character", null)
func body_enter(body):
	if !body.is_in_group("characters"):
		return
	if body.get_meta("grabbing"):
		return
	# TODO
	if body.get_meta("grabbed"):
		return
	if !hidden:
		if body == global.player:
			print("CLOSET")
			activate_l_door(true)
			activate_r_door(true)
		elif body.has_meta("action"):
			var action = body.get_meta("action")
			if action == "hide":
				activate_l_door(true)
				activate_r_door(true)
				body.set_meta("smart_object", true)
				body.do_stop()
				set_meta("hidden_character", body)
				hidden = true
				yield(get_tree().create_timer(hide_delay), "timeout")
				body.add_collision_exception_with($closet/closet_door_left/static_collision)
				body.add_collision_exception_with($closet/closet_door_right/static_collision)
				body.add_collision_exception_with($closet/static_collision)
				body.global_transform = $hide_spot.global_transform
				yield(get_tree().create_timer(0.1), "timeout")
				body.global_transform = $hide_spot.global_transform
				yield(get_tree().create_timer(hide_delay), "timeout")
				activate_l_door(false)
				activate_r_door(false)
			else:
				return
		else:
			activate_l_door(true)
			activate_r_door(true)
	else:
		if body == get_meta("hidden_character"):
			return
		if body == global.player:
				activate_l_door(true)
				activate_r_door(true)
				$Timer.start(4.0)
		elif body.has_meta("action"):
			var action = body.get_meta("action")
			if action == "hide":
				return
			if action == "patrol":
#				var c = get_meta("hidden_character")
				activate_l_door(true)
				activate_r_door(true)
				$Timer.start(4.0)
func body_exit(_body):
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass
