package com.input.board.appskey.keyboard

import android.app.Application
import android.content.Intent
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager
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 com.input.board.appskey.keyboard.unityad.UnityAdManager
import com.input.board.appskey.keyboard.unityad.onLoadCallBack
import com.unity3d.ads.IUnityAdsInitializationListener
import com.unity3d.ads.UnityAds
import org.json.JSONArray

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

        lateinit var list: MutableList<MainBean>

        const val unityGameID = "5649398"

        const val testMode = false

        var SDKOK = false
        const val AD_INIT_ACTION ="ACTION"

    }

    override fun onCreate() {
        super.onCreate()
        appInstance = this

        initUNity()
        dealFile()
    }

    private fun dealFile() {
        val openFile = appInstance.assets.open("encrypt_keyboard.json")
        val deFilePath = deFile(openFile)
        val jsonString = DesFile.getJsonString(deFilePath)
        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 initUNity() {
        Log.d(UnityAdManager.TAG,"----------------------application init")
        UnityAds.initialize(this, unityGameID, testMode, object : IUnityAdsInitializationListener {
            override fun onInitializationComplete() {
                LocalBroadcastManager.getInstance(this@KeyBoardApp).sendBroadcast(Intent(AD_INIT_ACTION))
                SDKOK = true
                UnityAdManager.loadAllAdNew()

            }

            override fun onInitializationFailed(
                error: UnityAds.UnityAdsInitializationError?,
                message: String?
            ) {
                SDKOK = false
            }

        });
    }

}