package com.color.keyboard.black

//import com.pretty.keyboard.theme.keyboard.helper.ObjectBox
import android.app.Application
import android.content.Intent
import android.graphics.Typeface
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.applovin.sdk.AppLovinMediationProvider
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkInitializationConfiguration
import com.color.keyboard.black.bean.ChildData
import com.color.keyboard.black.bean.ClassData
import org.json.JSONArray
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.StringWriter
import java.security.AccessController.getContext


class ColorApp : Application() {
    companion object {
        lateinit var colorAppInstance: ColorApp

        lateinit var list: MutableList<ClassData>

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

        const val AD_BROADCAST: String = "on_success_action"
        const val MAX_SDK: String =
            "CiVvkZKZlVhJJ696v3IBuPvq8pmLmwWefV1aP489QxDAUhVNsllZM8Uwvm7w1UUwURQ1RX6fO9Omo3fuvo7NlT"
        var initSDK: Boolean = false
    }

    override fun onCreate() {
        super.onCreate()
        colorAppInstance = this
        defaultFont = Typeface.createFromAsset(assets, "font5.otf")
        dealFile()

        initSdk()

    }

    private fun initSdk() {
        val initConfig = AppLovinSdkInitializationConfiguration.builder(MAX_SDK, this)
            .setMediationProvider(AppLovinMediationProvider.MAX)
            .build()
        AppLovinSdk.getInstance(this).initialize(initConfig) {
            initSDK = true
            LocalBroadcastManager.getInstance(colorAppInstance.applicationContext)
                .sendBroadcast(Intent(AD_BROADCAST))
        }
        AppLovinSdk.getInstance(this).settings.setVerboseLogging(true)

        //MAX广告测试
//        AppLovinSdk.getInstance( this ).showMediationDebugger();
    }

    private fun dealFile() {
        val openFile = colorAppInstance.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 {
            //            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) {
            ""
        }
    }


}