package com.app.intelligent.board

import android.app.Application
import android.graphics.Typeface
import com.app.intelligent.board.uimodel.KeyboardItemInfo
import com.app.intelligent.board.uimodel.KeyboardItemDataWrapper
import com.up.uploadlibrary.UpLoadManager
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 AppContextHelper : Application() {
    companion object {
        lateinit var appInstance: AppContextHelper

        lateinit var list: MutableList<KeyboardItemDataWrapper>

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

        // 关键：添加@JvmField让Java可访问，声明为public
        @JvmField
        var trendingKeyboards: MutableList<KeyboardItemInfo> = mutableListOf()
    }

    override fun onCreate() {
        super.onCreate()
        appInstance = this
        defaultFont = Typeface.createFromAsset(assets, "my_font.ttf")
        UpLoadManager.init(this, TAG, { s: String?, s2: String? -> null })

        dealFile()
    }

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

    private fun resolveJsonString(string: String) {
        val jsonData = JSONArray(string)
        var dataList: MutableList<KeyboardItemDataWrapper> = mutableListOf()
        for (i in 0 until jsonData.length()) {
            jsonData.getJSONObject(i).run {
                val pName = getString("parent_name")
                val listArray = getJSONArray("keyboard_list")
                var beanDetailsList: MutableList<KeyboardItemInfo> = 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")
                        }
                        beanDetailsList.add(
                            KeyboardItemInfo().apply {
                                setImgPath(imgPath)
                                setZipPath(zipPath)
                                setTitleName(title)
                                setImgGif(imgGif)
                                thumbUrl = thUrl
                                thumbGif = thGif
                            })
                    }
                }
                val shuffled = beanDetailsList.shuffled()
                val dataBeanWrapper = KeyboardItemDataWrapper()
                    .apply {
                        parentName = pName
                        keyboardList = shuffled
                    }
                dataList.add(dataBeanWrapper)
            }
        }
        updateDataList(dataList)
    }

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