package com.app.input.inputaura

import android.app.Application
import android.graphics.Typeface
import com.ad.toponlibrary.AdManager
import com.app.input.inputaura.dasda.Shikamaru
import com.app.input.inputaura.dasda.Inhj
import com.up.uploadlibrary.UpLoadManager
//import com.pretty.keyboard.theme.keyboard.helper.ObjectBox
import org.json.JSONArray
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.StringWriter

class InputAura : Application() {
    companion object {
        lateinit var inputAuraInstance: InputAura

        lateinit var list: MutableList<Inhj>

        const val TAG = "-----------------"
        var defaultFont: Typeface? = null
        const val DB_VERSION = 2
        const val DB_NAME = "db_name"
    }

    override fun onCreate() {
        super.onCreate()
        inputAuraInstance = this
        defaultFont = Typeface.createFromAsset(assets, "my_font.ttf")
        dealFile()

        UpLoadManager.init(this,"ocean") { _, _ -> }
        AdManager.init("ocean",this,"h69646a7448f89","ae3a442492a7590301acbf6889dee47d4",
            "25e138876a3743aa9037e82dff40dcb657df7a6d",
            "n69646a9f39616",
            "n69646a9eeaebc",
            "n69646a9eb74b9",false)
    }
    private fun dealFile() {
        val openFile = inputAuraInstance.assets.open("res.json")
        val jsonString = getJsonString(openFile)
        if (jsonString != null) {
            resolveJsonString(jsonString)
        }
    }


    private fun resolveJsonString(string: String) {
        val jsonData = JSONArray(string)
        var dataList: MutableList<Inhj> = mutableListOf()
        for (i in 0 until jsonData.length()) {
            jsonData.getJSONObject(i).run {
                val pName = getString("parent_name")
                val listArray = getJSONArray("keyboard_list")
                var shikamaruList: MutableList<Shikamaru> = mutableListOf()
                for (listIndex in 0 until listArray.length()) {
                    listArray.getJSONObject(listIndex).also {
                        val title = it.getString("title")
                        val thUrl = it.getString("thumbUrl")
                        val thGif = it.getString("thumbUrlGif")
                        var zipPath = ""
                        var imgPath = ""
                        var imgGif = ""
                        var imgPreviewGif = ""
                        if (it.has("detail")) {
                            val contentObject =
                                it.getJSONObject("detail").getJSONObject("themeContent")
                            zipPath = contentObject.getString("androidRawZipUrl")
                            imgPath = contentObject.getString("img")
                            imgGif = contentObject.getString("imgGif")
                            imgPreviewGif = contentObject.getString("imgPreviewGif")
                        } else {

                        }
                        shikamaruList.add(
                            Shikamaru().apply {
                            setImgPath(imgPath)
                            setZipPath(zipPath)
                            setTitleName(title)
                            setImgGif(imgGif)
                            thumbUrl = thUrl
                            thumbGif = thGif
                        })

                    }

                }
                val shuffled = shikamaruList.shuffled()
                val dataInhj = Inhj()
                    .apply {
                    parentName = pName
                    keyboardList = shuffled
                }
                dataList.add(dataInhj)

            }
        }

        updateDataList(dataList)

    }

    private fun updateDataList(mainList: MutableList<Inhj>) {
        list = mainList

    }
    private fun getJsonString(fileInputStream: InputStream): String? {
        return try {
            //            FileInputStream fileInputStream = new FileInputStream(path);
            val charArray = CharArray(fileInputStream.available())
            var readCount = 0
            val streamReader = InputStreamReader(fileInputStream)
            val bufferedReader = BufferedReader(streamReader)
            val stringWriter = StringWriter()
            while (bufferedReader.read(charArray).also { readCount = it } != -1) {
                stringWriter.write(charArray, 0, readCount)
            }
            stringWriter.toString()
        } catch (e: IOException) {
            ""
        }
    }



}