import SwiftUI


struct TagDetailView: View {

    @Environment(\.language)
    private var language

    @ObservedObject
    var tag: Tag

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

    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                DetailTitle(
                    title: "Tag",
                    text: "A tag groups posts and pages together based on a common theme.")

                BoolPropertyView(
                    title: "Appears in overviews",
                    value: $tag.isVisible,
                    footer: "Indicate if the tag should appear in the tag list of posts and pages. If the tag is not visible, then it can still be used as a filter.")

                IdPropertyView(
                    id: $tag.id,
                    title: "Tag id",
                    footer: "The unique id of the tag for references",
                    validation: tag.isValid) {
                        tag.update(id: $0)
                    }

                LocalizedTagDetailView(
                    tag: tag.localized(in: language),
                    transferImage: transferImage)
                .id(tag.id + language.rawValue)
            }
            .padding()
        }
    }
}

extension TagDetailView: MainContentView {

    init(item: Tag) {
        self.tag = item
    }

    static let itemDescription = "a tag"
}
