extends MarginContainer


export var slot_index := 0
export(String) var inv_comp_name := ""
export(String) var category := ""

var item_struct: IItem
var stack_amount := 0

var inv_comp = null

onready var ui_stackamount: Label = get_node("Background/Overlay/StackAmount")
onready var ui_image: TextureRect = get_node("Background/Overlay/Image")


func _ready():

	if inv_comp_name != "":
		inv_comp = get_tree().get_nodes_in_group(inv_comp_name)[0]
		refresh_slot()
	


func set_inventory_component(_inv_comp):
	inv_comp = _inv_comp
	refresh_slot()


func refresh_slot():
	item_struct = inv_comp.inv_struct_list[slot_index]
	stack_amount = inv_comp.inv_amount_list[slot_index]
	
	if item_struct != null:
		ui_image.texture = item_struct.i_image
		ui_stackamount.text = str(stack_amount)
		
		ui_stackamount.visible = item_struct.i_stackable
	else:
		stack_amount = 0
		inv_comp.inv_amount_list[slot_index] = 0
	
	if stack_amount <= 0:
		stack_amount = 0
		inv_comp.inv_amount_list[slot_index] = stack_amount
		
		ui_image.visible = false
		ui_stackamount.visible = false
		
		if item_struct != null:
			item_struct.queue_free()
	else:
		ui_image.visible = true


func has_category():
	if category == "":
		return false
	else:
		return true
	

func _on_gui_input_signal(event):
	if event is InputEventMouseButton:
		# not event.pressed means released
		if event.button_index == BUTTON_RIGHT and not event.pressed:
			if inv_comp.inv_struct_list[slot_index] != null:
				
				inv_comp.use_item_at_slot(slot_index)
				# If right clicking item from a Container, don't use it, but add it
				#elif interactor_inv_comp.add_to_inventory(item_struct, stack_amount):
				#	inv_comp.inv_struct_list[slot_index] = null
				#	inv_comp.inv_amount_list[slot_index] = 0
				#	refresh_slot()
			else:
				refresh_slot()
