import SwiftUI
import SFSafeSymbols

struct PageDetailView: View {

    @Environment(\.language)
    private var language

    @EnvironmentObject
    private var content: Content

    @ObservedObject
    private var page: Page

    @State
    private var showFileSelectionSheet = false

    init(page: Page) {
        self.page = page
    }

    private var requiredFilesText: String {
        switch page.requiredFiles.count {
        case 0: return "No files"
        case 1: return "1 file"
        default: return "\(page.requiredFiles.count) files"
        }
    }

    #warning("Show info on page generation")
    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                DetailTitle(
                    title: "Page",
                    text: "A page contains longer content")
                HStack(alignment: .firstTextBaseline) {
                    Button(action: generate) {
                        Text("Generate")
                    }
                    .disabled(content.isGeneratingWebsite)
//                    switch didGenerateWebsite {
//                    case .none:
//                        Image(systemSymbol: .questionmarkCircleFill)
//                            .foregroundStyle(.gray)
//                    case .some(true):
//                        Image(systemSymbol: .checkmarkCircleFill)
//                            .foregroundStyle(.green)
//                    case .some(false):
//                        Image(systemSymbol: .xmarkCircleFill)
//                            .foregroundStyle(.red)
//                    }
                }
                IdPropertyView(
                    id: $page.id,
                    footer: "The page id is used to link to it internally.",
                    validation: page.isValid,
                    update: { page.update(id: $0) })

                OptionalStringPropertyView(
                    title: "External url",
                    text: $page.externalLink,
                    footer: "Set an external url to mark this page as external. It will not be generated, and links will be created using the provided url")

                BoolPropertyView(
                    title: "Draft",
                    value: $page.isDraft,
                    footer: "Indicate a page as a draft to hide it from the website")
                .disabled(page.isExternalUrl)
                .onChange(of: page.externalLink) { _, newValue in
                    // Ensure that external pages are not drafts
                    if newValue != nil && page.isDraft {
                        page.isDraft = false
                    }
                }

                BoolPropertyView(
                    title: "Hide date",
                    value: $page.hideDate,
                    footer: "Do not show the date string on the page")
                .disabled(page.isExternalUrl)

                DatePropertyView(
                    title: "Start date",
                    value: $page.startDate,
                    footer: "The date when the page content started")
                .disabled(page.isExternalUrl)

                OptionalDatePropertyView(
                    title: "End date",
                    isEnabled: $page.hasEndDate,
                    date: $page.endDate,
                    footer: "The date when the page content ended")
                .disabled(page.isExternalUrl)

                GenericPropertyView(
                    title: "Required files",
                    footer: "The additional files required by the page") {
                        HStack {
                            Image(systemSymbol: .squareAndPencilCircleFill)
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(height: 20)
                            Text(requiredFilesText)
                            Spacer()
                        }
                        .padding(.vertical, 8)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showFileSelectionSheet = true
                        }
                }

                LocalizedPageDetailView(
                    isExternalPage: page.isExternalUrl,
                    page: page.localized(in: language))
                .id(page.id + language.rawValue)
            }
            .padding()
        }
        .sheet(isPresented: $showFileSelectionSheet) {
            MultiFileSelectionView(selectedFiles: $page.requiredFiles, insertSorted: true)
        }
    }

    private func generate() {
        DispatchQueue.global(qos: .userInitiated).async {
            content.generatePage(page)
        }
    }
}

extension PageDetailView: MainContentView {

    init(item: Page) {
        self.init(page: item)
    }

    static let itemDescription = "a page"
}


#Preview {
    PageDetailView(page: .empty)
}
