package com.keyboard.skinning.cool.skin

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.Log
import com.keyboard.skinning.cool.App
import com.keyboard.skinning.cool.tools.CconfigKey
import com.keyboard.skinning.cool.tools.SpSkin
import java.io.File

object KeyAction {

    fun keyToUpper(mKeyBoard: com.keyboard.skinning.cool.resourcecode.CusKeyBoard) {
        for (key in mKeyBoard.keys) {
            if (key.label != null) {
                if (key.label.length == 1) {
                    val charLabel = key.label.toString()[0]
                    val toUpperCase = charLabel.toUpperCase()
                    key.codes[0] = toUpperCase.toInt()
                    key.label = toUpperCase.toString()
                }

            }
        }
    }


    fun keyToLowerCase(mKeyBoard: com.keyboard.skinning.cool.resourcecode.CusKeyBoard) {
        for (key in mKeyBoard.keys) {
            if (key.label != null) {
                if (key.label.length == 1) {
                    val charLabel = key.label.toString()[0]
                    val toLowerCase = charLabel.toLowerCase()
                    key.codes[0] = toLowerCase.toInt()
                    key.label = toLowerCase.toString()
                }

            }
        }
    }

    fun primaryCodeToChar(primCode: Int): String {
        val toString = primCode.toChar().toString()

        return toString
    }

    @SuppressLint("SuspiciousIndentation")
    fun readBgOrVideo(
        context: Context,
        playAction: (gif: String?, bgDraw: Drawable?) -> Unit
    ) {

        SpSkin.getSkinPath()?.let { resPath ->
            val videoPath = "${resPath}res/raw/${CconfigKey.videoName}"
            val videoPath2 = "${resPath}res/raw/${CconfigKey.video}"
            val backgroundPath = "${resPath}res/drawable-xxhdpi-v4/${CconfigKey.bgName}"
            val backgroundPath_png = "${resPath}res/drawable-xxhdpi-v4/${CconfigKey.bgName_png}"
            val file = File(videoPath)
            val file2 = File(videoPath2)
            val file3 = File(backgroundPath)
            val file4 = File(backgroundPath_png)
            if (file.exists() || file2.exists()) {
                Log.d(App.TAG, "--------11111111= resPath=${resPath}")
                val path: String = if (file.exists()) {
                    videoPath
                } else {
                    videoPath2
                }


                val bitmapDrawable =
                    BitmapDrawable(context.resources, BitmapFactory.decodeFile(path))

                playAction.invoke(path, null)

//                playAction.invoke(mediaPlayer,null);
            } else {
                Log.d(
                    App.TAG,
                    "--------11111111= file3.exists()" + file3.exists() + "---resPath=" + resPath
                )
                if (file3.exists()) {
                    val bitmapDrawable =
                        BitmapDrawable(context.resources, BitmapFactory.decodeFile(backgroundPath))
                    playAction.invoke(null, bitmapDrawable)
                } else if (file4.exists()) {
                    val bitmapDrawable =
                        BitmapDrawable(context.resources, BitmapFactory.decodeFile(backgroundPath_png))
                    playAction.invoke(null, bitmapDrawable)
                } else {
                    playAction.invoke(null, null)
                }


            }

        }
    }


}