import Foundation

final class TagOverviewPage: Item {

    static let id = "all-tags"

    @Published
    var german: LocalizedTagOverviewPage

    @Published
    var english: LocalizedTagOverviewPage


    init(content: Content, german: LocalizedTagOverviewPage, english: LocalizedTagOverviewPage) {
        self.german = german
        self.english = english
        super.init(content: content, id: TagOverviewPage.id)
    }

    override var itemType: ItemType {
        .tagOverview
    }

    override func title(in language: ContentLanguage) -> String {
        localized(in: language).title
    }

    override func absoluteUrl(in language: ContentLanguage) -> String {
        makeCleanAbsolutePath(internalPath(for: language))
    }

    func filePathRelativeToOutputFolder(for language: ContentLanguage) -> String {
        makeCleanRelativePath(internalPath(for: language))
    }

    private func internalPath(for language: ContentLanguage) -> String {
        content.settings.paths.tagsOutputFolderPath + "/" + localized(in: language).urlComponent
    }

    func contains(urlComponent: String) -> Bool {
        english.urlComponent == urlComponent || german.urlComponent == urlComponent
    }

    var file: TagOverviewFile {
        .init(german: german.file,
              english: english.file)
    }
}

extension TagOverviewPage: LocalizedItem {

}

final class LocalizedTagOverviewPage: ObservableObject {

    unowned let content: Content

    @Published
    var title: String

    /**
     The string to use when creating the url for the page.
     */
    @Published
    var urlComponent: String

    @Published
    var linkPreviewImage: FileResource?

    @Published
    var linkPreviewTitle: String?

    @Published
    var linkPreviewDescription: String?

    init(content: Content, title: String, urlString: String, linkPreviewImage: FileResource? = nil, linkPreviewTitle: String? = nil, linkPreviewDescription: String? = nil) {
        self.content = content
        self.title = title
        self.urlComponent = urlString
        self.linkPreviewImage = linkPreviewImage
        self.linkPreviewTitle = linkPreviewTitle
        self.linkPreviewDescription = linkPreviewDescription
    }

    init(content: Content, file: LocalizedTagOverviewFile, image: FileResource?) {
        self.content = content
        self.title = file.title
        self.urlComponent = file.url
        self.linkPreviewImage = image
        self.linkPreviewTitle = file.linkPreviewTitle
        self.linkPreviewDescription = file.linkPreviewDescription
    }

    var file: LocalizedTagOverviewFile {
        .init(url: urlComponent,
              title: title,
              linkPreviewImage: linkPreviewImage?.id,
              linkPreviewTitle: linkPreviewTitle,
              linkPreviewDescription: linkPreviewDescription)
    }

    func isValid(urlComponent: String) -> Bool {
        !urlComponent.isEmpty &&
        content.isValidIdForTagOrPageOrPost(urlComponent) &&
        !content.containsTag(withUrlComponent: urlComponent)
    }
}
