package com.develop.base.mvvm

import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.develop.base.util.FragmentKit
import com.smarx.notchlib.NotchScreenManager

/**
 * 基类activity，绑定viewBinding
 */
abstract class BaseBindingActivity<T : ViewBinding> : AppCompatActivity() {
    protected lateinit var binding: T

    private val viewModelProviderOfActivity: ViewModelProvider by lazy {
        ViewModelProvider(this)
    }

    fun <T : ViewModel> getViewModel(type: Class<T>): T {
        return viewModelProviderOfActivity.get(type)
    }


    val mainHandler by lazy {
        Handler(Looper.getMainLooper())
    }

    abstract fun createViewBinding(inflater: LayoutInflater): T


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //去掉title/actionBar/支持显示到刘海区域
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        );
        supportActionBar?.hide()
        NotchScreenManager.getInstance().setDisplayInNotch(this)
        binding = createViewBinding(layoutInflater)
        setContentView(binding.root)
    }


    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        setFullscreen()
    }

    /**全屏显示*/
    private fun setFullscreen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            window.attributes.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.setDecorFitsSystemWindows(false)
            window.insetsController?.apply {
                hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
                systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            }
        } else {
            window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_IMMERSIVE
                    or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
        }

    }


    //=============================fragment相关==========================/
    fun addFragment(layoutId: Int, fragment: Fragment) {
        FragmentKit.addFragment(this, fragment::class.java, layoutId)
    }

    fun replaceFragment(layoutId: Int, fragment: Fragment) {
        FragmentKit.replaceFragment(this, fragment::class.java, layoutId)
    }

    fun replaceFragment(layoutId: Int, fragment: Fragment, bundle: Bundle) {
        FragmentKit.replaceFragment(this, fragment::class.java, layoutId, bundle, false)
    }

    fun ensureFragmentAdd(layoutId: Int, fragment: Fragment) {
        val existed = supportFragmentManager.findFragmentByTag(
            fragment::class.java.simpleName
        )
        if (existed != null) {
            return
        }
        supportFragmentManager
            .beginTransaction()
            .add(layoutId, fragment, fragment::class.java.simpleName)
            .commitAllowingStateLoss()
    }

    fun replaceFragmentInstance(layoutId: Int, fragment: Fragment) {
        supportFragmentManager
            .beginTransaction()
            .replace(layoutId, fragment)
            .commitAllowingStateLoss()
    }


    open fun handleBackPress() {
        finish()
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.x == 0f && event.y == 0f) {
            return true
        }
        return super.onTouchEvent(event)
    }
}