extends Node

var enable_input = true
var last_keys_pressed_amount = 0
var jump_pressed_time = 0
var player_scene = null
var game_scene
var was_enabled = false


enum DIRECTIONS {UP, DOWN, LEFT, RIGHT, NONE}

@onready var axis = Vector2.ZERO
var pressed_direction = DIRECTIONS.NONE

# Called when the node enters the scene tree for the first time.
func _ready():
	pressed_direction = DIRECTIONS.NONE

func _process(delta):
	
	#game_scene = SceneManager.game_scene
	#if game_scene != null:
	#	player_scene = game_scene.player_scene
	#else:
		#if the game_scene is null, then there is no player, meaning player_scene is null too
	#	player_scene = null
	
	get_input_axis()
	
	_handle_action_input()
	
	last_keys_pressed_amount = get_keys_pressed_amount()
	
	if Input.is_action_just_pressed("reset"):
		if player_scene != null:
			player_scene.reset_position()
	

	if player_scene != null:
		if player_scene.driving_object == null:
			control_object(player_scene, delta)
					
		else:
			control_object(player_scene.driving_object, delta)
				
	
func control_object(controlable, delta):
	if controlable == null:
		return
	
	if controlable.driver != null:
		if controlable.ray_ceiling.is_colliding():
			controlable.is_blocked = true
		else:
			controlable.is_blocked = false

	var is_grid_alighned = controlable.grid_distance_x < 0.1 and controlable.grid_distance_z < 0.1		

	
	controlable.keys_amount_hack = last_keys_pressed_amount
		
	#only left or right key pressed
	if  axis.x != 0 and axis.y == 0:

		if axis.x > 0:
			controlable.requested_direction = DIRECTIONS.LEFT
		elif axis.x < 0:
			controlable.requested_direction = DIRECTIONS.RIGHT
	
	#only up or down key pressed				
	elif axis.x == 0 and axis.y != 0:

		if axis.y > 0:
			controlable.requested_direction = DIRECTIONS.DOWN
		elif axis.y < 0:
			controlable.requested_direction = DIRECTIONS.UP
	
	#combination of left/ right and up/down	
	elif axis.x != 0 and axis.y != 0:
		if controlable.requested_direction == DIRECTIONS.NONE:
			controlable.requested_direction = controlable.view_direction
	
		#first handle just pressed actions
		if Input.is_action_just_pressed("ui_left"):
			controlable.requested_direction = DIRECTIONS.LEFT
		elif Input.is_action_just_pressed("ui_down"):
			controlable.requested_direction = DIRECTIONS.DOWN
		elif Input.is_action_just_pressed("ui_up"):
			controlable.requested_direction = DIRECTIONS.UP
		elif Input.is_action_just_pressed("ui_right"):
			controlable.requested_direction = DIRECTIONS.RIGHT
			
		#if !controlable.can_move() or controlable.front_area.has_overlapping_bodies():
		#movement with 2 keys pressed, and when can't walk further into the current direction
		elif axis.x > 0 and axis.y > 0 and (is_grid_alighned or !controlable.can_move() or controlable.front_area.has_overlapping_bodies()): # left and down
			if controlable.requested_direction == DIRECTIONS.LEFT and controlable.view_direction == DIRECTIONS.LEFT:
				controlable.requested_direction = DIRECTIONS.DOWN
			elif controlable.requested_direction == DIRECTIONS.DOWN and controlable.view_direction == DIRECTIONS.DOWN:
				controlable.requested_direction = DIRECTIONS.LEFT

		elif axis.x > 0 and axis.y < 0 and (is_grid_alighned or !controlable.can_move() or controlable.front_area.has_overlapping_bodies()): #left and up
			if controlable.requested_direction == DIRECTIONS.LEFT and controlable.view_direction == DIRECTIONS.LEFT:
				controlable.requested_direction = DIRECTIONS.UP
			elif controlable.requested_direction == DIRECTIONS.UP and controlable.view_direction == DIRECTIONS.UP:
				controlable.requested_direction = DIRECTIONS.LEFT
		
		elif axis.x < 0 and axis.y > 0 and (is_grid_alighned or !controlable.can_move() or controlable.front_area.has_overlapping_bodies()): #right and down
			if controlable.requested_direction == DIRECTIONS.RIGHT and controlable.view_direction == DIRECTIONS.RIGHT:
				controlable.requested_direction = DIRECTIONS.DOWN
			elif controlable.requested_direction == DIRECTIONS.DOWN and controlable.view_direction == DIRECTIONS.DOWN:
				controlable.requested_direction = DIRECTIONS.RIGHT		
			
		elif axis.x < 0 and axis.y < 0 and (is_grid_alighned or !controlable.can_move() or controlable.front_area.has_overlapping_bodies()): #right and up
			if controlable.requested_direction == DIRECTIONS.RIGHT and controlable.view_direction == DIRECTIONS.RIGHT:
				controlable.requested_direction = DIRECTIONS.UP
			elif controlable.requested_direction == DIRECTIONS.UP and controlable.view_direction == DIRECTIONS.UP:
				controlable.requested_direction = DIRECTIONS.RIGHT
				
		

	else:
		controlable.requested_direction = DIRECTIONS.NONE
		
	if controlable.requested_direction != DIRECTIONS.NONE and controlable.last_requested_direction != DIRECTIONS.NONE:
		if controlable.requested_direction != controlable.last_requested_direction:
			apply_turn_cooldown(controlable)
	
	#if Input.is_action_just_pressed("debug_key_1"):
	#	controlable.enable_movement = true
	#if Input.is_action_just_pressed("debug_key_2"):
	#	controlable.enable_movement = false
	
	#if Input.is_action_pressed("stop") and controlable.is_on_floor():
	#	if controlable.enable_movement == true:
	#		was_enabled = true
	#	controlable.enable_movement = false
	#elif was_enabled:
	#	controlable.enable_movement = true
	#	was_enabled = false
		

func apply_turn_cooldown(controlable):
	controlable.enable_turning = false	
	await get_tree().create_timer(0.07).timeout
	controlable.enable_turning = true		

#action input (jumping, etc)		
func _handle_action_input():
	var max_jump_timer_frames = 1
	if player_scene == null:
		return

	if Input.is_action_pressed("jump"):
		
		if jump_pressed_time < max_jump_timer_frames:
			jump_pressed_time += 1
		
		if jump_pressed_time >= max_jump_timer_frames:
			if player_scene.is_jumping == false:
				jump_pressed_time = 0
				
				jump(19.5)
			
	

func jump(_jump_vel = 10):
		
	
		if player_scene != null:
			if player_scene.driving_object != null:
				player_scene.requested_direction = player_scene.driving_object.requested_direction
				player_scene.velocity.x = player_scene.driving_object.velocity.x
				player_scene.velocity.z = player_scene.driving_object.velocity.z
				("input manager - jump dir " + str(player_scene.direction_to_string(player_scene.requested_direction)))
			player_scene.jump(_jump_vel)

#helper functions
func get_input_axis():
	axis.x = int(Input.is_action_pressed("ui_left")) - int(Input.is_action_pressed("ui_right"))
	axis.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))	

func get_keys_pressed_amount():
	var keys = 0
	if Input.is_action_pressed("ui_right"):
			keys += 1
	if Input.is_action_pressed("ui_left"):
			keys += 1
	if Input.is_action_pressed("ui_up"):
			keys += 1
	if Input.is_action_pressed("ui_down"):
			keys += 1
	return keys


