import Foundation

struct FeedEntry: HtmlProducer {

    private let data: FeedEntryData

    init(data: FeedEntryData) {
        self.data = data
    }

    private var cardLinkClassText: String {
        data.link != nil ? " linked-card" : ""
    }

    func populate(_ result: inout String) {
        result += "<article><div class='card\(cardLinkClassText)'>"
        switch data.media {
        case .images(let images):
            ImageGallery(id: data.entryId, images: images).populate(&result)
        case .video(let videos):
            PostVideo(videos: videos).populate(&result)
        case .none:
            break
        }

        if let url = data.link?.url {
            result += "<div class='card-content' onclick=\"window.location.href='\(url)'\">"
        } else {
            result += "<div class='card-content'>"
        }
        result += "<h3>\(data.textAboveTitle)</h3>"
        if let title = data.title {
            result += "<h2>\(title.htmlEscaped())</h2>"
        }
        TagList(tags: data.tags).populate(&result)
        ContentLabels(labels: data.labels).populate(&result)

        result += data.text
        if let url = data.link {
            result += "<div class='link-center'><div class='link'>\(url.text)</div></div>"
        }
        result += "</div></div></article>\n" // Closes card-content, card, article
    }
}
