package ru.forbion.f7cloud.feature.files

import java.util.Locale

enum class OpenableKind {
    IMAGE,
    VIDEO,
    AUDIO,
    PDF,
}

object OpenableFiles {
    private val IMAGE_EXT = setOf("jpg", "jpeg", "png", "gif", "webp", "bmp", "heic", "heif", "svg")
    private val VIDEO_EXT = setOf("mp4", "webm", "mkv", "mov", "3gp", "m4v", "avi", "ts")
    private val AUDIO_EXT = setOf("mp3", "aac", "ogg", "oga", "opus", "wav", "flac", "m4a", "weba")
    private val PDF_EXT = setOf("pdf")

    fun kind(name: String): OpenableKind? {
        val ext = name.lowercase(Locale.ROOT).substringAfterLast('.', missingDelimiterValue = "")
        return when (ext) {
            in IMAGE_EXT -> OpenableKind.IMAGE
            in VIDEO_EXT -> OpenableKind.VIDEO
            in AUDIO_EXT -> OpenableKind.AUDIO
            in PDF_EXT -> OpenableKind.PDF
            else -> null
        }
    }

    fun isOpenable(name: String): Boolean = kind(name) != null || ArchiveFiles.isArchive(name)

    fun isImage(name: String): Boolean = kind(name) == OpenableKind.IMAGE
    fun isVideo(name: String): Boolean = kind(name) == OpenableKind.VIDEO
    fun isAudio(name: String): Boolean = kind(name) == OpenableKind.AUDIO
    fun isPdf(name: String): Boolean = kind(name) == OpenableKind.PDF

    fun hint(name: String): String? = when (kind(name)) {
        OpenableKind.IMAGE -> "Просмотр"
        OpenableKind.VIDEO -> "Видео"
        OpenableKind.AUDIO -> "Аудио"
        OpenableKind.PDF -> "PDF"
        null -> if (ArchiveFiles.isArchive(name)) "Открыть с помощью…" else null
    }
}
