import SwiftUI

struct PathSettingsView: View {

    @Environment(\.language)
    private var language

    @EnvironmentObject
    private var content: Content

    @State
    private var showLoadErrorSheet: Bool = false

    @State
    private var loadErrors: [String] = []

    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                FolderOnDiskPropertyView(
                    title: "Content Folder",
                    folder: $content.storage.contentScope,
                    footer: "The folder where the raw content of the website is stored") { url in
                        content.update(contentPath: url, callback: showLoadErrors)
                    }

                FolderOnDiskPropertyView(
                    title: "Output Folder",
                    folder: $content.storage.outputScope,
                    footer: "The folder where the generated website is stored") { url in
                        content.storage.save(outputPath: url)
                    }

                StringPropertyView(
                    title: "Pages output folder",
                    text: $content.settings.paths.pagesOutputFolderPath,
                    footer: "The path in the output folder where the generated pages are stored")

                StringPropertyView(
                    title: "Tags output folder",
                    text: $content.settings.paths.tagsOutputFolderPath,
                    footer: "The path in the output folder where the generated tag pages are stored")

                StringPropertyView(
                    title: "Files output folder",
                    text: $content.settings.paths.filesOutputFolderPath,
                    footer: "The path in the output folder where the copied files are stored")

                StringPropertyView(
                    title: "Images output folder",
                    text: $content.settings.paths.imagesOutputFolderPath,
                    footer: "The path in the output folder where the generated images are stored")

                StringPropertyView(
                    title: "Videos output folder",
                    text: $content.settings.paths.videosOutputFolderPath,
                    footer: "The path in the output folder where the generated videos are stored")

                StringPropertyView(
                    title: "Audio output folder",
                    text: $content.settings.paths.audioOutputFolderPath,
                    footer: "The path in the output folder where the audio files are stored")

                StringPropertyView(
                    title: "Assets output folder",
                    text: $content.settings.paths.assetsOutputFolderPath,
                    footer: "The path in the output folder where assets are stored")
            }
            .padding()
            .sheet(isPresented: $showLoadErrorSheet) {
                StorageStatusView(isPresented: $showLoadErrorSheet)
            }
        }
    }

    private func showLoadErrors() {
        guard !content.storageErrors.isEmpty else {
            return
        }
        showLoadErrorSheet = true
    }
}

#Preview {
    PathSettingsView()
        .environmentObject(Content.mock)
        .padding()
}
