import SwiftUI

struct TagOverviewDetailView: View {

    @Binding
    var language: ContentLanguage

    @EnvironmentObject
    private var content: Content

    private var transferImage: (language: ContentLanguage, image: FileResource)? {
        content.tagOverview?.localized(in: language.next).linkPreview.image.map { (language.next, $0) }
    }

    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                if let tag = content.tagOverview?.localized(in: language) {
                    LocalizedTagDetailView(
                        tag: tag,
                        transferImage: transferImage)
                        .id(language)
                } else {
                    Button("Create", action: createTagOverviewPage)
                }
            }
            .padding()
        }
    }

    private func createTagOverviewPage() {
        content.tagOverview = Tag(
            content: content,
            id: "all-tags",
            german: .init(content: content, urlComponent: "alle", name: "Alle Tags"),
            english: .init(content: content, urlComponent: "all", name: "All tags"))
    }
}
