//
//  YHLoginManager.swift
//  galaxy
//
//  Created by EDY on 2024/2/22.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHLoginManager: NSObject {
    static let shared = YHLoginManager()
    
    var userModel: YHUserModel?
    private override init() {}
    
    func isLogin() -> Bool {
        let ttt = userModel?.token?.count ?? 0
        if ttt != 0 {
            return true
        }
        return false
    }
    
    // 保存用户数据到本地
    func saveLocalUserInfo() {
        if let userModel = userModel {
            UserDefaults.standard.set(userModel.toDictionary(), forKey: YhConstant.KLoginedInfoKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    // 清空用户数据
    func clearUserInfo() {
        userModel = nil
        UserDefaults.standard.set(nil, forKey: YhConstant.KLoginedInfoKey)
        UserDefaults.standard.synchronize()
    }
    
    func loadLocalUserInfoData() {
        let dic = UserDefaults.standard.value(forKey: YhConstant.KLoginedInfoKey)
        guard let resultModel = YHUserModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
            return
        }
        self.userModel = resultModel
    }
}
