package com.input.board.appskey.keyboard.customer

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 android.media.MediaPlayer
import android.view.SurfaceHolder
import android.view.SurfaceView
import com.input.board.appskey.keyboard.fileTools.SpSkin
import java.io.File

object KeyFunction {

    fun keyToUpper(mKeyBoard: MyKeyBoard) {
        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: MyKeyBoard) {
        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,
        surfaceView: SurfaceView,
        playAction: (mediaPlayer: MediaPlayer?,bgDraw:Drawable?) -> Unit
    ) {

        SpSkin.getSkinPath()?.let { resPath ->
            val videoPath = "${resPath}res/raw/${KeyCodeConst.videoName}"
            val backgroundPath = "${resPath}res/drawable-xxhdpi-v4/${KeyCodeConst.bgName}"
            val file = File(videoPath)
            if (file.exists()) {
                val mediaPlayer = MediaPlayer().apply {
                    setDataSource(videoPath)
                    prepareAsync()
                }
                surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
                    override fun surfaceCreated(holder: SurfaceHolder) {
                        mediaPlayer.setDisplay(holder)
                    }

                    override fun surfaceChanged(
                        holder: SurfaceHolder,
                        format: Int,
                        width: Int,
                        height: Int
                    ) {

                    }

                    override fun surfaceDestroyed(holder: SurfaceHolder) {

                    }

                })
                playAction.invoke(mediaPlayer,null);
            }else{

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

                    playAction.invoke(null,bitmapDrawable)

            }

        }
    }


}