package com.app.keyboard.colorful.alldata

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.util.Xml
import androidx.core.content.ContextCompat
import com.app.keyboard.colorful.MyApp
import com.app.keyboard.colorful.R
import com.app.keyboard.colorful.alldata.config.ServiceConfig
import com.app.keyboard.colorful.helper.NyTheme
import com.app.keyboard.colorful.helper.AllResName
import com.app.keyboard.colorful.helper.Sp
import org.xmlpull.v1.XmlPullParser
import java.io.File
import java.io.StringReader

class ConfigManager(var context: Context) {


    private var textSize = 13f

    var functionDraw: Drawable =
        getDefaultDrawList(R.drawable.dekey_normal, R.drawable.dekey_presse)
    var generalDraw: Drawable =
        getDefaultDrawList(R.drawable.dekey_normal, R.drawable.dekey_presse)

    var toDraw: Drawable = getDefaultDrawList(R.drawable.dekey_normal, R.drawable.dekey_presse)
    var spaceDraw: Drawable = getDefaultDrawList(R.drawable.dekey_normal, R.drawable.dekey_presse)

    var switchDraw: Drawable? = null
    var deleteDraw: Drawable? = null
    var backDraw: Drawable? = null
    var searchDraw: Drawable? = null

    var shiftDraw: Drawable? = null
    var shiftLockDraw: Drawable? = null

    var keyTextColor: Int = ContextCompat.getColor(context, R.color.black)
    var keyTextColorFunction: Int = ContextCompat.getColor(context, R.color.black)


//
//    fun getKeyboard() = {
//        val skinPath = Sp.getSkinPath()
//        val configFilePath = skinPath + "assets/keyboard.conf"
//        val file = File(configFilePath)
//        if(file.exists()){
//              KeyboardConfigParser.initConfig(configFilePath)
//        }else{
//              null
//        }
//    }

    fun getConfig(): ServiceConfig? {
        val skinPath = Sp.getSkinPath()
        val configFilePath = skinPath + "assets/keyboard.conf"
        val file = File(configFilePath)
        return if (file.exists()) {
            NyTheme.initConfig(configFilePath)
        } else {
            null
        }
    }

    fun getConfigBg(name: String): Drawable? {
        Sp.getSkinPath()?.let { resPath ->
            val pPath = "${resPath}res/drawable-xhdpi-v4/"

            return getDrawList(
                pPath + name,
                pPath + name
            )
        }
        return null
    }

    fun updateSkinConfig() {
        Sp.getSkinPath()?.let { resPath ->
            val pPath = "${resPath}res/drawable-xhdpi-v4/"
            pPath.let {
                readColors(resPath) {
                    for ((name, value) in it) {
                        if (name == AllResName.keyTextColorName) {
                            keyTextColor = value
                        }
                        if (name == AllResName.keyTextColorFunctionName) {
                            keyTextColorFunction = value
                        }
                    }

                }
                functionDraw = getDrawList(
                    it + AllResName.functionNormalName,
                    it + AllResName.functionPressName
                )
                generalDraw = getDrawList(it + AllResName.normalName, it + AllResName.pressName)
                toDraw = getDrawList(it + AllResName.toNormalName, it + AllResName.toPressName)
                spaceDraw =
                    getDrawList(it + AllResName.spaceNormalName, it + AllResName.spacePressName)
                switchDraw =
                    getDrawList(it + AllResName.imeSwitchName, it + AllResName.imeSwitchName)
                deleteDraw = getDrawList(
                    it + AllResName.deleteNormalName,
                    it + AllResName.deletePressName
                )
                backDraw = getDrawList(it + AllResName.backName, it + AllResName.backName)
                searchDraw = getDrawList(it + AllResName.searchName, it + AllResName.searchName)
                shiftDraw = getDrawList(
                    it + AllResName.shiftNormalName,
                    it + AllResName.shiftNormalName
                )
                shiftLockDraw =
                    getDrawList(it + AllResName.shiftLockName, it + AllResName.shiftLockName)
            }

        }
    }


    private fun getDefaultDrawList(normalDrawId: Int, pressDrawId: Int): StateListDrawable {
        val normalDraw = ContextCompat.getDrawable(MyApp.myAppInstance, normalDrawId)
        val pressDraw = ContextCompat.getDrawable(MyApp.myAppInstance, pressDrawId)
        val stateListDrawable = StateListDrawable().apply {
            addState(
                intArrayOf(android.R.attr.state_pressed),
                pressDraw
            )
            addState(intArrayOf(), normalDraw)
        }

        return stateListDrawable


    }


    private fun getDrawList(normalPath: String, pressPath: String): StateListDrawable {
        val pressDraw = BitmapFactory.decodeFile(pressPath)
        val normalDraw = BitmapFactory.decodeFile(normalPath)
        val stateListDrawable = StateListDrawable().apply {
            addState(
                intArrayOf(android.R.attr.state_pressed),
                BitmapDrawable(context.resources, pressDraw)
            )
            addState(intArrayOf(), BitmapDrawable(context.resources, normalDraw))
        }

        return stateListDrawable


    }

    private fun readColors(resPath: String, callBack: (Map<String, Int>) -> Unit) {
        val resMaps = mutableMapOf<String, Int>()

        val pPath = "${resPath}res/colors.xml"
        val file = File(pPath)
        if (file.exists()) {
            val xmlPullParser = Xml.newPullParser().apply {
                setInput(StringReader(file.readText()))
                setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false)

            }
            var curType = xmlPullParser.eventType
            while (curType != XmlPullParser.END_DOCUMENT) {
                val b = curType == XmlPullParser.START_TAG
                val b1 = xmlPullParser.name == "color"
                val b2 = xmlPullParser.name == "item"
                if (b && (b1 || b2)) {
                    val attributeName = xmlPullParser.getAttributeValue(null, "name")
                    val nextTextValue = xmlPullParser.nextText()
                    val b3 = attributeName == AllResName.keyTextColorName
                    val b4 = attributeName == AllResName.keyTextColorFunctionName
                    if (b3 || b4) {
                        resMaps[attributeName] = Color.parseColor(nextTextValue)
                    }
                }
                curType = xmlPullParser.next()
            }

        }

        callBack.invoke(resMaps)


    }


}