import SwiftUI

struct ContentView: View {
    @ObservedObject var materialStore: MaterialStore
    @ObservedObject var normalStore: NormalGeneratorStore
    @State private var selectedTool: StudioTool = .pbr

    var body: some View {
        VStack(spacing: 0) {
            BrandHeaderView(selectedTool: selectedTool)

            Divider()

            TabView(selection: $selectedTool) {
                PBRMaterialView(store: materialStore)
                    .tabItem {
                        Label(StudioTool.pbr.title, systemImage: StudioTool.pbr.systemImage)
                    }
                    .tag(StudioTool.pbr)

                NormalGeneratorView(store: normalStore)
                    .tabItem {
                        Label(StudioTool.normalGenerator.title, systemImage: StudioTool.normalGenerator.systemImage)
                    }
                    .tag(StudioTool.normalGenerator)
            }
        }
        .toolbar {
            ToolbarItemGroup {
                switch selectedTool {
                case .pbr:
                    Button {
                        materialStore.buildPreview()
                    } label: {
                        Label("Build Preview", systemImage: "arrow.triangle.2.circlepath")
                    }
                    .disabled(!materialStore.hasAnyTexture)
                    .help("Build PBR Preview")

                    Button {
                        materialStore.exportMaterialSet()
                    } label: {
                        Label("Export", systemImage: "square.and.arrow.up")
                    }
                    .disabled(!materialStore.canExport)
                    .help("Export PBR Set")

                case .normalGenerator:
                    Button {
                        normalStore.buildPreview()
                    } label: {
                        Label("Build Preview", systemImage: "arrow.triangle.2.circlepath")
                    }
                    .disabled(!normalStore.canExport)
                    .help("Build Generated Map Preview")

                    Button {
                        normalStore.exportSet()
                    } label: {
                        Label("Export", systemImage: "square.and.arrow.up")
                    }
                    .disabled(!normalStore.canExport)
                    .help("Export Generated Map Set")
                }
            }
        }
    }
}

private struct BrandHeaderView: View {
    let selectedTool: StudioTool

    var body: some View {
        HStack(spacing: 12) {
            Text("BREDA TECH")
                .font(.title3.weight(.bold))
                .tracking(1.2)

            Divider()
                .frame(height: 18)

            Label(selectedTool.title, systemImage: selectedTool.systemImage)
                .font(.headline)
                .foregroundStyle(.secondary)

            Spacer()
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 10)
        .background(.bar)
    }
}

private struct PBRMaterialView: View {
    @ObservedObject var store: MaterialStore

    var body: some View {
        HSplitView {
            TextureSidebarView(store: store)
                .frame(minWidth: 290, idealWidth: 320, maxWidth: 380)

            SettingsPanelView(store: store)
                .frame(minWidth: 360, idealWidth: 440)

            PreviewPanelView(store: store)
                .frame(minWidth: 320, idealWidth: 380)
        }
    }
}

private struct TextureSidebarView: View {
    @ObservedObject var store: MaterialStore

    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            HStack {
                Text("Textures")
                    .font(.title3.weight(.semibold))
                Spacer()
                Button {
                    store.reset()
                } label: {
                    Image(systemName: "trash")
                }
                .buttonStyle(.borderless)
                .disabled(!store.hasAnyTexture)
                .help("Reset Inputs")
            }
            .padding([.horizontal, .top])
            .padding(.bottom, 8)

            ScrollView {
                LazyVStack(spacing: 10) {
                    ForEach(TextureSlotKind.allCases) { kind in
                        TextureSlotView(store: store, kind: kind)
                    }
                }
                .padding(.horizontal)
                .padding(.bottom)
            }
        }
    }
}

private struct TextureSlotView: View {
    @ObservedObject var store: MaterialStore
    let kind: TextureSlotKind
    @State private var isDropTarget = false

    private var asset: TextureAsset? {
        store.textures[kind]
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            HStack(spacing: 8) {
                Image(systemName: kind.systemImage)
                    .foregroundStyle(.secondary)
                    .frame(width: 18)

                Text(kind.title)
                    .font(.headline)
                    .lineLimit(1)

                if kind.required {
                    Text("Required")
                        .font(.caption2)
                        .foregroundStyle(.secondary)
                        .padding(.horizontal, 6)
                        .padding(.vertical, 2)
                        .background(.tertiary.opacity(0.35), in: Capsule())
                }

                Spacer()

                if asset != nil {
                    Button {
                        store.removeTexture(kind)
                    } label: {
                        Image(systemName: "xmark.circle")
                    }
                    .buttonStyle(.borderless)
                    .help("Remove \(kind.title)")
                }
            }

            if let asset {
                HStack(spacing: 10) {
                    Image(nsImage: asset.preview)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 54, height: 54)
                        .clipShape(RoundedRectangle(cornerRadius: 6))

                    VStack(alignment: .leading, spacing: 3) {
                        Text(asset.fileName)
                            .font(.callout)
                            .lineLimit(1)
                        Text(asset.sizeLabel)
                            .font(.caption)
                            .foregroundStyle(.secondary)
                    }

                    Spacer()

                    Button {
                        store.chooseTexture(for: kind)
                    } label: {
                        Image(systemName: "arrow.triangle.2.circlepath")
                    }
                    .buttonStyle(.borderless)
                    .help("Replace \(kind.title)")
                }
            } else {
                Button {
                    store.chooseTexture(for: kind)
                } label: {
                    Label("Choose", systemImage: "plus")
                        .frame(maxWidth: .infinity)
                }
                .buttonStyle(.bordered)
            }
        }
        .padding(10)
        .background(isDropTarget ? Color.accentColor.opacity(0.18) : Color.secondary.opacity(0.08), in: RoundedRectangle(cornerRadius: 8))
        .overlay(
            RoundedRectangle(cornerRadius: 8)
                .stroke(isDropTarget ? Color.accentColor : Color.secondary.opacity(0.14), lineWidth: 1)
        )
        .help(kind.help)
        .onDrop(of: [.fileURL], isTargeted: $isDropTarget) { providers in
            store.acceptDrop(providers, for: kind)
        }
    }
}
