extends Popup

var dragging : bool = false

var item : Item
@onready var image : TextureRect = %Image
@onready var item_name : Label = %ItemName
@onready var item_type : Label = %ItemType
@onready var item_grade : Label = %ItemGrade
@onready var primary_stat : Label = %PrimaryStat
@onready var secondary_stats : Label = %SecondaryStats
@onready var brief : Label = %Brief


func setup(itm : Item) -> void:
	item = itm
	title = item.name + " Information"
	image.texture = item.image
	item_name.text = item.name
	item_type.text = item.item_type_name()
	item_grade.text = item.grade
	primary_stat.text = item.primary_stat()
	#TODO: Implement item secondary stats meaningfully
	var ss_string = ""
	var ss_count = 0
	var pp_list : Array = item.stats.get_property_list() 
	print(item.stats)
	print(item.stats.INT)
	for stat in pp_list.slice(9, len(pp_list)-1):
		var val = item.stats.get(stat.name)
		if val:
			ss_string += stat.name + " " + ("+" if val > 0 else "") 
			ss_string += str(val) + ("\n" if ss_count % 2 else "    ")
			ss_count += 1
	if ss_count > 0:
		secondary_stats.text = ss_string
	brief.text = item.brief
	
func _on_drag_region_gui_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			dragging = event.pressed
	elif dragging and event is InputEventMouseMotion:
		position += Vector2i(event.screen_relative)
			
