from shlex import quote

from ..compressor import Compressor


class JPEGCompressor(Compressor):
    @classmethod
    def get_file_type(cls) -> str:
        return "jpeg"

    def build_command(self, result_item) -> str:
        jpegoptim = "jpegoptim --max={} -o --stdout {} > {}"
        jpegoptim2 = "jpegoptim -o --stdout {} > {}"

        if self.settings.jpg_progressive:
            jpegoptim += " --all-progressive"
            jpegoptim2 += " --all-progressive"

        if not self.settings.metadata:
            jpegoptim += " --strip-all"
            jpegoptim2 += " --strip-all"

        if self.settings.file_attributes:
            jpegoptim += " --preserve --preserve-perms"
            jpegoptim2 += " --preserve --preserve-perms"

        if self.settings.lossy:  # lossy compression
            command = jpegoptim.format(
                self.settings.jpg_lossy_level,
                quote(result_item.filename),
                quote(result_item.tmp_filename),
            )
        else:  # lossless compression
            command = jpegoptim2.format(
                quote(result_item.filename), quote(result_item.tmp_filename)
            )
        return command
