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

import android.inputmethodservice.InputMethodService
import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener
import android.os.SystemClock
import android.view.View
import android.view.inputmethod.EditorInfo
import com.keyboard.theme.app.keyboard.R
import com.keyboard.theme.app.keyboard.databinding.ViewKeyboradBinding
import com.keyboard.theme.app.keyboard.utils.StaticValue

class InputService : InputMethodService() ,OnKeyboardActionListener{


    private var mouble = false
    private var views = intArrayOf(R.xml.a_xml, R.xml.b_xml, R.xml.c_xml)
    private var la_Time = -3L
    private lateinit var viewBinding: ViewKeyboradBinding

    override fun onCreateInputView(): View {
        viewBinding = ViewKeyboradBinding.inflate(layoutInflater, null, false)
        viewBinding.idCustomInput.setOnKeyboardActionListener(this)
        initInputView()


        return viewBinding.root
    }

    private fun changeXml(mode: Int) {
        viewBinding.idCustomInput.run {
            when (mode) {
                0 -> {
                    xmlMode = StaticValue.xml0
                    keyboard = Keyboard(mContext, views[0])
                }

                2 -> {
                    xmlMode = StaticValue.xml2
                    keyboard = Keyboard(mContext, views[2])

                }

                1 -> {
                    xmlMode = StaticValue.xml1
                    keyboard = Keyboard(mContext, views[1])
                }
            }
        }
    }
    override fun onWindowShown() {
        super.onWindowShown()

        viewBinding.idCustomInput.upUi(this)
    }
    private fun keySB(toBig: Boolean, keyboard: Keyboard) {
        for (key in keyboard.keys) {
            if (!key.label.isNullOrEmpty()) {
                if (key.label.length == 1) {
                    var strin: Char = if (toBig) {
                        key.label.toString()[0].uppercaseChar()
                    } else {
                        key.label.toString()[0].lowercaseChar()
                    }
                    key.run {
                        label = strin.toString()
                        codes[0] = strin.code
                    }
                }
            }

        }

    }

    private fun initInputView() {
        viewBinding.idCustomInput.run {
            keyboard = Keyboard(this@InputService, views[0])
            isEnabled = true

        }
    }

    override fun onPress(primaryCode: Int) {
        mouble = false
        if (primaryCode == Keyboard.KEYCODE_SHIFT) {
            if (300 > SystemClock.elapsedRealtime() - la_Time) {
                mouble = true
            }
            la_Time = SystemClock.elapsedRealtime()
        }
    }

    override fun onRelease(primaryCode: Int) {

    }

    override fun onKey(primaryCode: Int, keyCodes: IntArray?) {
        when (primaryCode) {
            Keyboard.KEYCODE_SHIFT -> {

                viewBinding.idCustomInput.run {
                    val myKeyboard = keyboard
                    when (shift_status) {
                        StaticValue.Shift_S -> {
                            shift_status = if (mouble) {
                                StaticValue.Shift_B_lo
                            } else {
                                StaticValue.Shift_B
                            }
                            keySB(true, myKeyboard)
                            keyboard = myKeyboard
                        }

                        StaticValue.Shift_B_lo -> {
                            shift_status = StaticValue.Shift_S
                            keySB(false, myKeyboard)
                            keyboard = myKeyboard
                        }

                        StaticValue.Shift_B -> {
                            shift_status = if (mouble) {
                                StaticValue.Shift_B_lo
                            } else {
                                keySB(false, myKeyboard)
                                StaticValue.Shift_S
                            }
                            keyboard = myKeyboard
                        }
                    }

                }


            }

            Keyboard.KEYCODE_DONE -> {
                currentInputConnection.performEditorAction(EditorInfo.IME_ACTION_DONE)
            }

            Keyboard.KEYCODE_MODE_CHANGE -> {
                viewBinding.idCustomInput.run {
                    if (xmlMode == StaticValue.xml0) {
                        changeXml(1)
                    } else {
                        changeXml(0)
                    }
                }

            }

            StaticValue.SHIFT_NUMBER -> {

                changeXml(2)
            }

            Keyboard.KEYCODE_DELETE -> {
                currentInputConnection.deleteSurroundingText(1, 0)
            }

            StaticValue.SHIFT_SYMBOL -> {
                changeXml(1)
            }


            else -> {
                currentInputConnection.commitText(primaryCode.toChar().toString(), 1)

                viewBinding.idCustomInput.keyboard = viewBinding.idCustomInput.apply {
                    if (shift_status == StaticValue.Shift_B) {
                        shift_status = StaticValue.Shift_S
                        keySB(false, viewBinding.idCustomInput.keyboard)
                    }

                }.keyboard

            }
        }
    }

    override fun onText(text: CharSequence?) {

    }

    override fun swipeLeft() {

    }

    override fun swipeRight() {

    }

    override fun swipeDown() {

    }

    override fun swipeUp() {

    }
}