
struct ImageCompareCommand: CommandProcessor {

    static let commandType: CommandType = .imageCompare

    let content: Content

    let results: PageGenerationResults

    let language: ContentLanguage

    init(content: Content, results: PageGenerationResults, language: ContentLanguage) {
        self.content = content
        self.results = results
        self.language = language
    }

    func process(_ arguments: [String], markdown: Substring) -> String {
        guard arguments.count == 2 else {
            invalid(markdown)
            return ""
        }
        let leftImageId = arguments[0]
        let rightImageId = arguments[1]
        guard let leftImage = content.image(leftImageId) else {
            results.missing(file: leftImageId, source: "Image compare")
            return ""
        }
        guard let rightImage = content.image(rightImageId) else {
            results.missing(file: rightImageId, source: "Image compare")
            return ""
        }

        let size = content.settings.pages.contentWidth

        let leftImageSet = leftImage.imageSet(
            width: size, height: size,
            language: language,
            extraAttributes: ImageCompare.extraAttributes)

        let rightImageSet = rightImage.imageSet(
            width: size, height: size,
            language: language,
            extraAttributes: ImageCompare.extraAttributes)

        results.require(imageSet: leftImageSet)
        results.require(imageSet: rightImageSet)
        results.require(icon: ImageCompare.requiredIcon)
        results.require(headers: .imageCompareJs, .imageCompareCss)

        return ImageCompare(left: leftImageSet, right: rightImageSet).content
    }
}
