package com.xty.base.act

import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.tamsiree.rxkit.RxActivityTool
import com.xty.base.R
import com.xty.base.vm.BaseVm
import com.xty.base.weight.DeleteDialog
import com.xty.base.weight.TipDialog
import com.xty.common.LogUtils
import com.xty.common.MMkvHelper
import com.xty.common.ReflexUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.LoginOutEvent
import com.xty.common.weight.LoadingView
import org.greenrobot.eventbus.EventBus
import pub.devrel.easypermissions.EasyPermissions

abstract class BaseVmAct<V:BaseVm>: IBaseAct() , EasyPermissions.PermissionCallbacks ,EasyPermissions.RationaleCallbacks {

    lateinit var mViewModel:V
    val loadingView by lazy {   LoadingView.Builder(this).setCancelable(true).cteated() }
    val deleteDialog by lazy {   DeleteDialog(this ,  "删除后列表信息将被移除,\n请确认是否删除?") {
        deleteData()
    } }
    override fun initView() {
        super.initView()
        val superType = ReflexUtils.getSuperType(javaClass) as Class<V>
        if(superType!=null){
            mViewModel= ViewModelProvider(this)[superType]
        }

        mViewModel.nowData.observe(this){
            loadingView.dismiss()
            MMkvHelper.clearUserInfo()
            EventBus.getDefault().post(LoginOutEvent())
            /*if(componentName.className!="LoginAct" ){
                ToastUtils.show(R.string.again_login)
                RxActivityTool.finishAllActivity()
                MMkvHelper.clearUserInfo()
                RouteManager.goAct(ARouterUrl.LOGIN_ACT)
            }*/
        }
        mViewModel.loading=loadingView
        liveObserver()

    }

   abstract  fun liveObserver()

   open fun deleteData(){}

    open fun onSuccess(requestCode: Int) {}
    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        LogUtils.e("BaseVmAct","onPermissionsGranted${perms.size}")
        onSuccess(requestCode)
    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        LogUtils.e("BaseVmAct","onPermissionsDenied${perms.size}")
    }


    override fun onRationaleAccepted(requestCode: Int) {
        LogUtils.e("BaseVmAct","onRationaleAccepted")
    }

    override fun onRationaleDenied(requestCode: Int) {
        LogUtils.e("BaseVmAct","onRationaleDenied")
    }
}