package xn.hxp.receiver

import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.KeyEvent
import com.blankj.utilcode.util.LogUtils

class ScanKeyEventHelper(private val callback: ((content: String) -> Unit)? = null) {

    private var mCaps = false
    private var mKeyContent = StringBuilder()

    fun analysisKeyEvent(event: KeyEvent) {
        val keyCode = event.keyCode
        if (KeyEvent.KEYCODE_SHIFT_RIGHT == keyCode
            || KeyEvent.KEYCODE_SHIFT_LEFT == keyCode
        ) {
            mCaps = KeyEvent.ACTION_DOWN == event.action
        }

        if (KeyEvent.ACTION_DOWN != event.action) return

        if (KeyEvent.KEYCODE_ENTER == keyCode) {
            if (mKeyContent.isNotEmpty()) callback?.invoke(mKeyContent.toString())
            mKeyContent.clear()
        } else {
            decodeKeyCode(keyCode, event)?.let { keyChar ->
                mKeyContent.append(keyChar)
                if (mKeyContent.toString().length == 1) {
                    Handler(Looper.getMainLooper()).postDelayed({
                        callback?.invoke(mKeyContent.toString())
                        mKeyContent.clear()
                    }, 1500)
                }
            }

        }


    }

//    private fun decodeKeyCode(keyCode: Int): Char? {
//
//        return when (keyCode) {
//            in KeyEvent.KEYCODE_0..KeyEvent.KEYCODE_9 -> {
//                // 0:48=0x30
//                return (keyCode - KeyEvent.KEYCODE_0 + 0x30).toChar()
//            }
//
//            in KeyEvent.KEYCODE_A..KeyEvent.KEYCODE_Z -> {
//                // a:97=0x61 A:65=0x41
//                return (keyCode - KeyEvent.KEYCODE_A + if (mCaps) 0x41 else 0x61).toChar()
//            }
//            KeyEvent.KEYCODE_PERIOD -> '.'
//            KeyEvent.KEYCODE_MINUS -> if (mCaps) '_' else '-'
//            KeyEvent.KEYCODE_SLASH -> '/'
//            KeyEvent.KEYCODE_BACKSLASH -> if (mCaps) '|' else '\\'
//            KeyEvent.KEYCODE_EQUALS -> '=' // 等号
//
//            else -> {
//                null
//            }
//        }
//    }

//
//    private fun decodeKeyCode(keyCode: Int,event: KeyEvent): Char? {
//        val isShiftPressed = event.isShiftPressed
//        LogUtils.i("=======shifou $isShiftPressed  ${KeyEvent.KEYCODE_7}")
//        return when (keyCode) {
//            in KeyEvent.KEYCODE_0..KeyEvent.KEYCODE_9 -> {
//                return if(14==KeyEvent.KEYCODE_7 &&isShiftPressed){
//                    '&'
//                }else{
//                    // 0:48=0x30
//                    (keyCode - KeyEvent.KEYCODE_0 + 0x30).toChar()
//                }
//            }
//
//            in KeyEvent.KEYCODE_A..KeyEvent.KEYCODE_Z -> {
//                // a:97=0x61 A:65=0x41
//                return (keyCode - KeyEvent.KEYCODE_A + if (isShiftPressed) 0x41 else 0x61).toChar()
//            }
//            KeyEvent.KEYCODE_PERIOD -> '.'
//            KeyEvent.KEYCODE_MINUS -> if (isShiftPressed) '_' else '-'
//            KeyEvent.KEYCODE_SLASH -> '/'
//            KeyEvent.KEYCODE_BACKSLASH -> if (isShiftPressed) '|' else '\\'
//            KeyEvent.KEYCODE_EQUALS -> '=' // 等号
//
//
//            else -> {
//                null
//            }
//        }
//    }

    /**
     * 不知道乱写一通写的啥,总之处理了 但是结果是错的 勉强能用
     * @author 前同事之作
     */
    private fun decodeKeyCode(keyCode: Int, event: KeyEvent): Char? {
        val isShiftPressed = event.isShiftPressed

        if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
            return if (KeyEvent.KEYCODE_7 == 14 && isShiftPressed) {
                '&'
            } else {
                // 0:48=0x30
                (keyCode - KeyEvent.KEYCODE_0 + 0x30).toChar()
            }
        } else if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
            // a:97=0x61 A:65=0x41
            return (keyCode - KeyEvent.KEYCODE_A + if (isShiftPressed) 0x41 else 0x61).toChar()
        } else if (keyCode == KeyEvent.KEYCODE_PERIOD) {
            return '.'
        } else if (keyCode == KeyEvent.KEYCODE_MINUS) {
            return if (isShiftPressed) '_' else '-'
        } else if (keyCode == KeyEvent.KEYCODE_SLASH) {
            return '/'
        } else if (keyCode == KeyEvent.KEYCODE_BACKSLASH) {
            return if (isShiftPressed) '|' else '\\'
        } else if (keyCode == KeyEvent.KEYCODE_EQUALS) {
            return '=' // 等号
        } else {
            return null
        }
    }
}