extends Area2D
class_name List

var current_item = 0
var mask_list: Array[MaskResource]
var guest_list: Array[CharacterResource]
var items: Array

var pref_score_name: Dictionary[CharacterResource.Preference, String] = {
	CharacterResource.Preference.LOVE: "Loves ",
	CharacterResource.Preference.APPROVE: "Likes ",
	CharacterResource.Preference.DONT_CARE: "Doesn't care about ",
	CharacterResource.Preference.DISAPPROVE: "Disapproves ",
	CharacterResource.Preference.HATE: "Hates ",
}

@onready var chara: Sprite2D = $"../GuestList/Chara"
@onready var mask: Sprite2D = $"../MaskList/Mask"

@onready var label: Label = $Label

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

func set_list(_items):
	print(name)
	for i in _items:
		print(i.name)
	
	current_item = 1
	if _items.size() == 0:
		items = []
		label.text = "...\n...\n..."
		return
	items = _items
	cycle_through_items()


func cycle_through_items() -> void:
	if items.size() == 0:
		label.text = "...\n...\n..."
		return
	
	current_item += 1
	var index = current_item % items.size()
	var item = items.get(index)
	if item is MaskResource:
		mask.visible = true
		mask.texture = item.mask_sprite
		label.text = "\n\n\n\n" + item.name + "\n\n" + item.description
	elif item is CharacterResource:
		chara.visible = true
		chara.texture = item.chara_sprite
		
		label.text = item.name + "\n\nTraits\n"
		for guest_trait: Trait in item.traits:
			label.text += guest_trait.name + "\n"
		label.text += "\n\nPreferences\n"
		for other_trait in item.preferences:
			var pref = item.preferences[other_trait]
			label.text += pref_score_name[pref] + other_trait.name + "\n"
	
	
func _on_mouse_entered() -> void:
	Input.set_default_cursor_shape(Input.CursorShape.CURSOR_POINTING_HAND)


func _on_mouse_exited() -> void:
	Input.set_default_cursor_shape(Input.CursorShape.CURSOR_ARROW)


func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event is InputEventMouseButton and event.is_pressed():
		Input.set_default_cursor_shape(Input.CursorShape.CURSOR_ARROW)
		cycle_through_items()
