package com.xty.base.act

import android.os.Build
import android.os.Bundle
import android.view.*
import android.view.View.NO_ID
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.OnKeyboardListener
import com.tamsiree.rxkit.RxActivityTool
import com.xty.base.R
import com.xty.common.LogUtils
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.math.roundToInt


abstract class IBaseAct : AppCompatActivity() {
    val bundle by lazy { Bundle() }
    var isDarkBar=true
    var isKeyBoard=true
    val mainScope by lazy {  MainScope() }
    val myGestureDetector by lazy { GestureDetector(this,MyGestureDetector()) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        openCache()
        setContentView(setLayout())
        EventBus.getDefault().register(this)
        RxActivityTool.addActivity(this)
        initData()
        initView()
    }

    abstract fun setLayout(): View

    open fun initData() {}
    open fun initView() {
        initImmbar()
    }

    open fun  openCache(){}


    private fun initImmbar() {
      ImmersionBar.with(this).statusBarDarkFont(isDarkBar).keyboardEnable(isKeyBoard)
          .setOnKeyboardListener { isPopup, _ -> keyBoardHind(isPopup) }
          .init()
    }
    open fun keyBoardHind(isPopup: Boolean){

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return myGestureDetector.onTouchEvent(event)  //拦截暴力点击
    }

    inner class MyGestureDetector : GestureDetector.SimpleOnGestureListener() {

        override fun onContextClick(e: MotionEvent?): Boolean {
            return super.onContextClick(e)
        }

        override fun onDoubleTap(e: MotionEvent?): Boolean {
            LogUtils.d("doubleClick", "连续2次电极了傲！！！ " )
            return true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mainScope.cancel()
        RxActivityTool.removeActivity(this)
        EventBus.getDefault().unregister(this)
    }

    @Subscribe (threadMode = ThreadMode.MAIN)
    fun noneEvent(none: Any?){
    }


    /**
     * 状态栏适配
     */
    fun statusBar(view:View){
        view.post {
            var  statusBarHeight1 = -1;
            var  resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                val layoutParams = view.layoutParams as ViewGroup.LayoutParams
                statusBarHeight1 = getStatusBarHeight()
                layoutParams.height=statusBarHeight1
                view.layoutParams=layoutParams
            }

        }
    }

    /**
     * 状态栏的高度
     */
    fun getStatusBarHeight():Int{
        var  statusBarHeight1 = -1;
        var  resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = resources.getDimensionPixelSize(resourceId);

        }
        return statusBarHeight1
    }

    private val NAVIGATION = "navigationBarBackground"

    // 该方法需要在View完全被绘制出来之后调用，否则判断不了
      fun isNavigationBarExist(): Boolean {
        val vp =  window.decorView as ViewGroup
        if (vp != null) {
            for (i in 0 until vp.childCount) {
                vp.getChildAt(i).context.packageName
                if (vp.getChildAt(i).id != NO_ID && NAVIGATION ==  getResources()
                        .getResourceEntryName(vp.getChildAt(i).id)
                ) {
                    return true
                }
            }
        }
        return false
    }

    fun dp2px(dps: Int): Int {
        return (resources.displayMetrics.density * dps).roundToInt()
    }

}