import SwiftUI

struct GenerationContentView: View {

    @Environment(\.language)
    private var language

    @EnvironmentObject
    private var content: Content

    @Binding
    private var selectedSection: SettingsSection

    init(selected: Binding<SettingsSection>) {
        self._selectedSection = selected
    }

    var body: some View {
        switch selectedSection {
        case .pages:
            PageSettingsContentView()
        default:
            generationView
        }
    }

    @ViewBuilder
    private var generationView: some View {
            VStack(alignment: .leading) {
                Text("Website Generation")
                    .font(.largeTitle)
                    .bold()
                Text("Regenerate the website and monitor the output")
                    .foregroundStyle(.secondary)
                    .padding(.bottom, 30)

                HStack {
                    Button {
                        if content.isGeneratingWebsite {
                            content.endCurrentGeneration()
                        } else {
                            content.generateWebsiteInAllLanguages()
                        }
                    } label: {
                        Text(content.isGeneratingWebsite ? "Cancel" : "Generate")
                    }
                    .disabled(content.isGeneratingWebsite != content.shouldGenerateWebsite)
                    if content.isGeneratingWebsite {
                        ProgressView()
                            .progressViewStyle(.circular)
                            .frame(height: 25)
                    }
                    Spacer()
                }
                Text(content.generationStatus)
                    .padding(.vertical, 5)
                HStack(spacing: 8) {
                    Text("\(content.results.imagesToGenerate.count) images")
                    Text("\(content.results.externalLinks.count) external links")
                    Text("\(content.results.resultCount) items processed")
                    Text("\(content.results.requiredFiles.count) files")
                }
                List {
                    Section("Inaccessible files (\(content.results.inaccessibleFiles.count))") {
                        ForEach(content.results.inaccessibleFiles.sorted()) { file in
                            Text(file.id)
                        }
                    }
                    Section("Unparsable files (\(content.results.unparsableFiles.count))") {
                        ForEach(content.results.unparsableFiles.sorted()) { file in
                            Text(file.id)
                        }
                    }
                    Section("Missing files (\(content.results.missingFiles.count))") {
                        ForEach(content.results.missingFiles.sorted(), id: \.self) { file in
                            Text(file)
                        }
                    }
                    Section("Missing tags (\(content.results.missingTags.count))") {
                        ForEach(content.results.missingTags.sorted(), id: \.self) { tag in
                            Text(tag)
                        }
                    }
                    Section("Missing pages (\(content.results.missingPages.count))") {
                        ForEach(content.results.missingPages.sorted(), id: \.self) { page in
                            Text(page)
                        }
                    }
                    Section("Invalid commands (\(content.results.invalidCommands.count))") {
                        ForEach(content.results.invalidCommands.sorted(), id: \.self) { markdown in
                            Text(markdown)
                        }
                    }
                    Section("Invalid blocks (\(content.results.invalidBlocks.count))") {
                        ForEach(content.results.invalidBlocks.sorted(), id: \.self) { markdown in
                            Text(markdown)
                        }
                    }
                    Section("Warnings (\(content.results.warnings.count))") {
                        ForEach(content.results.warnings.sorted(), id: \.self) { warning in
                            Text(warning)
                        }
                    }
                    Section("Unsaved output files (\(content.results.unsavedOutputFiles.count))") {
                        ForEach(content.results.unsavedOutputFiles.sorted(), id: \.self) { file in
                            Text(file)
                        }
                    }
                    Section("Empty pages (\(content.results.emptyPages.count))") {
                        ForEach(content.results.emptyPages.sorted()) { id in
                            Text("\(id.pageId) (\(id.language))")
                        }
                    }
                }
            }.padding()
    }
}

#Preview {
    GenerationContentView(selected: .constant(.folders))
        .environmentObject(Content.mock)
        .padding()
}
