extends Reference

const NAME = "sleeping"

var ctx: KinematicBody = null
var hop_position: Spatial
var made_it_to_bed: bool
var model_transform: Spatial
var anim_player: AnimationPlayer
var old_model_origin: Vector3

var waiting := false

func _init(p_made_it_to_bed: bool, p_hop_position: Spatial = null):
	hop_position = p_hop_position
	made_it_to_bed = p_made_it_to_bed

func enter_from(state):
	hop_position = hop_position if hop_position else ctx.get_tree().get_nodes_in_group("bed_hop_position")[0]
	model_transform = ctx.get_node("model_transform")
	anim_player = ctx.get_node("model_transform/node_animations")

	# Pause everything but the player
	ctx.get_tree().paused = true
	ctx.get_node("hunger_timer").pause_mode = ctx.PAUSE_MODE_STOP
	ctx.pause_mode = ctx.PAUSE_MODE_PROCESS
	
	# Drop any carried item
	if ctx.has_item():
		ctx.drop_item_in_hand()

	# Prepare to play animation

	if made_it_to_bed:
		# Slide the player to the front of the bed
		old_model_origin = model_transform.global_transform.origin
		var tween = model_transform.create_tween()
		tween.tween_property(model_transform, "global_transform:origin", hop_position.global_transform.origin, .1)
		yield(tween, "finished")
		# Warp the player to the right position & orientation
		model_transform.global_transform = hop_position.global_transform
	else:
		# Harm the player and show message
		ctx.set_health(ctx.health - 1)
		var node = Dialogic.start("day_end")
		node.pause_mode = ctx.PAUSE_MODE_PROCESS
		ctx.add_child(node)
		yield(node, "timeline_end")
		# Warp the player to the right position & orientation
		ctx.global_transform.origin = hop_position.global_transform.origin
		model_transform.global_transform = hop_position.global_transform
		old_model_origin = model_transform.global_transform.origin


	# Play hop animation
	ctx.get_node("model_transform/DungMan/AnimationTree")["parameters/Move/playback"].travel("idle")
	anim_player.call_deferred("play", "hop_into_bed")
	yield(anim_player, "animation_finished")
	waiting = true

	# Reset the day timer
	for game_manager in ctx.get_tree().get_nodes_in_group("game_manager"):
		game_manager.reset_day_timer()

func exit_to(state):
	ctx.pause_mode = ctx.PAUSE_MODE_INHERIT
	ctx.get_node("hunger_timer").pause_mode = ctx.PAUSE_MODE_INHERIT
	ctx.get_tree().paused = false

func physics_process(delta):
	pass

func unhandled_input(event: InputEvent):
	if not waiting:
		return
	if not event.is_action_pressed("action"):
		return

	ctx.get_tree().set_input_as_handled()

	# Play hop out animation
	anim_player.call_deferred("play", "hop_out_of_bed")
	yield(anim_player, "animation_finished")

	# Slide the player back to the front of the bed
	var tween = model_transform.create_tween()
	tween.tween_property(model_transform, "global_transform:origin", old_model_origin, .1)
	yield(tween, "finished")
	
	# Show message
	var node = Dialogic.start("wake_up")
	node.pause_mode = ctx.PAUSE_MODE_PROCESS
	ctx.add_child(node)
	yield(node, "timeline_end")

	ctx.change_state(ctx.StateDefault.new())

func on_sleep(made_it_to_bed: bool, hop_position: Spatial):
	assert(false, "shouldn't be able to sleep while sleeping")
