package com.input.board.appskey.keyboard.customer

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 android.view.SurfaceView
import androidx.core.content.ContextCompat
import com.input.board.appskey.keyboard.KeyBoardApp
import com.input.board.appskey.keyboard.R
import com.input.board.appskey.keyboard.fileTools.SpSkin
import org.xmlpull.v1.XmlPullParser
import java.io.File
import java.io.StringReader

class KeyBoardSkin(var context: Context) {


    private var textSize = 13f

    var functionDraw: Drawable = getDefaultDrawList(R.mipmap.key_normal,R.mipmap.key_pressed_on)
    var generalDraw: Drawable = getDefaultDrawList(R.mipmap.key_normal,R.mipmap.key_pressed_on)

    var toDraw: Drawable = getDefaultDrawList(R.mipmap.key_normal,R.mipmap.key_pressed_on)
    var spaceDraw: Drawable = getDefaultDrawList(R.mipmap.key_normal,R.mipmap.key_pressed_on)

    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 updateSkinConfig(surfaceView: SurfaceView) {
        SpSkin.getSkinPath()?.let { resPath ->
            val pPath = "${resPath}res/drawable-xhdpi-v4/"
            pPath.let {
                readColors(resPath) {
                    for ((name, value) in it) {
                        if (name == KeyCodeConst.keyTextColorName) {
                            keyTextColor = value
                        }
                        if (name == KeyCodeConst.keyTextColorFunctionName) {
                            keyTextColorFunction = value
                        }
                    }

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

        }
    }


    private fun getDefaultDrawList(normalDrawId: Int, pressDrawId: Int): StateListDrawable {
        val normalDraw = ContextCompat.getDrawable(KeyBoardApp.appInstance,normalDrawId )
        val pressDraw = ContextCompat.getDrawable(KeyBoardApp.appInstance,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 == KeyCodeConst.keyTextColorName
                    val b4 = attributeName == KeyCodeConst.keyTextColorFunctionName
                    if (b3 || b4) {
                        resMaps[attributeName] = Color.parseColor(nextTextValue)
                    }
                }
                curType = xmlPullParser.next()
            }

        }

        callBack.invoke(resMaps)


    }


}