class_name InventoryManager
extends Resource
## Currently doing this in a weird way where the inventory items are the 
## dictionary keys. This would make world inventory items ownable
## by the resourse itself. At least that's what I think. We'll see.
## I'm interested in an approach where the game world items aren't
## duplicated. 


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

# count of items in dictionary value
var _items :Dictionary


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.
	
func select_primary(_item :Item) -> void:
	if _items.has(_item):
		primary_selection = _item

func switch_primary() -> Item:
	## Don't switch unless there is another item
	if primary_selection != null and  _items.size() > 1:
		var found_item :bool = false
		var itemarr = _items.keys()
		for i in itemarr:
			## Item found in last iteration, grab the next one.
			if found_item:
				if i == secondary_selection:
					break
				primary_selection = i
				return i
			if i == primary_selection:
				found_item = true
		## Item found but was the last in the list, grab the first one.
		if found_item and itemarr[0] != primary_selection:
			if itemarr[0] != secondary_selection:
				primary_selection = itemarr[0]
				return itemarr[0]
	## Swap primary and secondary 
	if secondary_selection != null:
		var i = secondary_selection
		if primary_selection != null:
			secondary_selection = primary_selection
		primary_selection = i
		return i
	## Nothing changed 
	return null

func switch_secondary() -> Item:
	## Don't switch unless there is another item
	if secondary_selection != null and  _items.size() > 1:
		var found_item :bool = false
		var itemarr = _items.keys()
		for i in itemarr:
			## Item found in last iteration, grab the next one.
			if found_item:
				if i == primary_selection:
					break
				secondary_selection = i
				return i
			if i == secondary_selection:
				found_item = true
		## Item found but was the last in the list, grab the first one.
		if found_item and itemarr[0] != secondary_selection:
			if itemarr[0] != primary_selection:
				secondary_selection = itemarr[0]
				return itemarr[0]
	## Swap primary and secondary 
	if primary_selection != null:
		var i = primary_selection
		if secondary_selection != null:
			primary_selection = secondary_selection
		secondary_selection = i
		return i
	## Nothing changed 
	return null

func select_secondary(_item :Item) -> void:
	if _items.has(_item):
		secondary_selection = _item

func add_to_inventory(_item :Item, _count :int = 1) -> int:
	if _items.has(_item):
		_items[_item] += _count
	else:
		_items[_item] = _count
	if primary_selection == null:
		primary_selection = _item
	elif secondary_selection == null:
		secondary_selection = _item
	return _items[_item]

func remove_from_inventory (_item :Item) -> int:
	if _items.has(_item):
		if _item.consumable:
			## Reduce inventory for item
			_items[_item] -= 1
			if _items[_item] == 0: ## Item count 0, no inentory
				if primary_selection == _item:
					primary_selection = null
				if secondary_selection == _item:
					secondary_selection = null
				_items.erase(_item) ## Remove it
				return 0
			else: 
				return _items[_item] ## Return item count
		else:
			## Non consumables don't get cleared out.
			return 1
	else:
		return 0

func get_item_inventory_count(_item :Item) -> int:
	if _items.has(_item):
		return _items[_item]
	return 0

func clear_item_from_inventory (_item :Item) -> void:
	if _items.has(_item):
		_items.erase(_item)


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass
