package deckers.thibault.aves.utils

object MimeTypes {
    private const val IMAGE = "image"

    // generic raster
    private const val BMP = "image/bmp"
    const val GIF = "image/gif"
    private const val HEIC = "image/heic"
    private const val HEIF = "image/heif"
    private const val ICO = "image/x-icon"
    private const val JPEG = "image/jpeg"
    private const val PNG = "image/png"
    const val TIFF = "image/tiff"
    private const val WBMP = "image/vnd.wap.wbmp"
    const val WEBP = "image/webp"

    // raw raster
    private const val DNG = "image/x-adobe-dng"

    // vector
    const val SVG = "image/svg+xml"

    private const val VIDEO = "video"

    private const val MP2T = "video/mp2t"
    private const val WEBM = "video/webm"

    fun isImage(mimeType: String?) = mimeType != null && mimeType.startsWith(IMAGE)

    fun isVideo(mimeType: String?) = mimeType != null && mimeType.startsWith(VIDEO)

    fun isMultimedia(mimeType: String?) = when (mimeType) {
        HEIC, HEIF -> true
        else -> isVideo(mimeType)
    }

    // returns whether the specified MIME type represents
    // a raster image format that allows an alpha channel
    fun canHaveAlpha(mimeType: String?) = when (mimeType) {
        BMP, GIF, ICO, PNG, TIFF, WEBP -> true
        else -> false
    }

    // as of Flutter v1.22.0
    fun isSupportedByFlutter(mimeType: String, rotationDegrees: Int?, isFlipped: Boolean?) = when (mimeType) {
        JPEG, GIF, WEBP, BMP, WBMP, ICO, SVG -> true
        PNG -> rotationDegrees ?: 0 == 0 && !(isFlipped ?: false)
        else -> false
    }

    // as of metadata-extractor v2.14.0
    fun isSupportedByMetadataExtractor(mimeType: String) = when (mimeType) {
        WBMP, MP2T, WEBM -> false
        else -> true
    }

    // Glide automatically applies EXIF orientation when decoding images of known formats
    // but we need to rotate the decoded bitmap for the other formats
    // maybe related to ExifInterface version used by Glide:
    // https://github.com/bumptech/glide/blob/master/gradle.properties#L21
    fun needRotationAfterGlide(mimeType: String) = when (mimeType) {
        DNG, HEIC, HEIF, PNG, WEBP -> true
        else -> false
    }

    // Thumbnails obtained from the Media Store are automatically rotated
    // according to EXIF orientation when decoding images of known formats
    // but we need to rotate the decoded bitmap for the other formats
    fun needRotationAfterContentResolverThumbnail(mimeType: String) = when (mimeType) {
        DNG, PNG -> true
        else -> false
    }
}
