extends MovementComponent

## Constants available to you
# UP = -1.0
# DOWN = 1.0
# LEFT = -1.0
# RIGHT = 1.0

# Avalable functions to call 
# A Series of helper functions
# go_up():
# go_down():
# go_left():
# go_right():
# stop():


export var player_number: int = 1

export (NodePath) var interactable_node

onready var interactable_receiver_node: Interactable_Receiver = get_node(interactable_node) 
onready var ladder_detection:RayCast2D = $"../Ladder_Detection"

var ladder_collision: RayCast2D

func _ready():
	ladder_collision = ladder_detection
	# Guess there isn't a hit from inside in 3.5
	# Remember to make raycast as tall as needed for the player
	#ladder_collision.hit_from_inside = true

func process_physics(delta):
	PlayerInfo.player_position = owner.global_position

func process_input(event: InputEvent):
	var interactables = interactable_receiver_node.interactable_function_callables
	for interactable in interactables: # Iterate through the interactable collection
		if interactable is Interactable: # Make sure we didn't push something dumb in there.
			if (Input.is_action_pressed("ui_up")): # We pressed up?
				interactable.trigger_interaction()
	get_movement_direction()
	
	
# Return the desired direction of movement for the character
# in the range [-1, 1], where positive values indicate a desire
# to move to the right and negative values to the left.
func get_movement_direction() -> float:
	#return Input.get_axis('move_left', 'move_right')
	desired_movement_vector.x = Input.get_axis("move_left_" + str(player_number), "move_right_" + str(player_number))
	return Input.get_axis("move_left_" + str(player_number), "move_right_" + str(player_number))

# Return a boolean indicating if the character wants to jump
func wants_jump() -> bool:
	
	if Input.is_action_just_pressed("jump_" + str(player_number)):
		desired_movement_vector.y = 1
		return true
	else:
		return false

# Return a boolean indicating if the character wants to jump
func wants_shoot() -> bool:	
	if Input.is_action_just_pressed("shoot_" + str(player_number)):
		return true
	else:
		return false

func wants_dash() -> bool:	
	if Input.is_action_just_pressed("dash_" + str(player_number)):
		return true
	else:
		return false

func wants_roll() -> bool:
	if Input.is_action_just_pressed("dash_" + str(player_number)) and Input.is_action_pressed("ui_up"):
		return true
	else:
		return false

func wants_climb() -> bool:
	if ladder_detection.is_colliding() and Input.is_action_pressed("ui_up"):
		#print("betcha wanna climb though!")
		return true
	else:
		return false

func get_climb_shape_location() -> Vector2:
	if ladder_detection.is_colliding():
		var point: Vector2 = ladder_collision.get_collision_point()
		var shape_id = ladder_collision.get_collider_shape()
		var object = ladder_collision.get_collider()
		
		# This works but doesn't get the shapes location. 
#		if object is Area2D:
#			print("You got an area bro!" , object.global_position)
			
		
		# This appears to get the shapes location!
		var area = Physics2DServer.area_get_transform(object)
		var areashape = Physics2DServer.area_get_shape_transform(object, shape_id)
		
		
		var y_dir = Input.get_axis("ui_up" , "ui_down")
#var target = get_collider() # A CollisionObject2D.
#var shape_id = get_collider_shape() # The shape index in the collider.
#var owner_id = target.shape_find_owner(shape_id) # The owner ID in the collider.
#var shape = target.shape_owner_get_owner(owner_id)

		#UiManager.debug_text = str(round(point.x)) + ' ' + str(round(point.y)) #( "Mod(" + str(sign(_move_speed_modifier)) + ") Dir(" + str(move_direction) + ") Vel(" + str(sign(current_x_velocity)) + ") Mov(" + str(sign(move_component.desired_movement_vector.x)) +

		return Vector2(areashape.origin.x, y_dir)
	else:
		return Vector2(-1,-1)

## Other needed variables
# desired_movement_vector: Vector2 - Used to store desired movement depending on the state
# current_movement_state:String - The name of the current State

# Since animactor state machine can actually view properties from this type
# I'm thinking about moving the velocity tracker in here instead of player.
# velocity: Vector2 - Current velocity, you can change this at will but state may modify it

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


# Called every frame. 'delta' is the elapsed time since the previous frame.
# you do not normally need to define this for Actor
#func process(delta):
#   pass

# For Enemy and NPC Actors:
# should be called on the frame process. 

# For Player Actors:
# Should be called on the input process 


