extends Actor

export var player_number: int = 1

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

onready var player_inventory :InventoryManager = InventoryManager.new()

var punch_item :Item = preload("res://assets/items/punch.tres")

# Called when the node enters the scene tree for the first time.
func _ready():
	PlayerInfo.player_health = $Health_Component.health
	PlayerInfo.player_inventory = player_inventory
	# Set initial player position in world.
	global_position = LevelInfo.player_start_position

	player_inventory.add_to_inventory(punch_item)
	player_inventory.select_primary(punch_item)




# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	PlayerInfo.player_position = global_position.round()
	match movement_state_machine.current_state.name:
		"idle":
			if Input.is_action_just_released("switch_primary_item_" + str(player_number)):
				var _selected_item = player_inventory.switch_primary()
				if _selected_item:
					print("Switched Primary item to: ", _selected_item.name)
			elif Input.is_action_just_released("switch_secondary_item_" + str(player_number)):
				var _selected_item = player_inventory.switch_secondary()
				if _selected_item:
					print("Switched Secondary item to: ", _selected_item.name)

func hit_Receiver(damage):
	$Hurtbox_Component.set_hurtbox(false)
	$Health_Component.take_damage(damage)
	if $Health_Component.health > 0:
		movement_state_machine.change_to_known_state('hurt')
	PlayerInfo.player_health = $Health_Component.health
	if PlayerInfo.player_health <= 0:
		return
	yield( get_tree().create_timer(2 + movement_state_machine.get_state_reference('hurt').timeout_seconds), "timeout")
	$Hurtbox_Component.set_hurtbox(true)

func touch_the_thing(the_thing: Interactable) -> bool:
	print("You see! a THING...", the_thing.name)
	if the_thing is HealthPickup:
		# Do some healthy stuff.
		the_thing.trigger_interaction()
		return false
	if the_thing is LevelTransition:
		if movement_state_machine.current_state.name != 'enter_right':
			if the_thing._transition_type == 'door_right':
				# Make sure we're facing right.
				transform.x.x = movement_component.RIGHT
				movement_state_machine.change_to_known_state('enter_right')
			elif the_thing._transition_type == 'door_left':
				transform.x.x = movement_component.LEFT
				movement_state_machine.change_to_known_state('enter_right')
			the_thing.trigger_interaction()
	if the_thing is ItemPickup:
		player_inventory.add_to_inventory(the_thing.item, the_thing.count)
		#player_inventory.select_secondary(the_thing.item)
		the_thing.trigger_interaction()
	return true

var state_to_item_map :Dictionary = {
	"sword": "attack_sword" ,
	"gun": "attack_shoot",
	"punch" : "attack_punch", 
	"mushroom" : "attack_shoot"
}

##TODO: We need to find a way to 'use' the item.
func primary_item() -> String:
#	if player_inventory.primary_selection.consumable == false:
#		if state_to_item_map.has(player_inventory.primary_selection.name):
#			player_inventory.remove_from_inventory(player_inventory.primary_selection)
#			return state_to_item_map[player_inventory.primary_selection.name]

	if player_inventory.primary_selection != null: # Have to make sure we even have a second
		var _item_name :String = player_inventory.primary_selection.name
		if state_to_item_map.has(_item_name):
			player_inventory.remove_from_inventory(player_inventory.primary_selection)
			return state_to_item_map[_item_name]

	return ''

func secondary_item() -> String:
	if player_inventory.secondary_selection != null: # Have to make sure we even have a second
		var _item_name :String = player_inventory.secondary_selection.name
		if state_to_item_map.has(_item_name):
			player_inventory.remove_from_inventory(player_inventory.secondary_selection)
			return state_to_item_map[_item_name]

	return ''

#func check_state_change(_new_state_name: String) -> bool:
#	match _new_state_name:
#		"attack_sword":
#			print("nope.")
#			return false
#		"attack_shoot":
#			return true
#		_: # None
#			return true
#	return true
