extends Control

onready var global: Node = get_node("/root/Global")
onready var animation_player: AnimationPlayer = $AnimationPlayer

onready var slots_container: GridContainer = $Slots
onready var slots: Array = slots_container.get_children()
var current_slot: int setget set_current_slot

onready var item_scene = preload("res://Inventory/Item.tscn")

enum Items {
	cartridge,
	vynil,
	key,
	crystal,
	cheese,
	rec_cartridge,
	shovel,
	note
}

func _ready():
	#add_item(Items.cheese)
	#add_item(Items.note)
	#add_item(Items.cartridge)
# warning-ignore:return_value_discarded
	#add_item(Items.rec_cartridge)
	#add_item(Items.vynil)
	for i in slots.size():
		slots[i].inventory = self

func set_current_slot(value):
	current_slot = value
	if current_slot != null:
		global.use_item = true
		animation_player.play("Hide")

func add_item(id: int) -> bool:
	for i in slots.size():
		if slots[i].get_child_count() == 0: # Slot is empty
			var inst = item_scene.instance()
			inst.frame = id
			slots[i].add_child(inst)
			print("Added ", slots[i])
			return true
	return false

func remove_item(id: int) -> bool:
	for i in slots.size():
		if slots[i].get_child_count() == 0:
			continue
		var item: Sprite = slots[i].get_child(0)
		if item == null:
			continue
		if item.frame == id:
			slots[i].remove_child(item)
			print("Removed ", slots[i])
			return true
	print("Not found ", id)
	return false
