import SwiftUI

struct PostLabelsView: View {

    @ObservedObject
    var post: Post

    @ObservedObject
    var localized: LocalizedPost

    @ObservedObject
    var other: LocalizedPost

    @Environment(\.colorScheme)
    var colorScheme

    @State
    private var showLabelEditor: Bool = false

    var body: some View {
        ScrollView(.horizontal) {
            HStack(spacing: 5) {
                if localized.labels.isEmpty {
                    Text("Labels")
                        .font(.headline)
                }
                ForEach(localized.labels) { label in
                    HStack {
                        PageIconView(icon: label.icon)
                            .frame(maxWidth: 16, maxHeight: 16)
                            .scaleEffect(25/16)
                        Text(label.value)
                    }
                    .padding(.vertical, 2)
                    .padding(.horizontal, 8)
                    .background(colorScheme == .light ? Color.white : Color.black)
                    .cornerRadius(8)
                }
                Button(action: { showLabelEditor = true }) {
                    Image(systemSymbol: .squareAndPencilCircleFill)
                        .resizable()
                        .aspectRatio(1, contentMode: .fit)
                        .frame(height: 22)
                        .foregroundColor(Color.gray)
                        .background(Circle()
                            .fill(Color.white)
                            .padding(1))
                }.buttonStyle(.plain)
                if !other.labels.isEmpty {
                    Button("Transfer") {
                        localized.labels = other.labels.map {
                            // Copy instead of reference
                            ContentLabel(icon: $0.icon, value: $0.value)
                        }
                    }
                }
                if !localized.labels.isEmpty {
                    Button("Copy") {
                        var command = "```labels"
                        for label in localized.labels {
                            command += "\n\(label.icon.rawValue): \(label.value)"
                        }
                        command += "\n```"
                        let pasteboard = NSPasteboard.general
                        pasteboard.clearContents()
                        pasteboard.setString(command, forType: .string)
                    }
                }
                if !post.associatedWorkouts.isEmpty {
                    Button("From workout") {
                        post.updateLabelsFromWorkout()
                    }
                }
            }
            .padding(.vertical, 2)
        }
        .sheet(isPresented: $showLabelEditor) {
            LabelModificationView(labels: $localized.labels)
        }
    }

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

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

private struct LabelModificationView: View {

    @Environment(\.dismiss)
    private var dismiss

    @Binding
    var labels: [ContentLabel]

    var body: some View {
        VStack {
            Text("Labels")
                .font(.title)
            LabelCreationView(labels: $labels)
            Button("Save") {
                dismiss()
            }
        }.padding()
    }
}
