package com.yzx.ebrand.model

import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils

class  BaseUser(var token: String,
                var type:Int,
                var role:String,
                var user_id:Int,
                var unit_id:Int,
                var school_id:Int,
                var unit_name:String,
                var school_name:String,
                var direct_login:Int,
                var tmp_token:String,
                var phone:String,
)

class UserAvatar(val identity:String,val head_portrait:String)

class WxQrTicket(val ticket:String)

class User(
    var user_name: String,
    var user_id: Int,
    var account_type: Int,
    var role: String,
    var token: String,
    var school_id: Int,
    var phone: String,
    var ticket: String,
    var loginTime: Long,
    var unit_name:String,
    var school_name:String,
) {
    fun save() {
        val sp = SPUtils.getInstance()
        sp.put("user_name", user_name)
        sp.put("user_id", user_id)
        sp.put("token", token)
        sp.put("school_id", school_id)
        sp.put("phone", phone)
        sp.put("ticket", ticket)
        sp.put("account_type", account_type)
        sp.put("role", role)
        sp.put("loginTime", TimeUtils.getNowMills())
        sp.put("unit_name", if (unit_name.isNullOrBlank()){school_name}else{unit_name})
    }

    companion object {
        fun getUser(): User {
            val sp = SPUtils.getInstance()
            return User(
                sp.getString("user_name", ""),
                sp.getInt("user_id", 0),
                sp.getInt("account_type", 0),
                sp.getString("role", ""),
                sp.getString("token", ""),
                sp.getInt("school_id", 0),
                sp.getString("phone", ""),
                sp.getString("ticket", ""),
                sp.getLong("loginTime", 0),
                sp.getString("unit_name", ""),
                sp.getString("unit_name", ""),
            )
        }

        fun isLogin(): Boolean {
            val now = TimeUtils.getNowMills()
            val user = getUser()
            if (user.token.isNullOrBlank()) {
                return false
            }
            if (now - user.loginTime > 24 * 60 * 60 * 1000) {
                return false
            }
            return true
        }

        fun getUnitCount(): Int = SPUtils.getInstance().getInt("unit_count", 1)

        fun saveUnitCount(count: Int) {
            SPUtils.getInstance().put("unit_count", count);
        }

        fun saveLastLoginTime(time:Long){
            val sp = SPUtils.getInstance()
            sp.put("loginTime", time)
        }

        fun clearUserInfo() {
            val sp = SPUtils.getInstance()
            sp.remove("user_name")
            sp.remove("user_id")
            sp.remove("account_type")
            sp.remove("role")
            sp.remove("token")
            sp.remove("school_id")
            sp.remove("phone")
            sp.remove("ticket")
            sp.remove("loginTime")
            sp.remove("unit_name")
        }
    }
}