package com.keyboard.theme.app.keyboard.myservice

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView
import android.util.AttributeSet
import com.keyboard.theme.app.keyboard.utils.KeyBoardManager
import com.keyboard.theme.app.keyboard.utils.Common
import com.keyboard.theme.app.keyboard.utils.StaticValue

@Suppress("DEPRECATION")
class MyCustomView @JvmOverloads constructor(
    var mContext: Context,
    attributeSet: AttributeSet? = null,
    style: Int = 0
) : KeyboardView(mContext, attributeSet, style) {
    var keyBoardManager = KeyBoardManager.myKeyBoardManager
    var shift_status = StaticValue.Shift_S
    var xmlMode = StaticValue.xml0

    private var mPaint: Paint = Paint().apply {
        isAntiAlias = true
        textAlign = Paint.Align.CENTER
        textSize = Common.spToUnitPx(16f, mContext)
        color = keyBoardManager.keycolor
    }

    override fun onDraw(canvas: Canvas) {
        keyboard.keys.forEach {
            when (it.codes[0]) {
                Keyboard.KEYCODE_SHIFT -> {
                    andDraw(
                        it,
                        keyBoardManager.functionBackgroundDraw,
                        getCurIc(),
                        canvas
                    )
                }
                StaticValue.SHIFT_NUMBER, StaticValue.SHIFT_SYMBOL -> {
                    andDraw(it,  keyBoardManager.functionBackgroundDraw, null, canvas)
                }

                Keyboard.KEYCODE_DELETE -> {
                    andDraw(
                        it,
                        keyBoardManager.functionBackgroundDraw,
                        keyBoardManager.icDel,
                        canvas
                    )
                }

                Keyboard.KEYCODE_MODE_CHANGE, Keyboard.KEYCODE_DONE -> {
                    andDraw(it,  keyBoardManager.functionBackgroundDraw, null, canvas)
                }

                else -> {
                    andDraw(it,  keyBoardManager.normalBackgroundDraw, null, canvas)
                }
            }
        }

    }

    fun upUi(con: Context) {
        keyBoardManager.uodateConfig(con)
        background = keyBoardManager.allBg
        invalidate()

    }

    private fun getCurIc(): Drawable? {
        return when (shift_status) {
            StaticValue.Shift_B_lo -> keyBoardManager.icShittLock
            StaticValue.Shift_B -> keyBoardManager.icBshift
            StaticValue.Shift_S -> keyBoardManager.icSshift
            else -> keyBoardManager.icSshift
        }
    }



    @SuppressLint("SuspiciousIndentation")
    private fun andDraw(
        myKey: Keyboard.Key,
        keyBG: Drawable,
        icon: Drawable?,
        canvas: Canvas,
    ) {

        myKey.run {
            keyBG.run {
                bounds = Rect(
                    x.plus(paddingLeft),
                    y.plus(paddingTop),
                    width.plus(x.plus(paddingLeft)),
                    height.plus(y.plus(paddingTop))
                )
                state = currentDrawableState
                draw(canvas)


            }
        }
        myKey.run {
            icon?.apply {
                myKey.icon = this

                var icon_w = myKey.icon.intrinsicWidth.toFloat()
                var icon_wr = icon_w / myKey.width.toFloat()
                var icon_h = myKey.icon.intrinsicHeight.toFloat()
                var icon_hr = icon_h / myKey.height.toFloat()


                var tep1 = 0f
                var tep2 = 0f
                if (icon_wr > icon_hr) {
                      tep2 = icon_wr
                      tep1 = icon_wr.coerceAtLeast(0.5f)

                } else {
                    tep2 = icon_hr
                    tep1 = icon_hr.coerceAtLeast(0.5f)

                }
                icon_h = (icon_h / tep2) * tep1
                icon_w = (icon_w / tep2) * tep1
                myKey.icon.let {
                    it.bounds = Rect().apply {

                        top =
                            (myKey.y + paddingTop + (myKey.height - icon_h) / 2f).toInt()
                        left =
                            (myKey.x + paddingLeft + (myKey.width - icon_w) / 2f).toInt()
                        bottom = (top + icon_h).toInt()
                        right = (left + icon_w).toInt()

                    }
                    it.draw(canvas)
                }
            }

            mPaint.color = keyBoardManager.keycolor
                if (!label.isNullOrEmpty()) {
                    val y1 = y.plus(paddingRight).plus((height.div(2f))).plus((mPaint.textSize.minus(mPaint.descent())).div(2f))
                    val x1 = x.plus(paddingLeft).plus((width.div(2f)))
                    canvas.drawText(label.toString(), x1, y1, mPaint)
                }
        }




    }
}