package com.dahe.gldriver.utils

import android.content.Context
import android.content.Intent
import com.dahe.gldriver.push.PushHelper
import com.dahe.gldriver.ui.HomeActivity
import com.dahe.gldriver.ui.LauncherActivity
import com.dahe.gldriver.ui.account.LoginActivity
import com.dahe.gldriver.ui.account.SelectRoleActivity
import com.dahe.glex.bean.UserBean
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.BaseSPUtils
import com.dahe.mylibrary.utils.ToastUtils
import com.google.gson.Gson

class UserUtils private constructor(){

    companion object{
        val instance = Holder.holder
    }

    private object Holder {
        val holder = UserUtils()
    }

    fun loginOut(context : Context){
        if (null== SPUtils.instance.getUserInfo(context))
            return
        //删除推送别名
        PushHelper.delete(context)
        BaseSPUtils.cleanUserInfo(context)
        val intent = Intent(context, LauncherActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(intent)
        ToastUtils.showToast(context, "您的账号再异地登录，请重新登录")
    }

    fun loginIn(context: Context,userBean: UserBean){
        SPUtils.instance.setUserInfo(context, Gson().toJson(userBean))
        //设置推送别名
        PushHelper.setAlias(context,userBean)
        if (userBean.isCarCaptain == "9") {//是否是车队长（0=否,1=是,9=未选择）
            ActivityUtils.startActivity(
                context,
                SelectRoleActivity::class.java
            )
        } else {
            ActivityUtils.startActivity(
                context,
                HomeActivity::class.java
            )
        }
        ActivityUtils.finishToActivity(LoginActivity::class.java,true)

    }
}