package com.photo.keyboard.themes.mykeyboard

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

import androidx.core.content.ContextCompat
import com.photo.keyboard.themes.App
import com.photo.keyboard.themes.R
import com.photo.keyboard.themes.manager.ConstValue
import com.photo.keyboard.themes.manager.Utils
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(App.instance, R.drawable.im_shift)
    var icDel: Drawable? =
        ContextCompat.getDrawable(App.instance, R.drawable.im_del)
    var allBg: Drawable? =
        ContextCompat.getDrawable(App.instance, R.mipmap.bg)

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

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

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


    init {
        val default =
            ContextCompat.getDrawable(App.instance, R.drawable.default_keybg)
        val press = ContextCompat.getDrawable(
            App.instance,
            R.drawable.default_keybg_press
        )
        if (press != null) {
            if (default != null) {
                val listDrawable = Utils.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 == ConstValue.title_color) {
                    keycolor = Color.parseColor(xmlP.nextText())
                }
            }
            eventT = xmlP.next()
        }


    }

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

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


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

            }
            Utils.getbgic(con, it.plus(ConstValue.xx_path).plus(ConstValue.title_bg))?.run {
                allBg = this
            }
            Utils.getbgic(con, it.plus(ConstValue.xx_path).plus(ConstValue.title_bg_new))?.run {
                allBg = this
            }

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

            }


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


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


        }
    }


    fun getConfigBg(contetx:Context,name: String): Drawable? {
        Utils.getCurPath()?.let { resPath ->
            val pPath = "${resPath}/drawable-xhdpi-v4/"

            return Utils.getbgic(
                contetx,
                pPath + name
            )
        }
        return null
    }

    fun getConfig(): com.photo.keyboard.themes.resconfig.KeyboardConfig? {
        val skinPath = Utils.getCurPath()

        val substringBefore = skinPath?.substringBefore("res")
        val configFilePath = substringBefore + "assets/keyboard.conf"
        val file = File(configFilePath)
        return if (file.exists()) {
            com.photo.keyboard.themes.resconfig.KeyboardConfigParser.initConfig(configFilePath)
        } else {
            null
        }
    }
}