package com.input.board.appskey.keyboard

import android.app.Application
import android.util.Log
import com.anythink.core.api.ATSDK
import com.anythink.core.api.NetTrafficeCallback
import com.input.board.appskey.keyboard.fileTools.DesFile
import com.input.board.appskey.keyboard.fileTools.DesFile.deFile
import com.input.board.appskey.keyboard.jsonBean.ChildBean
import com.input.board.appskey.keyboard.jsonBean.MainBean
import org.json.JSONArray

class KeyBoardApp : Application() {
    companion object {
        lateinit var appInstance: KeyBoardApp

        lateinit var list: MutableList<MainBean>

        const val TAG = "+++++++++++++++++++++"

    }
    private val APPId = "h66b1d37c22a77"
    private val AppKey = "a3f5e17582cca6e43dcbb0732c125dec8"

    private val debug_Key = "63db2183733f4027ed5333d9ea59e1540d46b1a6"
    override fun onCreate() {
        super.onCreate()
        appInstance = this

        initTopOnSDK()
        dealFile()
    }

    private fun dealFile() {
        val openFile = appInstance.assets.open("source_keyboard.json")
        val jsonString = DesFile.getJsonString(openFile)
        resolveJsonString(jsonString)
    }


    private fun resolveJsonString(string: String) {
        val jsonData = JSONArray(string)
        var dataList: MutableList<MainBean> = 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 mainBean = MainBean().apply {
                    parentName = pName
                    keyboardList = childBeanList
                }
                dataList.add(mainBean)

            }
        }

        updateDataList(dataList)

    }

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

    }

    private fun initTopOnSDK() {
        ATSDK.checkIsEuTraffic(this, object : NetTrafficeCallback {
            override fun onResultCallback(isEU: Boolean) {
                Log.e(TAG, "onResultCallback:$isEU")
                if (isEU && ATSDK.getGDPRDataLevel(this@KeyBoardApp) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(this@KeyBoardApp)
                }
            }

            override fun onErrorCallback(errorMsg: String) {
                Log.e(TAG, "onErrorCallback:$errorMsg")
            }
        })
        ATSDK.init(this, APPId, AppKey)
//        AdManager.loadAllAd()
        //测试工具
//        ATDebuggerUITest.showDebuggerUI(this, debug_Key)
//        ATSDK.setNetworkLogDebug(true)
    }

}