import SwiftUI

struct LabelEditingView: View {

    @ObservedObject
    var label: ContentLabel

    @State
    private var showIconPicker: Bool = false

    var body: some View {
        HStack {
            Button(action: { showIconPicker = true }) {
                PageIconView(icon: label.icon)
                    .frame(maxWidth: 20, maxHeight: 20)
                    .contentShape(Rectangle())
            }
            .buttonStyle(.plain)
            TextField("", text: $label.value)
                .textFieldStyle(.plain)
        }
        .sheet(isPresented: $showIconPicker) {
            LabelIconSelectionView(selected: $label.icon)
        }
    }
}

private struct LabelIconSelectionView: View {

    @Environment(\.dismiss)
    var dismiss

    @Binding
    var selected: PageIcon

    var body: some View {
        VStack {
            List(PageIcon.allCases, id: \.rawValue) { icon in
                HStack {
                    Image(systemSymbol: selected == icon ? .checkmarkCircleFill : .circle)
                    PageIconView(icon: icon)
                        .frame(maxWidth: 20, maxHeight: 20)
                    Text(icon.name)
                    Spacer()
                }
                .contentShape(Rectangle())
                .onTapGesture {
                    selected = icon
                    dismiss()
                }
            }.frame(minHeight: 300)
            Button("Done") {
                dismiss()
            }
        }.padding()
    }
}

struct PostLabelsView: View {

    @ObservedObject
    var post: LocalizedPost

    @ObservedObject
    var other: LocalizedPost

    @Environment(\.colorScheme)
    var colorScheme

    var body: some View {
        ScrollView(.horizontal) {
            HStack(spacing: 5) {
                Text("Labels")
                    .font(.headline)
                ForEach(post.labels, id: \.icon) { label in
                    HStack {
                        Button(action: { remove(label) }) {
                            Image(systemSymbol: .minusCircleFill)
                                .foregroundStyle(.red)
                        }
                        .buttonStyle(.plain)
                        LabelEditingView(label: label)
                    }
                    .padding(.vertical, 2)
                    .padding(.horizontal, 8)
                    .background(colorScheme == .light ? Color.white : Color.black)
                    .cornerRadius(8)
                }
                Button("Add", action: addLabel)
                if !other.labels.isEmpty {
                    Button("Transfer") {
                        post.labels = other.labels.map {
                            // Copy instead of reference
                            ContentLabel(icon: $0.icon, value: $0.value)
                        }
                    }
                }
            }
            .padding(.vertical, 2)
        }
    }

    func addLabel() {
        post.labels.append(.init(icon: .clockFill, value: "Value"))
    }

    func remove(_ label: ContentLabel) {
        guard let index = post.labels.firstIndex(of: label) else {
            return
        }
        post.labels.remove(at: index)
    }
}
