package com.swago.baseswago.util

import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import com.google.gson.Gson
import com.swago.baseswago.model.LoginModel
import com.swago.baseswago.model.UserInfoModel

/**
 *@date 2021/8/28 10:38
 *description:
 */
object UserInfo {

    private var userInfoModel: UserInfoModel? = null
    private var loginModel:LoginModel? = null

    fun setLoginInfo(loginModel: LoginModel){
        Firebase.analytics.setUserId(loginModel.user_account)
        Firebase.crashlytics.setUserId(loginModel.user_account)
        this.loginModel = loginModel
    }

    fun getLoginModel():LoginModel?{
        if (loginModel == null){
            val infoJson = SpUtil.readString("login_info")
            if (infoJson.isNullOrEmpty()) {
                return null
            } else {
                loginModel = Gson().fromJson(infoJson, LoginModel::class.java)
            }
        }
        return loginModel
    }

    fun getUserInfo(): UserInfoModel? {
        if (userInfoModel == null) {
            val infoJson = SpUtil.readString("user_info")
            if (infoJson.isNullOrEmpty()) {
                return null
            } else {
                userInfoModel = Gson().fromJson(infoJson, UserInfoModel::class.java)
            }
        }
        return userInfoModel
    }

    fun setUserInfo(userInfoModel: UserInfoModel) {
        this.userInfoModel = userInfoModel
    }

    fun clearUserInfo() {
        loginModel= null
        userInfoModel = null
    }
}