class_name Inventory
const Item = preload("res://item/Item.gd")
const ItemStack = preload("res://item/ItemStack.gd")

var items:Array[ItemStack] = []

enum InventorySortType {
  ITEM_TYPE,
  # ITEM_KEY
}

signal itemAdded(item:Item.Id)
signal itemRemoved(item:Item.Id)
signal itemQuantityChanged(item:Item.Id, quantity:int)
signal inventorySorted(sortBy:InventorySortType, reverse:bool)

func setItem(item:Item.Id, quantity:int) -> void:
  if quantity < 0:
    push_error("Cannot set item quantity to negative value, using 0")
    quantity = 0

  # Is item already in inventory?
  for itemStack in items:
    if itemStack.item != item:
      continue
    
    # Item stack already present.
    if quantity == 0:
      items.erase(itemStack)
      itemRemoved.emit(item)
      itemQuantityChanged.emit(item, 0)
    else:
      itemStack.quantity = quantity
      itemQuantityChanged.emit(item, quantity)
    return

  # Not in stack, create new stack
  var stack:ItemStack = ItemStack.new()
  stack.item = item
  stack.quantity = quantity
  items.append(stack)
  itemAdded.emit(item)
  itemQuantityChanged.emit(item, quantity)

func getItemQuantity(item:Item.Id) -> int:
  for itemStack in items:
    if itemStack.item == item:
      return itemStack.quantity
  return 0

func addItem(item:Item.Id, quantity:int = 1) -> void:
  # Add can only take positive quantities, otherwise use set or remove
  if quantity <= 0:
    push_error("Cannot add non-positive item quantity")
    return
  self.setItem(item, self.getItemQuantity(item) + quantity)

func addStack(stack:ItemStack) -> void:
  self.addItem(stack.item, stack.quantity)

func removeItem(item:Item.Id) -> void:
  self.setItem(item, 0)

func sort(sortBy:InventorySortType, reverse:bool = false) -> void:
  match sortBy:
    InventorySortType.ITEM_TYPE:
      items.sort_custom(_sortByItemType)
    # InventorySortType.ITEM_KEY:
    #   items.sort_custom(_sortByItemKey)
  
  if reverse:
    items.reverse()

  inventorySorted.emit(sortBy, reverse)

# Sorters
func _sortByItemType(a:ItemStack, b:ItemStack) -> int:
  return int(Item.getType(a.item)) - int(Item.getType(b.item))

# func _sortByItemKey(a:ItemStack, b:ItemStack) -> int:
#   return Item.getItemById(a.item).key.casecmp_to(Item.getItemById(b.item).key)
