import SwiftUI

struct TagOverviewDetailView: View {

    @Environment(\.language)
    private var language

    @EnvironmentObject
    private var content: Content

    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                DetailTitle(
                    title: "Tag Overview",
                    text: "Configure the page showing all tags")

                if let page = content.tagOverview?.localized(in: language) {
                    TagOverviewDetails(page: page)
                        .id(language)
                } else {
                    Button("Create", action: createTagOverviewPage)
                }
            }
            .padding()
        }
    }

    private func createTagOverviewPage() {
        content.tagOverview = TagOverviewPage(
            content: content,
            german: .init(content: content, title: "Alle Tags", urlString: "alle"),
            english: .init(content: content, title: "All tags", urlString: "all"))
    }
}

private struct TagOverviewDetails: View {

    @EnvironmentObject
    private var content: Content

    @ObservedObject
    var page: LocalizedTagOverviewPage

    var body: some View {
        VStack(alignment: .leading) {
            StringPropertyView(
                title: "Title",
                text: $page.title,
                footer: "The title of the overview page")

            IdPropertyView(
                id: $page.urlComponent,
                title: "Page URL String",
                footer: "The url component to use for the link to the page",
                validation: page.isValid,
                update: { page.urlComponent = $0 })

            OptionalStringPropertyView(
                title: "Preview Title",
                text: $page.linkPreviewTitle,
                prompt: page.title,
                footer: "The title to use for the page when linking to it")

            OptionalImagePropertyView(
                title: "Preview Image",
                selectedImage: $page.linkPreviewImage,
                footer: "The image to show for previews of this page")

            OptionalTextFieldPropertyView(
                title: "Preview Description",
                text: $page.linkPreviewDescription,
                footer: "The description to show in previews of the page")
        }
    }
}
