extends CharacterBody3D

@export var camera : Camera3D
@export var mouse_sensitivity : float = 0.003 # TODO: this is sketchy check that its framerate independant pls
@export var raycast : RayCast3D

@export var held_object : Node3D
@export var has_object : bool = false
@export var hand : Node3D
@export var collider : CollisionShape3D
@export var area : Area3D
@export var interact_sound : AudioStreamPlayer

signal object_clicked(object : Node3D)

var SPEED = 5.0
const JUMP_VELOCITY = 9 #was 4.5

#effects
var dash_time = 0
@export var dash_node : Node3D
var dashing = false


# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Handle jump. # now handled by chronos
	#if Input.is_action_just_pressed("jump") and is_on_floor():
		#velocity.y = JUMP_VELOCITY
	
	#free the mouse for debugging
	if Input.is_action_just_pressed("esc"):
		#if (Input.mouse_mode == Input.MOUSE_MODE_CAPTURED):
			#Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		#else:
			#Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
		get_parent().pause()
	
	# interact with stuff
	if Input.is_action_just_pressed("drop"):
		drop()
	
	# interact with stuff
	if Input.is_action_just_pressed("interact"):
		if "name" in raycast.get_collider():
			if "command_name" in raycast.get_collider():
				# clicked a command block
				print(raycast.get_collider().command_name)
				
				#hand.add_child(held_object)
				if(!has_object):
					held_object = raycast.get_collider()
					held_object.reparent(hand)
					held_object.position = Vector3(0,0,0)
					held_object.freeze = true
					held_object.collider.disabled = true
					has_object = true
					interact_sound.pitch_scale = randf_range(0.8,1.2)
					interact_sound.play()
			elif "placed_command" in raycast.get_collider():
				# clicked a pedestal, place on pedestal if its not already got something
				var clicked_pedestal = raycast.get_collider() as Pedestal
				print("clicked pedestal: "+ clicked_pedestal.name)
				if(has_object and not clicked_pedestal.has_command):
					print("trying to place")
					held_object.reparent(clicked_pedestal.slot)
					held_object.position = Vector3(0,0,0)
					held_object.rotation_degrees = Vector3(0,0,0)
					held_object.collider.disabled = false
					has_object = false
					clicked_pedestal.has_command = true
					clicked_pedestal.placed_command = held_object
					interact_sound.pitch_scale = randf_range(0.8,1.2)
					interact_sound.play()
			else:
				print(raycast.get_collider().name)
			object_clicked.emit(raycast.get_collider())
		else:
			print("womp womp")
	
	
		

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("mov_left", "mov_right", "mov_up", "mov_down")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	if (dash_time > 0 ):
		gravity = 0
		dash_time -= delta
		var dash_direction = camera.global_position.direction_to(dash_node.global_position)
		dash_direction.y = 0
		dash_direction = dash_direction.normalized()
		velocity = dash_direction * SPEED*350 * delta 
	elif dashing:
		dashing = false
		velocity = Vector3(0,0,0)
		gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
		pass
	
	
	
	move_and_slide()
	

func _input(event):
	# camera control
	if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
		rotate_y(-event.relative.x * mouse_sensitivity)
		camera.rotate_x(-event.relative.y * mouse_sensitivity)
		camera.rotation.x = clampf(camera.rotation.x, -deg_to_rad(70), deg_to_rad(70))

func drop():
	if has_object:
		held_object.reparent(get_parent())
		held_object.freeze = false
		held_object.collider.disabled = false
		has_object = false
		


func _on_chrono_manager_broadcast(command):
	match command:
		"jump":
			velocity.y = JUMP_VELOCITY
		"dash":
			#move in looked direction for 0.5 secs
			dash_time = 0.3
			dashing = true
		"run":
			SPEED = 10
		"phase":
			collider.disabled = true
		_:
			printerr("bad command sent")

func clear_effects():
	#enable collider
	collider.disabled = false
	#end dash
	
	
	SPEED = 5
	pass


func _on_chrono_manager_tick(time):
	clear_effects()


func _on_area_3d_area_entered(area):
	pass # Replace with function body.
