package com.kdanmobile.reader.thumb

import java.io.*
import kotlin.collections.ArrayList

object FileUtil {
    private var readerFolder: File? = null
    private const val EXTENSION_SEPARATOR = '.'
    private const val UNIX_SEPARATOR = '/'
    private const val NOT_FOUND = -1
    private const val WINDOWS_SEPARATOR = '\\'

    @Synchronized
    fun init(folder: File) {
        if (!folder.exists() || !folder.isDirectory) {
            folder.mkdirs()
        }
        readerFolder = folder
    }

    @Synchronized
    fun getKdanPDFReaderFolder(): File? {
        return readerFolder
    }

    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
    }

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

    private 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
    }

    private 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)
    }

    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")
            }
        }
    }
}