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
    }


}