extends RigidBody3D

var grabject : bool = true
var low_height : bool = false

@export var model : String = "mug"
@export var impact_loud : bool = false
@export var impact_lethal : bool = false
@export var impact_annoyonly : bool = false
@export var impact_sound : String = "genericimpact"
@export var prompt : String = "Grab mug"
@export var icon : String = "hand"
var icon_offset : float = 0.0
var collided_this_frame : bool = false

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	if model == "mug" or "mug_full":
		$mesh_mugge.visible = true
		$shape_mugge.disabled = false
		$shape_mugge_secondary.disabled = false
		icon_offset = $shape_mugge.shape.height / 2
		if model == "mug_full":
			$mesh_mugge/coffee.visible = true

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	pass
	
func _physics_process(_delta: float) -> void:
	if linear_velocity.length() < 0.025:
		$SleepTimer.start(2)

func sleep():
	freeze = true

func hold(delay):
	freeze = true
	$DelayTimer.start(delay)

func hold2():
	set_collision_layer_value(1, false)
	if model == "mug" or model == "mug_full":
		$mesh_mugge.visible = false
		if model == "mug_full":
			mug_spill(global_transform.origin)
	global_transform.origin.y += 500

func unhold(spawnlocation):
	global_transform.origin = spawnlocation
	freeze = false
	if model == "mug" or model == "mug_full":
		$mesh_mugge.visible = true

func throw(spawnlocation, direction, force):
	unhold(spawnlocation)
	apply_central_impulse(direction * force)

func handle_collision(_hush):
	var colliders = get_colliding_bodies()
	for body in colliders:
		print(body.get_name())
		if !collided_this_frame and body.get("npc"):
			body.impact()

func mug_spill(_location):
	$mesh_mugge/coffee.visible = false
	model = "mug"
	prompt = "Grab mug"
