package com.key.board.journey.inputmethod

import android.app.Application
import android.graphics.Typeface
import com.ad.toponlibrary.AdManager
import com.ad.toponlibrary.AdManager.mContext
import com.anythink.debug.api.ATDebuggerUITest
import com.key.board.journey.inputmethod.data.ChildBean
import com.key.board.journey.inputmethod.data.ClassBean
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 MyKeyboard : Application() {
    companion object {
        lateinit var myKeyboardInstance: MyKeyboard

        lateinit var list: MutableList<ClassBean>

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

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

        AdManager.init(TAG,this,"h688c1f394307b","a75e9df07167c5ba8721c7c876747aa13",
            "9788519403e27aa6420a9b9ad49bc0fc76166bea",
            "n688c1f94c5fde",
            "n688c1f9482a73",
            "n688c1f943a1a0",false)
        UpLoadManager.init(this,TAG)

    }

    private fun dealFile() {
        val openFile = myKeyboardInstance.assets.open("data.json")
        val jsonString = getJsonString(openFile)
        if (jsonString != null) {
            resolveJsonString(jsonString)
        }
    }


    private fun resolveJsonString(string: String) {
        val jsonData = JSONArray(string)
        var dataList: MutableList<ClassBean> = mutableListOf()
        for (i in 0 until jsonData.length()) {
            jsonData.getJSONObject(i).run {
                val pName = getString("parent_name")
                val listArray = getJSONArray("keyboard_list")
                var childBeanList: MutableList<ChildBean> = 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 {

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

                    }

                }
                val shuffled = childBeanList.shuffled()
                val dataClassBean = ClassBean()
                    .apply {
                    parentName = pName
                    keyboardList = shuffled
                }
                dataList.add(dataClassBean)

            }
        }

        updateDataList(dataList)

    }

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

    }
    private fun getJsonString(fileInputStream: InputStream): String? {
        return try {
            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) {
            ""
        }
    }



}