extends Control
class_name InventorySlot

signal clicked(item_id: String)
signal right_clicked(item_id: String)
signal hovered(item_id: String)
signal unhovered(item_id: String)

var item_id: String = ""
var is_hovered: bool = false

@onready var item_box: ColorRect = $ItemBox
@onready var hover_highlight: ColorRect = $HoverHighlight

func _ready() -> void:
	hover_highlight.visible = false

func set_item(item_def: ItemDefinition) -> void:
	item_id = item_def.id
	item_box.color = Color(1, 0.6, 0.2, 1)

func set_item_color(color: Color) -> void:
	item_box.color = color

func set_hover(hovered: bool) -> void:
	is_hovered = hovered
	hover_highlight.visible = hovered
	if hovered:
		item_box.color = item_box.color.lightened(0.2)
	else:
		var def = InventoryManager.get_item_definition(item_id)
		if def:
			item_box.color = Color(1, 0.6, 0.2, 1)
		else:
			item_box.color = Color(0.8, 0.8, 0.8, 1)

func _gui_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.button_index == 1 and event.pressed:
			clicked.emit(item_id)
		elif event.button_index == 2:
			right_clicked.emit(item_id)

func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		var rect = get_global_rect()
		var mouse_pos = get_global_mouse_position()
		var was_hovered = is_hovered
		is_hovered = rect.has_point(mouse_pos)
		if is_hovered != was_hovered:
			set_hover(is_hovered)
			if is_hovered:
				hovered.emit(item_id)
			else:
				unhovered.emit(item_id)
