package com.grkj.iscs_mc.view.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.grkj.iscs_mc.view.dialog.LoadingProgressDialog
import com.grkj.iscs_mc.util.ActivityUtils
import pub.devrel.easypermissions.EasyPermissions

/**
 * 页面基类
 */
abstract class BaseActivity<T : ViewBinding?> : AppCompatActivity() {

    protected var mBinding: T? = null
    abstract val viewBinding: T
    private var loadingProgressDialog: LoadingProgressDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = viewBinding
        setContentView(mBinding!!.root)
        ActivityUtils.addActivity(this)
        initView()
    }

    /**
     * 控件初始化
     */
    abstract fun initView()

    override fun onDestroy() {
        super.onDestroy()
        mBinding = null
        ActivityUtils.removeActivity(this)
    }

    fun handleLoading(isShow: Boolean, progressStr: String? = null) {
        if (isShow) {
            loadingProgressDialog ?: let {
                loadingProgressDialog = LoadingProgressDialog(this)
            }
            loadingProgressDialog?.show()
            loadingProgressDialog?.setProgress(progressStr)
        } else {
            loadingProgressDialog?.dismiss()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    /**
     * 退出页面，默认直接finish
     */
    open fun exitPage() {
        finish()
    }
}