//
//  UserData.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/7.
//

import Foundation
import RxSwift

public let USER = UserData.default

open class UserData {
    public static let `default` = UserData()
    
    public let refreshTokenSub = ReplaySubject<Bool>.create(bufferSize: 1)
    
    public var isLogin : Bool {
        if token?.isEmpty == false {
            return true
        }
        return false
    }
    
    // 不需要记录
    public var token : String? {
        return UserDefaults.standard.object(forKey: token_key) as? String
    }
    
    func setToken(token:String?) {
        UserDefaults.standard.set(token, forKey: token_key)
    }
    
    // 需要记录
    public var supplierName : String? {
        return UserDefaults.standard.object(forKey: supplierName_key) as? String
    }
    
    func setSupplierName(supplierName:String?) {
        UserDefaults.standard.set(supplierName, forKey: supplierName_key)
    }
    
    // 需要记录
    public var phone : String? {
        return UserDefaults.standard.object(forKey: phone_key) as? String
    }
    
    func setPhone(phone:String?) {
        UserDefaults.standard.set(phone, forKey: phone_key)
    }
    
    // 不需要记录
    public var refreshToken : String? {
        return UserDefaults.standard.object(forKey: refresh_token_key) as? String
    }
    
    public func setRefreshToken(token: String?) {
        UserDefaults.standard.set(token, forKey: refresh_token_key)
    }
    
    // 不需要记录,1是子公司，2是服务商
    public var supplierType : Int? {
        return UserDefaults.standard.object(forKey: supplierType_key) as? Int
    }
    
    func setSupplierType(supplierType:Int?) {
        UserDefaults.standard.set(supplierType, forKey: supplierType_key)
    }
    
    // 不需要记录
    public var supplierId : Int? {
        return UserDefaults.standard.object(forKey: supplierId_key) as? Int
    }
    
    func setSupplierId(supplierId:Int?) {
        UserDefaults.standard.set(supplierId, forKey: supplierId_key)
    }
    
    // 不需要记录
    public var userId : Int? {
        return UserDefaults.standard.object(forKey: userId_key) as? Int
    }
    
    func setUserId(userId:Int?) {
        UserDefaults.standard.set(userId, forKey: userId_key)
    }
    
    public var username : String? {
        return UserDefaults.standard.object(forKey: username_key) as? String
    }
    
    func setUsername(username: String?) {
        UserDefaults.standard.set(username, forKey: username_key)
    }
    
    public var deviceId : String?

    public var regId : String?

    func showGongdanpiciAlert() {
        GongdanpiciTool.default.selectCountBySupplierId()
    }
    
    func clear() {
        setToken(token: nil)
        setRefreshToken(token: nil)
        setSupplierType(supplierType: nil)
        setUserId(userId: nil)
        setSupplierId(supplierId: nil)
        setUsername(username: nil)
        
        GongdanpiciTool.default.refreshCount()
    }
}
