//
//  BFLoginUserInfo.swift
//  PQSpeed
//
//  Created by SanW on 2020/5/27.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit
import BFCommonKit

// MARK: - 登录用户信息

/// 登录用户信息s
public class BFLoginUserInfo: NSObject {
    static public let shared = BFLoginUserInfo()
    public var uid: String = "" // 账号
    public  var userCode: String = "" // 账号
    public var accessToken: String = "" // token
    public var avatarUrl: String = "" // 头像
    public var city: String = "" // 城市
    public var province: String = "" // 省市
    public  var country: String = "" // 国家
    public  var phoneNumber: String = "" // 电话
    public var openId: String = "" // 微信openId
    public var nickName: String = "" // 昵称
    public var gender: String = "" // 性别
    public var expiredTime: String = "" // 过期时间
    public var videos: String = "0" // 视频数
    public  var idols: String = "0" // 关注数
    public var fans: String = "0" // 粉丝数
    public var otherSubscribes: String = "0" // 别人订阅我的数量
    public  var userStatus: String = "1" // 1有效，2 已删除，3 已屏蔽，4 敏感
    public var isVirtualUser: Bool = false // 是否是虚拟账号
    public  var mid = getMachineCode() // 设备ID

    @objc  public  func toString() -> String {
        let json: [String: Any] = [
            "accessToken": accessToken,
            "avatarUrl": avatarUrl,
            "city": city,
            "country": country,
            "gender": gender,
            "nickName": nickName,
            "openId": openId,
            "phoneNumber": phoneNumber,
            "province": province,
            "uid": uid,
            "userCode": userCode,
            "expiredTime": expiredTime,
            "videos": videos,
            "idols": idols,
            "fans": fans,
            "userStatus": userStatus,
            "isVirtualUser": isVirtualUser,
            "mid": mid,
        ]
        return dictionaryToJsonString(json) ?? ""
    }

    override public init() {
        super.init()
        resetData(isClear: false)
        if getUserDefaults(key: cMineVideos) != nil {
            videos = getUserDefaults(key: cMineVideos) as! String
        }
        if getUserDefaults(key: cMineIdols) != nil {
            idols = getUserDefaults(key: cMineIdols) as! String
        }
        if getUserDefaults(key: cMineFans) != nil {
            fans = getUserDefaults(key: cMineFans) as! String
        }
        if getUserDefaults(key: cOtherSubscribes) != nil {
            otherSubscribes = getUserDefaults(key: cOtherSubscribes) as! String
        }
        if getUserDefaults(key: cAvatarUrl) != nil {
            avatarUrl = getUserDefaults(key: cAvatarUrl) as! String
        }
        if getUserDefaults(key: cUpdatePhone) != nil {
            phoneNumber = getUserDefaults(key: cUpdatePhone) as! String
        }
    }

    @objc  public  func resetData(isClear: Bool) {
        if isClear {
            UserDefaults.standard.removeObject(forKey: cUserInfoStorageKey)
            UserDefaults.standard.removeObject(forKey: cMineVideos)
            UserDefaults.standard.removeObject(forKey: cMineFans)
            UserDefaults.standard.removeObject(forKey: cOtherSubscribes)
            UserDefaults.standard.removeObject(forKey: cMineIdols)
            UserDefaults.standard.removeObject(forKey: cAvatarUrl)
            UserDefaults.standard.removeObject(forKey: cUpdatePhone)
            UserDefaults.standard.synchronize()

            accessToken = ""
            avatarUrl = ""
            city = ""
            country = ""
            gender = ""
            nickName = ""
            openId = ""
            phoneNumber = ""
            province = ""
            uid = ""
            userCode = ""
            expiredTime = ""
            videos = "0"
            idols = "0"
            fans = "0"
            userStatus = "1"
            isVirtualUser = false
            mid = ""
            return
        }
        let userInfo: [String: Any] = jsonStringToDictionary(UserDefaults.standard.string(forKey: cUserInfoStorageKey) ?? "") ?? [:]
        updateData(userInfo: userInfo)
    }

    public  func updateData(userInfo: [String: Any]?) {
        if userInfo != nil, userInfo?.count ?? 0 > 0 {
            accessToken = "\(userInfo?["accessToken"] ?? "")"
            if userInfo?.keys.contains("token") ?? false {
                accessToken = "\(userInfo?["token"] ?? "")"
            }
            avatarUrl = "\(userInfo?["avatarUrl"] ?? "")"
            city = "\(userInfo?["city"] ?? "")"
            country = "\(userInfo?["country"] ?? "")"
            gender = "\(userInfo?["gender"] ?? "")"
            nickName = "\(userInfo?["nickName"] ?? "")"
            openId = "\(userInfo?["openId"] ?? "")"
            if userInfo?.keys.contains("phoneNumber") ?? false, !(userInfo?["phoneNumber"] is NSNull) {
                phoneNumber = "\(userInfo?["phoneNumber"] ?? "")"
            }
            if userInfo?.keys.contains("userStatus") ?? false, !(userInfo?["userStatus"] is NSNull) {
                userStatus = "\(userInfo?["userStatus"] ?? "")"
            }
            if userInfo?.keys.contains("mid") ?? false, !(userInfo?["mid"] is NSNull) {
                mid = "\(userInfo?["mid"] ?? "")"
            }
            if userInfo?.keys.contains("isVirtualUser") ?? false, !(userInfo?["isVirtualUser"] is NSNull) {
                isVirtualUser = (userInfo?["isVirtualUser"] as? Bool) ?? false
            }
            province = "\(userInfo?["province"] ?? "")"
            uid = "\(userInfo?["uid"] ?? "")"
            userCode = "\(userInfo?["userCode"] ?? "")"
            expiredTime = "\(userInfo?["expiredTime"] ?? "")"
            if getUserDefaults(key: cMineVideos) != nil {
                videos = getUserDefaults(key: cMineVideos) as! String
            }
            if getUserDefaults(key: cAvatarUrl) != nil {
                avatarUrl = getUserDefaults(key: cAvatarUrl) as! String
            }
            if getUserDefaults(key: cUpdatePhone) != nil {
                phoneNumber = getUserDefaults(key: cUpdatePhone) as! String
            }
            if getUserDefaults(key: cMineIdols) != nil {
                idols = getUserDefaults(key: cMineIdols) as! String
            }
            if getUserDefaults(key: cMineFans) != nil {
                fans = getUserDefaults(key: cMineFans) as! String
            }
            if getUserDefaults(key: cOtherSubscribes) != nil {
                otherSubscribes = getUserDefaults(key: cOtherSubscribes) as! String
            }
            PQBFConfig.shared.token = accessToken
            PQBFConfig.shared.loginUid = uid
            PQBFConfig.shared.uid = uid
        }
    }

    public  func isLogin() -> Bool {
        return accessToken.count > 0
    }

    public override func copy() -> Any {
        return self
    }

    public override func mutableCopy() -> Any {
        return self
    }
}
