import Foundation

struct FeedEntry {

    private let data: FeedEntryData

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

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

    var content: String {
        var result = "<article><div class='card\(cardLinkClassText)'>"
        ImageGallery(id: data.entryId, images: data.images).populate(&result)

        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>"
        }
        result += TagList(tags: data.tags).content

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