package com.kdanmobile.reader.thumb

import android.os.Environment
import java.io.File
import kotlin.collections.ArrayList

object FileUtil {
    val myFolder = "KdanPDFReader"
    val EXTENSION_SEPARATOR = '.'
    private val UNIX_SEPARATOR = '/'
    private val NOT_FOUND = -1
    private val WINDOWS_SEPARATOR = '\\'

    fun generateSelectedPagesString(selectPage: ArrayList<Int>): String {
        val pages = StringBuffer()
        for (n in selectPage) {
            val position = n + 1
            pages.append(position).append(",")
        }
        pages.deleteCharAt(pages.length - 1)
        return pages.toString()
    }

    fun createExtractBlankFile(fileName: String, pages: String, folder: File?): File {
        val ext = getExtension(fileName)
        val name = removeExtension(fileName) + String.format("(page-%s)", pages)
        var file = File(folder, String.format("%s.%s", name, ext))
        var i = 2
        while (file.exists()) {
            file = File(folder, String.format("%s(%d).%s", name, i, ext))
            i++
        }
        return file
    }

    @Synchronized
    fun getMyFile(): File? {
        var myFile: File? = null
        if (myFile == null) {
            myFile = File(getSdCardFile(), myFolder)
        }
        if (myFile.exists()) {
            myFile.mkdirs()
        }
        return myFile
    }

    private fun getSdCardFile(): File? {
        var sdcardFile: File? = null
        if (sdcardFile == null) {
            sdcardFile = Environment.getExternalStorageDirectory()
        }
        return sdcardFile
    }

    fun getExtension(filename: String?): String? {
        if (filename == null) {
            return null
        }
        val index = indexOfExtension(filename)
        return if (index == NOT_FOUND) {
            ""
        } else {
            filename.substring(index + 1)
        }
    }

    fun indexOfExtension(filename: String?): Int {
        if (filename == null) {
            return NOT_FOUND
        }
        val extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR)
        val lastSeparator = indexOfLastSeparator(filename)
        return if (lastSeparator > extensionPos) NOT_FOUND else extensionPos
    }

    fun indexOfLastSeparator(filename: String?): Int {
        if (filename == null) {
            return NOT_FOUND
        }
        val lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR)
        val lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR)
        return Math.max(lastUnixPos, lastWindowsPos)
    }

     private fun removeExtension(filename: String?): String? {
        if (filename == null) {
            return null
        }
        failIfNullBytePresent(filename)

        val index = indexOfExtension(filename)
        return if (index == NOT_FOUND) {
            filename
        } else {
            filename.substring(0, index)
        }
    }

    private fun failIfNullBytePresent(path: String) {
        val len = path.length
        for (i in 0 until len) {
            if (path[i].toInt() == 0) {
                throw IllegalArgumentException("Null byte present in file/path name. There are no " + "known legitimate use cases for such data, but several injection attacks may use it")
            }
        }
    }
}