import SwiftUI

struct ListBoxContent: View {
    let formId: String
    let widget: WidgetState
    @Environment(ConnectionManager.self) private var manager

    var body: some View {
        if !widget.label.isEmpty {
            Text(widget.label)
                .font(.headline)
        }
        ForEach(widget.items.indices, id: \.self) { index in
            Button {
                widget.selectedIndex = index
                manager.sendFormEvent(
                    formId: formId, widgetId: widget.id,
                    eventType: .listBoxItemClicked,
                    value: .uint(UInt64(index))
                )
            } label: {
                HStack {
                    Text(widget.items[index])
                    Spacer()
                    if index == widget.selectedIndex {
                        Image(systemName: "checkmark")
                            .foregroundStyle(.tint)
                    }
                }
            }
            .accessibilityAddTraits(index == widget.selectedIndex ? .isSelected : [])
            .disabled(!widget.enabled)
        }
    }
}
