package com.xhly.corelib.base.ui

import android.app.Activity
import android.content.Intent
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity

open class CommonBaseActivity : AppCompatActivity() {
    var lastHeight: Int = 0
    fun setSoftInput(rootLinearLayout: ViewGroup) {
        //有edittext和srcollview时使用,多edittext可能需要搭配  android:windowSoftInputMode="adjustPan" 以及 <item name="android:windowBackground">@color/white</item>
        if (Build.VERSION.SDK_INT >= 21) {
            window.decorView.viewTreeObserver.addOnGlobalLayoutListener {
                var r: Rect = Rect()
                window.decorView.getWindowVisibleDisplayFrame(r)
                if (lastHeight != r.bottom) {
                    lastHeight = r.bottom
                    var layayoutParams: ViewGroup.LayoutParams = rootLinearLayout.layoutParams
                    layayoutParams.height = r.bottom - rootLinearLayout.top
                    rootLinearLayout.layoutParams = layayoutParams
                    windowChange()
                }
            }
        }
    }

    open fun windowChange() {
        //window内容变动时调用，可以用来处理edittext光标和检测输入法的隐藏与显示
        try {
            if (!isSoftShowing(this)) {

            } else {

            }
        } catch (e: Exception) {

        }
    }

    fun isSoftShowing(activity: Activity): Boolean {
        //获取当前屏幕内容的高度
        val screenHeight = activity.window.decorView.height
        //获取View可见区域的bottom
        val rect = Rect()
        //DecorView即为activity的顶级view
        activity.window.decorView.getWindowVisibleDisplayFrame(rect)
        //考虑到虚拟导航栏的情况（虚拟导航栏情况下：screenHeight = rect.bottom + 虚拟导航栏高度）
        //选取screenHeight*2/3进行判断
        return screenHeight * 2 / 3 > rect.bottom
    }

    public fun startIntentActivity(activity: Class<AppCompatActivity>, bundle: Bundle? = null) {
        var intent = Intent(this, activity)
        try {
            if (bundle != null) {
                intent.putExtras(bundle)
            }
        } catch (e: Exception) {

        }
        startActivity(intent)
    }
}