package com.base.datarecovery.utils

import com.base.datarecovery.help.FileHelp
import java.io.File

object FileHexEx {
    private val TAG = "FileHexEx"

    fun isImage(file: File): Boolean {
        return isWebp(file) || isPngOrJpg(file)
    }

    fun isPngOrJpg(file: File): Boolean {
        val pngHex = "89504E47"
        val jpgHex1 = "FFD8FFE0"
        val jpgHex2 = "FFD8FFE1"
        val hex = FileHelp.readFileRange(file.absolutePath, 0, 4).byteArrayToHexString()
        return (hex == pngHex || hex == jpgHex1 || hex == jpgHex2)
    }

    fun isWebp(file: File): Boolean {
        val webpHex = "57454250"
        val hex = FileHelp.readFileRange(file.absolutePath, 8, 11).byteArrayToHexString()
        return hex == webpHex
    }

    fun isDocument(file: File): Boolean {
        return isPdf(file) ||
                isDocx(file) ||
                isXlsx(file) ||
                isPPTX(file) ||
                isXls(file) ||
                isPPT(file) ||
                isDoc(file)
    }

    fun isDoc(file: File): Boolean {
        val docHex = "57006F007200640044006F00630075006D0065006E007400"
        val hex = FileHelp.readFileRange(file.absolutePath, 1408, 1432).byteArrayToHexString()
        val flag = hex == docHex
//        LogEx.logDebug(TAG, "flag=$flag hex=${hex} ${file.absolutePath}")
        return flag
    }

    fun isPPT(file: File): Boolean {
        val pptHex = "50006F0077006500720050006F0069006E0074"
        val hex = FileHelp.readFileRange(file.absolutePath, 1408, 1427).byteArrayToHexString()
        val flag = pptHex == hex
//        LogEx.logDebug(TAG, "flag=$flag hex=${hex} ${file.absolutePath}")
        return flag
    }

    fun isPdf(file: File): Boolean {
        val pdfHex = "25504446"
        val hex = FileHelp.readFileRange(file.absolutePath, 0, 4).byteArrayToHexString()
        return pdfHex == hex
    }

    fun isXls(file: File): Boolean {
        val xlsHex = "4500540045007800740044006100740061"
        val hex = FileHelp.readFileRange(file.absolutePath, 1280, 1297).byteArrayToHexString()
        val flag = hex == xlsHex
//        LogEx.logDebug(TAG, "flag=$flag hex=${hex} ${file.absolutePath}")
        return flag
    }

    fun isDocx(file: File): Boolean {
        val docxHex = "6F6350726F70732F504B"
        val hex = FileHelp.readFileRange(file.absolutePath, 31, 41).byteArrayToHexString()
        val flag = docxHex == hex
        LogEx.logDebug(TAG, "flag=$flag hex=${hex} ${file.absolutePath}")
        return flag
    }


    fun isPPTX(file: File): Boolean {
        val pptxHex = "6CCD97CB6EDB301045F705FA0F02B785"
        val hex = FileHelp.readFileRange(file.absolutePath, 48, 64).byteArrayToHexString()
        val flag = hex == pptxHex
        LogEx.logDebug(TAG, "flag=$flag hex=${hex} ${file.absolutePath}")
        return flag
    }

    fun isXlsx(file: File): Boolean {
        val xlsxHex = "6CCD94CD6A02311485F785BE43C8B6CC"
        val hex = FileHelp.readFileRange(file.absolutePath, 48, 64).byteArrayToHexString()
        val flag = hex == xlsxHex
        LogEx.logDebug(TAG, "flag=$flag hex=${hex} ${file.absolutePath}")
        return flag
    }

    fun isVideo(file: File): Boolean {
        return isMp4(file) || isAvi(file) || isMkv(file)
    }

    fun isMp4(file: File): Boolean {
        val mp4Hex = "6D70343269736F6D000061316D6F6F76"
        val hex = FileHelp.readFileRange(file.absolutePath, 16, 32).byteArrayToHexString()
        val flag = hex == mp4Hex
//        LogEx.logDebug(TAG, "flag=$flag hex=${hex} ${file.absolutePath}")
        return flag
    }

    fun isAvi(file: File): Boolean {
        val aviHex = "524946461C1BA900415649204C495354"
        val hex = FileHelp.readFileRange(file.absolutePath, 0, 16).byteArrayToHexString()
        val flag = hex == aviHex
        LogEx.logDebug(TAG, "flag=$flag hex=${hex} ${file.absolutePath}")
        return flag
    }

    fun isMkv(file: File): Boolean {
        val mkvHex1 = "42F7810142F2810442F381084282886D"
        val hex1 = FileHelp.readFileRange(file.absolutePath, 16, 32).byteArrayToHexString()
        val flag1 = hex1 == mkvHex1
        LogEx.logDebug(TAG, "flag1=$flag1 hex=${hex1} ${file.absolutePath}")
        return if (flag1) {
            true
        } else {
            val mkvHex2 = "42F7810142F2810442F381084282886D6174726F736B614287"
            val hex2 = FileHelp.readFileRange(file.absolutePath, 9, 34).byteArrayToHexString()
            val flag2 = hex2 == mkvHex2
            LogEx.logDebug(TAG, "flag2=$flag2 hex=${hex2} ${file.absolutePath}")
            flag2
        }

    }


}