package com.themes.fancy.keyboard.keyboard.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
//import android.inputmethodservice.Keyboard
import com.themes.fancy.keyboard.keyboard.helper.SpSave
import com.themes.fancy.keyboard.keyboard.code.CusKeyBoard
import com.themes.fancy.keyboard.keyboard.helper.ResName
import java.io.File

object KeyFunction {

    fun keyToUpper(mKeyBoard: 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: 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
    ) {

        SpSave.getSkinPath()?.let { resPath ->
            val videoPath = "${resPath}res/raw/${ResName.videoName}"
            val videoPath2 = "${resPath}res/raw/${ResName.video}"
            val backgroundPath = "${resPath}res/drawable-xxhdpi-v4/${ResName.bgName}"
            val file = File(videoPath)
            val file2 = File(videoPath2)
            if (file.exists()||file2.exists()) {
                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{

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

                    playAction.invoke(null,bitmapDrawable)

            }

        }
    }


}