package com.base.pdfreaderallpdfreader.utils

import android.content.Context
import android.content.res.AssetManager
import java.io.BufferedReader
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader


object AssetUtils {

    fun Context.readByteArrayFromAsset(fileName: String): ByteArray {
        val assetManager: AssetManager = this.assets
        var ins: InputStream? = null
        val byteStream = ByteArrayOutputStream()

        try {
            ins = assetManager.open(fileName)
            var nextValue: Int
            while (ins.read().also { nextValue = it } != -1) {
                byteStream.write(nextValue)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                ins?.close()
                byteStream.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        return byteStream.toByteArray()
    }

    fun Context.getDemoPdf(): String {
        val demoFile = File(filesDir, "demo")
        demoFile.mkdirs()
        val file = File(demoFile, "DEMO.pdf")
        return file.absolutePath
    }

    fun Context.getDemoDocx(): String {
        val demoFile = File(filesDir, "demo")
        demoFile.mkdirs()
        val file = File(demoFile, "DEMO.docx")
        return file.absolutePath
    }

    fun Context.getDemoXlsx(): String {
        val demoFile = File(filesDir, "demo")
        demoFile.mkdirs()
        val file = File(demoFile, "DEMO.xlsx")
        return file.absolutePath
    }

    fun Context.getDemoPptx(): String {
        val demoFile = File(filesDir, "demo")
        demoFile.mkdirs()
        val file = File(demoFile, "DEMO.pptx")
        return file.absolutePath
    }

    fun Context.saveAssetsFile() {
        val demoFile = File(filesDir, "demo")
        demoFile.mkdirs()
        arrayOf(
            "DEMO.pdf",
            "DEMO.docx",
            "DEMO.xlsx",
            "DEMO.pptx"
        ).forEach {

            val file = File(demoFile, it)
            if (file.exists() && file.length() != 0L) {
                return
            }
            file.createNewFile()

            try {
                FileOutputStream(file).use { fos -> fos.write(readByteArrayFromAsset(it)) }
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }
    }

    fun Context.readJsonFromAsset(fileName: String): String {
        var json = ""
        try {
            val assetManager = this.assets
            val inputStream = assetManager.open(fileName)
            val reader = BufferedReader(InputStreamReader(inputStream))

            var line: String?
            val jsonString = StringBuilder()
            while (reader.readLine().also { line = it } != null) {
                jsonString.append(line)
            }

            reader.close()
            inputStream.close()

            json = jsonString.toString()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return json
    }
}