import SwiftUI

struct SettingsPanelView: View {
    @ObservedObject var store: MaterialStore

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 16) {
                Text("Material")
                    .font(.title3.weight(.semibold))

                GroupBox {
                    VStack(alignment: .leading, spacing: 12) {
                        TextField("Name", text: $store.settings.materialName)
                            .textFieldStyle(.roundedBorder)

                        Picker("Output Size", selection: $store.settings.outputResolution) {
                            ForEach(OutputResolution.allCases) { resolution in
                                Text(resolution.title).tag(resolution)
                            }
                        }
                        .pickerStyle(.segmented)
                    }
                    .padding(.vertical, 4)
                }

                GroupBox("Map Conversion") {
                    VStack(alignment: .leading, spacing: 12) {
                        Toggle("Roughness input is gloss/smoothness", isOn: $store.settings.roughnessInputIsGloss)
                        Toggle("Flip normal green channel", isOn: $store.settings.flipNormalGreen)
                    }
                    .padding(.vertical, 4)
                }

                GroupBox("Missing Map Defaults") {
                    VStack(alignment: .leading, spacing: 14) {
                        SliderRow(title: "AO", value: $store.settings.defaultOcclusion)
                        SliderRow(title: "Roughness", value: $store.settings.defaultRoughness)
                        SliderRow(title: "Metallic", value: $store.settings.defaultMetallic)
                    }
                    .padding(.vertical, 4)
                }

                GroupBox("Export") {
                    VStack(alignment: .leading, spacing: 12) {
                        Toggle("Write .gltf material", isOn: $store.settings.exportGLTF)
                        Toggle("Write notes", isOn: $store.settings.exportNotes)

                        HStack {
                            Button {
                                store.buildPreview()
                            } label: {
                                Label("Preview", systemImage: "eye")
                            }
                            .disabled(!store.hasAnyTexture)

                            Button {
                                store.exportMaterialSet()
                            } label: {
                                Label("Export", systemImage: "square.and.arrow.up")
                            }
                            .buttonStyle(.borderedProminent)
                            .disabled(!store.canExport)
                        }
                    }
                    .padding(.vertical, 4)
                }

                Spacer(minLength: 0)
            }
            .padding()
        }
    }
}

private struct SliderRow: View {
    let title: String
    @Binding var value: Double

    var body: some View {
        Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 10, verticalSpacing: 8) {
            GridRow {
                Text(title)
                    .frame(width: 76, alignment: .leading)
                Slider(value: $value, in: 0...1)
                Text(Formatters.percentLabel(value))
                    .font(.callout.monospacedDigit())
                    .foregroundStyle(.secondary)
                    .frame(width: 44, alignment: .trailing)
            }
        }
    }
}
