package com.mime.dualscreenview.activity

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.SpannableStringBuilder
import android.text.style.RelativeSizeSpan
import android.view.Gravity
import android.view.KeyEvent
import android.view.KeyEvent.KEYCODE_BACK
import android.view.KeyEvent.KEYCODE_MEDIA_FAST_FORWARD
import android.view.KeyEvent.KEYCODE_MEDIA_NEXT
import android.view.KeyEvent.KEYCODE_MEDIA_PREVIOUS
import android.view.KeyEvent.KEYCODE_MEDIA_REWIND
import android.view.KeyEvent.KEYCODE_VOLUME_DOWN
import android.view.KeyEvent.KEYCODE_VOLUME_MUTE
import android.view.KeyEvent.KEYCODE_VOLUME_UP
import android.view.MotionEvent
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.mime.dualscreenview.common.Blog
import com.mime.dualscreenview.data.HistoryManager
import com.mime.dualscreenview.data.model.HistoryItem
import com.mime.dualscreenview.data.model.LastInfo
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.types.BaseRealmObject
import kotlin.reflect.KClass


open class Base : AppCompatActivity() {

    inner class BaseHandler(looper : Looper  = Looper.getMainLooper()) : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
        }
    }
    val baseHandler = BaseHandler()
    var didBackPress = false

    val onBackPressRunnable = Runnable {
        didBackPress = false
    }

    val backPressString = arrayOf(
        "이퓨 워너 백투웹?\n플리즈~\n한번더 뒤로가기!!",
        "이퓨 워너 종료?\n플리즈~\n한번더 뒤로가기!~!",
    )
    fun firstBackPress() {
        didBackPress = true
        val origin = "이퓨 워너 종료?\n플리즈~ 한번더 뒤로가기!~!"
        val biggerText = SpannableStringBuilder(origin)
        biggerText.setSpan(RelativeSizeSpan(1.6f), 0, origin.length, 0)
        Toast.makeText(baseContext,biggerText,Toast.LENGTH_LONG).apply {
            setGravity(Gravity.CENTER, 0, 0)
        }.show()
        baseHandler.postDelayed(onBackPressRunnable,1500L)
    }


    var actionButtonPressX = 0f
    var actionButtonPressY = 0f
    override fun dispatchGenericMotionEvent(ev: MotionEvent?): Boolean {
        if (ev?.device?.name?.contains("BLE-M3") == true) {
            Blog.LOGE("keyEvent >>>>>  dispatchGenericMotionEvent ${ev}")
            ev?.action?.let { action ->
                when(action) {
                    MotionEvent.ACTION_HOVER_ENTER -> {
                        return false
                    }
                    MotionEvent.ACTION_HOVER_MOVE ->{return false}
                    MotionEvent.ACTION_BUTTON_PRESS ->{
                        if (actionButtonPressX * actionButtonPressY == 0f) {
                            actionButtonPressX = ev.x ?: 0f
                            actionButtonPressY = ev.y ?: 0f
                        }
                        return true
                    }
                    MotionEvent.ACTION_BUTTON_RELEASE ->{
                        
                        if (actionButtonPressY == ev.y) {
                            if (actionButtonPressX.minus(ev.x ?: 0f) > 0f) {
                                Blog.LOGE("Arrow Right Click")
                                onKeyClick(KeyEvent.KEYCODE_VOLUME_DOWN)
                            } else {
                                Blog.LOGE("Arrow Left Click")
                                onKeyClick(KeyEvent.KEYCODE_VOLUME_UP)
                            }
                        } else {
                            if (actionButtonPressY.minus(ev.y ?: 0f) > 0f) {
                                Blog.LOGE("Arrow Down Click")
                            } else {
                                Blog.LOGE("Arrow Up Click")
                            }
                        }
                        return false
                    }
                    MotionEvent.ACTION_HOVER_EXIT ->{
                        actionButtonPressX = 0f
                        actionButtonPressY = 0f
                        return false
                    }
                    else -> {return false}
                }
            }
        }
        return super.dispatchGenericMotionEvent(ev)
    }
    

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        Blog.LOGD(log = "keyCode : ${keyCode}, event : ${event}")
        return  when(keyCode) {
            KEYCODE_VOLUME_DOWN -> { true }
            KEYCODE_VOLUME_UP -> { true }
            KEYCODE_VOLUME_MUTE -> { true }
            KEYCODE_MEDIA_FAST_FORWARD -> { true }
            KEYCODE_MEDIA_REWIND -> { true }
            KEYCODE_MEDIA_PREVIOUS -> { true }
            KEYCODE_MEDIA_NEXT -> { true }
            KEYCODE_BACK -> { false }
            else -> {
                super.onKeyDown(keyCode, event)
            }
        }
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        Blog.LOGD(log = "keyCode : ${keyCode}, event : ${event}")
        return  when(keyCode) {
            KEYCODE_VOLUME_DOWN -> {
                onKeyClick(keyCode)
                true
            }
            KEYCODE_VOLUME_UP -> {
                onKeyClick(keyCode)
                true
            }
            KEYCODE_VOLUME_MUTE -> {
                onKeyClick(keyCode)
                true }
            KEYCODE_BACK -> {
//                onBackPressed()
                this.onBackPressed()
                false
            }
            KEYCODE_MEDIA_FAST_FORWARD -> { true }
            KEYCODE_MEDIA_REWIND -> { true }
            KEYCODE_MEDIA_PREVIOUS -> { true }
            KEYCODE_MEDIA_NEXT -> { true }
            else -> {
                super.onKeyDown(keyCode, event)
            }
        }
    }

    open fun onKeyClick(keyCode: Int) : Boolean {
        when(keyCode) {
            KEYCODE_VOLUME_DOWN -> {
                true
            }
            KEYCODE_VOLUME_UP -> {
                true
            }
            KEYCODE_VOLUME_MUTE -> {

            }
            KEYCODE_MEDIA_FAST_FORWARD -> { true }
            KEYCODE_MEDIA_REWIND -> { true }
            KEYCODE_MEDIA_PREVIOUS -> { true }
            KEYCODE_MEDIA_NEXT -> { true }
            else -> {

            }
        }
        return false
    }

    fun openRealm() : Realm = HistoryManager.openRealm

}