package com.themes.keyboard.black

import android.app.Application
import android.graphics.Typeface
import com.anythink.core.api.ATSDK
import com.anythink.core.api.NetTrafficeCallback
import com.themes.keyboard.black.localdata.ChildData
import com.themes.keyboard.black.localdata.ClassData
import com.themes.keyboard.black.topon.AdManager
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 MyApp : Application() {
    companion object {
        lateinit var myAppInstance: MyApp

        lateinit var list: MutableList<ClassData>

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

    override fun onCreate() {
        super.onCreate()
        myAppInstance = this
        defaultFont = Typeface.createFromAsset(assets, "font5.ttf")
        initTopOn()
        dealFile()

    }

    private fun dealFile() {
        val openFile = myAppInstance.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<ClassData> = mutableListOf()
        for (i in 0 until jsonData.length()) {
            jsonData.getJSONObject(i).run {
                val pName = getString("parent_name")
                val listArray = getJSONArray("keyboard_list")
                var childDataList: MutableList<ChildData> = 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 {

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

                    }

                }
                val shuffled = childDataList.shuffled()
                val dataClassData = ClassData()
                    .apply {
                        parentName = pName
                        keyboardList = shuffled
                    }
                dataList.add(dataClassData)

            }
        }

        updateDataList(dataList)

    }

    private fun updateDataList(mainList: MutableList<ClassData>) {
        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) {
            ""
        }
    }


    private fun initTopOn() {
        ATSDK.checkIsEuTraffic(this, object : NetTrafficeCallback {
            override fun onResultCallback(isEU: Boolean) {
                if (isEU && ATSDK.getGDPRDataLevel(this@MyApp) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(this@MyApp)
                }
            }

            override fun onErrorCallback(errorMsg: String) {

            }
        })
        val APPId = "h682c349f7aec8"
        val AppKey = "af6f1f9358ea1c8507a1f7247e71697c6"
        val debug_KEy = "fe06c76b794f95216f98e5982ae8f335fc558956"
        ATSDK.init(this, APPId, AppKey)
        AdManager.loadAllAd()
        //测试工具
//  ATDebuggerUITest.showDebuggerUI(this,debug_KEy);
    }

}