package com.yzx.escreen.activity.base

import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.yzx.escreen.presenter.base.BasePresenter
import com.yzx.escreen.utils.hideNavigationBar

/**
 * 类名：BaseActivity
 * 作者：Yun.Lei
 * 功能：
 * 创建日期：2020年5月6日14:27:04
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
abstract class BaseActivity<out P : BasePresenter<*>> : AppCompatActivity() {

    /**
     * 当前页面需要加载的layoutId，等价setContentView
     */
    abstract val inflateId: Int

    private var mLoadingDialog: QMUITipDialog? = null

    /**
     * 初始化视图操作在这里执行，执行时机为onCreate之后
     */
    abstract fun initView(): Unit

    /**
     * 数据初始化在这里执行，执行时机为initView之后
     */
    abstract fun initData(): Unit

    //kotlin 懒加载，在第一次使用Presenter时初始化，这种设计是针对一个View只针对一个Presenter。
    //多个Presenter的情况此处不应该使用泛型
    protected val mPresenter: P? by lazy { initPresenter() }

    abstract fun initPresenter(): P?


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        hideBottomUIMenu()
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
        actionBar?.hide()
        setContentView(inflateId)
        initView()
        initData()
        setStatusBar()

    }

    open fun setStatusBar() {
//        immersionBar {
//            statusBarColor(R.color.white)
//            fitsSystemWindows(false)
//            statusBarDarkFont(true, 0.2f)
//            keyboardEnable(true)
//            init()
//        }
    }


    private fun hideBottomUIMenu() {
        val params: WindowManager.LayoutParams = window.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        window.attributes = params
    }


    override fun onBackPressed() {
        super.onBackPressed()
    }

    override fun onDestroy() {
        super.onDestroy()

    }

    fun showDialogToast(
        msg: String,
        duration: Long = 1500,
        @QMUITipDialog.Builder.IconType icon: Int = QMUITipDialog.Builder.ICON_TYPE_SUCCESS
    ) {
        val tipDialog = QMUITipDialog.Builder(this)
            .setIconType(icon)
            .setTipWord(msg)
            .create()
        tipDialog.show()
        tipDialog.hideNavigationBar()
        window.decorView.postDelayed({
            tipDialog.dismiss()
        }, duration)

    }

    fun showLoading(msg: String = "加载中...") {
        mLoadingDialog = QMUITipDialog.Builder(this)
            .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
            .setTipWord(msg)
            .create()
        mLoadingDialog?.apply {
            show()
        }
    }

    fun hideLoading() {
        mLoadingDialog?.apply {
            if (isShowing) {
                dismiss()
            }
        }
    }

}