package com.sixin.module_p.ui.enter.mine

import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.launcher.ARouter
import com.lukouguoji.module_base.base.BaseViewModel
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.db.perference.SharedPreferenceUtil
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showConfirmDialog
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.service.repository.LoginRepository
import com.lukouguoji.module_base.util.WebSocketUtil
import com.sixin.module_p.ui.set.PdaSetActivity
import com.sixin.module_p.ui.user.about.PdaAboutUsActivity

class PdaMineViewModel : BaseViewModel() {

    val userName = SharedPreferenceUtil.getString(Constant.Share.userName).noNull()
    val userRole = MutableLiveData("计重员")

    fun onInit() {
        launchCollect({
            NetApply.api.getUserRole(
                SharedPreferenceUtil.getString(Constant.Share.account).noNull()
            )
        }) {
            onSuccess = {
                if (it.verifySuccess()) {
                    userRole.value = it.data?.roleName ?: ""
                }
            }
        }
    }

    fun onModifyPasswordClick() {
        PdaSetActivity.start(getTopActivity())
    }

    fun onAboutUsClick() {
        PdaAboutUsActivity.start(getTopActivity())
    }

    fun onLogoutClick() {
        getTopActivity().showConfirmDialog("确定要退出账号吗？") {
            LoginRepository.logout()
            WebSocketUtil.closeWebSocket()
            ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_LOGIN).navigation()
            FlowBus.with<Any>(ConstantEvent.LOGOUT)
                .tryEmit(true)
        }
    }

}