package com.casic.br.utils

import com.casic.br.model.UserDetailModel
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.utils.SaveKeyValues

object DeserializeModel {

    private val gson by lazy { Gson() }

    fun getUserModel(): UserDetailModel.DataModel? {
        val userJson = SaveKeyValues.getValue(LocaleConstant.USER_MODEL, "") as String
        if (userJson == "") {
            return null
        }
        val userDetailModel = gson.fromJson<UserDetailModel>(
            userJson, object : TypeToken<UserDetailModel>() {}.type
        )
        return userDetailModel.data
    }

    fun getHomeId(): Long {
        val homeId = SaveKeyValues.getValue(LocaleConstant.HOME_ID, "") as String
        return if (homeId == "") {
            val userModel = getUserModel()
            if (userModel == null) {
                Long.MAX_VALUE
            } else {
                if (userModel.syncDeptid == "") {
                    Long.MAX_VALUE
                } else {
                    userModel.syncDeptid.toLong()
                }
            }
        } else {
            homeId.toLong()
        }
    }

    fun isLogin(): Boolean {
        val loginTime = SaveKeyValues.getValue(LocaleConstant.LOGIN_TIME, 0L) as Long
        return if (loginTime == 0L) {
            false
        } else {
            if (System.currentTimeMillis() - loginTime > 84600 * 1000L) {
                false
            } else {
                SaveKeyValues.getValue(LocaleConstant.USER_IS_LOGIN, false) as Boolean
            }
        }
    }
}