import SwiftUI
import SFSafeSymbols

struct ItemSelectionView: View {

    @Binding
    var isPresented: Bool

    @Binding
    var selectedItems: [Item]

    @Environment(\.language)
    private var language

    @EnvironmentObject
    private var content: Content

    init(isPresented: Binding<Bool>, selectedItems: Binding<[Item]>) {
        self._isPresented = isPresented
        self._selectedItems = selectedItems
    }

    var body: some View {
        VStack {
            List {
                Section("Selected") {
                    ForEach(selectedItems) { item in
                        HStack {
                            Image(systemSymbol: .minusCircleFill)
                                .foregroundStyle(.red)
                                .onTapGesture {
                                    deselect(item: item)
                                }
                            Text(item.title(in: language))
                            Spacer()
                        }
                    }
                    .onMove(perform: moveItem)
                }
                if let tagOverview = content.tagOverview {
                    Section("Special Pages") {
                        HStack {
                            Image(systemSymbol: .plusCircleFill)
                                .foregroundStyle(.green)
                            Text("Tags Overview")
                            Spacer()
                        }
                        .contentShape(Rectangle())
                        .onTapGesture {
                            if !selectedItems.contains(where: { $0.itemReference == tagOverview.itemReference }) {
                                selectedItems.append(tagOverview)
                            }
                        }
                    }
                }
                Section("Tags") {
                    ForEach(content.tags) { item in
                        HStack {
                            Image(systemSymbol: .plusCircleFill)
                                .foregroundStyle(.green)
                            Text(item.title(in: language))
                            Spacer()
                        }
                        .contentShape(Rectangle())
                        .onTapGesture {
                            select(item: item)
                        }
                    }
                }
                Section("Pages") {
                    ForEach(content.pages) { item in
                        HStack {
                            Image(systemSymbol: .plusCircleFill)
                                .foregroundStyle(.green)
                            Text(item.title(in: language))
                        }
                        .contentShape(Rectangle())
                        .onTapGesture {
                            select(item: item)
                        }
                    }
                }
            }
            Button("Dismiss", action: dismiss)
        }
        .frame(minHeight: 500)
        .padding()
    }

    private func moveItem(from source: IndexSet, to destination: Int) {
        selectedItems.move(fromOffsets: source, toOffset: destination)
    }

    private func deselect(item: Item) {
        guard let index = selectedItems.firstIndex(of: item) else {
            return
        }
        selectedItems.remove(at: index)
    }

    private func select(item: Item) {
        if selectedItems.contains(item) {
            return
        }
        selectedItems.append(item)
    }

    private func dismiss() {
        isPresented = false
    }
}
