class_name InventoryUI extends Control

@export var inventory: Inventory
@export var item_scene: PackedScene
@export var carousel: Carousel

func _ready() -> void:
    _build_carousel()
    inventory.updated.connect(_build_carousel)

func _build_carousel():
    Utils.remove_children(carousel)

    for instance in inventory:
        print('pussy', instance)
        var scene = create_item()
        bind_item(scene, Option.from(instance.item))
        carousel.add_child(scene)

func create_item() -> Node:
    return item_scene.instantiate()

func bind_item(node: Node, item: Option):
    if node.has_method('bind'):
        node.bind(item)

func _gui_input(event: InputEvent) -> void:
    if event.is_action_pressed(PlayerInput.UIAction.Right):
        move_right()
    elif event.is_action_pressed(PlayerInput.UIAction.Left):
        move_left()

func move_by(delta: int):
    var next_index = carousel.get_carousel_index() + delta
    carousel.go_to_index(next_index)

func move_right():
    move_by(1)

func move_left():
    move_by(-1)
