package com.app.keyboard.colorful.alldata

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.app.keyboard.colorful.MyApp
import com.app.keyboard.colorful.helper.AllResName
import com.app.keyboard.colorful.helper.Sp
import java.io.File

object ColorFunction {

    fun keyToUpper(mKeyBoard: com.app.keyboard.colorful.rescode.CustKeyBoard) {
        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.app.keyboard.colorful.rescode.CustKeyBoard) {
        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
    ) {

        Sp.getSkinPath()?.let { resPath ->
            val videoPath = "${resPath}res/raw/${AllResName.videoName}"
            val videoPath2 = "${resPath}res/raw/${AllResName.video}"
            val backgroundPath = "${resPath}res/drawable-xxhdpi-v4/${AllResName.bgName}"
            val backgroundPath_png = "${resPath}res/drawable-xxhdpi-v4/${AllResName.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(MyApp.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(
                    MyApp.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)
                }


            }

        }
    }


}