package com.keyboard.theme.app.keyboard.utils

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.Xml

import androidx.core.content.ContextCompat
import com.keyboard.theme.app.keyboard.MyAppllication
import com.keyboard.theme.app.keyboard.R
import org.xmlpull.v1.XmlPullParser
import java.io.File
import java.io.StringReader

class KeyBoardManager {

    companion object {
        val myKeyBoardManager by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            KeyBoardManager()
        }
    }

    lateinit var functionBackgroundDraw: Drawable
    lateinit var spBackgroundDraw: Drawable
    lateinit var normalBackgroundDraw: Drawable

    var icShittLock: Drawable? =
        ContextCompat.getDrawable(MyAppllication.instance, R.drawable.im_shift)
    var icDel: Drawable? =
        ContextCompat.getDrawable(MyAppllication.instance, R.drawable.im_del)
    var allBg: Drawable? =
        ContextCompat.getDrawable(MyAppllication.instance, R.mipmap.bg)

    var icBshift: Drawable? =
        ContextCompat.getDrawable(MyAppllication.instance, R.drawable.im_shift)

    var keycolor: Int = MyAppllication.instance.resources.getColor(R.color.white, null)

    var icSshift: Drawable? =
        ContextCompat.getDrawable(MyAppllication.instance, R.drawable.im_shift)


    init {
        val default =
            ContextCompat.getDrawable(MyAppllication.instance, R.drawable.default_keybg)
        val press = ContextCompat.getDrawable(
            MyAppllication.instance,
            R.drawable.default_keybg_press
        )
        if (press != null) {
            if (default != null) {
                val listDrawable = Common.getStatus(default, press)
                functionBackgroundDraw = listDrawable

                normalBackgroundDraw = listDrawable
                spBackgroundDraw = listDrawable

            }
        }
    }




    private fun gettextcolor(colorXmlPath: String) {
        val file = File(colorXmlPath)
        if (!file.exists()) return
        val xmlP = Xml.newPullParser()

        xmlP.setInput(StringReader(file.readText()))
        xmlP.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false)


        var eventT = xmlP.eventType
        while (eventT != XmlPullParser.END_DOCUMENT) {
            if (eventT == XmlPullParser.START_TAG && (xmlP.name == "color" || xmlP.name == "item")) {
                val value = xmlP.getAttributeValue(null, "name")
                if (value != null && value == StaticValue.title_color) {
                    keycolor = Color.parseColor(xmlP.nextText())
                }
            }
            eventT = xmlP.next()
        }


    }

    fun uodateConfig(con: Context) {
        Common.getCurPath()?.let {
            Common.getbgic(
                con,
                it.plus(StaticValue.parent_path).plus(StaticValue.title_nor_Bg)
            )?.let { drawBG ->
                Common. getbgic(
                    con,
                    it.plus(StaticValue.parent_path).plus(StaticValue.title_nor_Bg_press)
                )?.let { drawPressBG ->
                    normalBackgroundDraw = Common.getStatus(drawBG, drawPressBG)
                }

            }
            gettextcolor(it.plus(StaticValue.color_path))


            Common.getbgic(con, it.plus(StaticValue.parent_path).plus(StaticValue.title_sp_Bg))?.let { drawBG ->
                Common.getbgic(
                    con,
                    it.plus(StaticValue.parent_path).plus(StaticValue.title_sp_Bg_press)
                )?.let { drawPressBG ->
                    spBackgroundDraw = Common.getStatus(drawBG, drawPressBG)
                }

            }
            Common. getbgic(con, it.plus(StaticValue.xx_path).plus(StaticValue.title_bg))?.run {
                allBg = this
            }

            Common.getbgic(
                con,
                it.plus(StaticValue.parent_path).plus(StaticValue.title_fun_Bg)
            )?.let { drawBG ->
                Common.getbgic(
                    con,
                    it.plus(StaticValue.parent_path).plus(StaticValue.title_func_bg_press)
                )?.let { drawPressBG ->
                    functionBackgroundDraw = Common.getStatus(drawBG, drawPressBG)
                }

            }


            Common.getbgic(con, it.plus(StaticValue.parent_path).plus(StaticValue.title_shitf_ic))?.let {
                icSshift = it
                icBshift = it
            }
            Common.getbgic(
                con,
                it.plus(StaticValue.parent_path).plus(StaticValue.title_del_ic)
            )?.let { drawBG ->
                    icDel = Common.getStatus(drawBG, drawBG)


            }
            Common.getbgic(con, it.plus(StaticValue.parent_path).plus(StaticValue.title_shitf_ic_lock))?.let {
                icShittLock = it
            }


        }
    }

}