//
//  PQSingletoMemoryUtil.swift
//  PQSpeed
//
//  Created by SanW on 2020/6/9.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit
import Photos
import BFCommonKit

// MARK: - 内存缓存数据

/// 内存缓存数据
public class PQSingletoMemoryUtil: NSObject {
    public static let shared = PQSingletoMemoryUtil()
    public var isShowAttendPoint: Bool = false // 是否有关注更新
    public var attendIdosUpdateCount: Int = 0 // 关注更新条数
    public var isLeftSlipRemind: Bool = false
    public var needLogin: Bool = false // 是否需要显示手机登录
    public var isFinishedCoging: Bool = false // 是否已完成配置请求
    public var isFinishedAlias: Bool = false // 是否完成绑定别名
    public var selectedTabIndex: String?
    public var commandVideoItem: BFVideoItemProtocol? // 口令内容
    public var commandLaunchParams: String = "" // 口令启动时的值
    public var commandReportParams: [String: Any]? // 口令启动时解析的其他值
    public var h5MsgVid: String? // 剪切板id
    public var isShowNoWiFiRemind: Bool = false // 是否提示过非WiFi提示
    public var isNeedRefreshAttend: Bool = false // 是否需要刷新关注
    public var isNeedRefreshJoinTopic: Bool = false // 是否需要刷新加入的话题
    
    // 剪切板content是否为空，如果为空走承接逻辑，冷启动不再加载缓存
    public var isEmptyPasteContent: Bool = false {
        didSet {
            BFLog(message: "isEmptyPasteContent = \(isEmptyPasteContent)")
        }
    }

    // 展示无服务提示视图（剪切板 data.reportData.downloadButtonType为 weapp_share_noServiceDownloadApp/weapp_categoryTab_noServiceDownloadApp
    public var isShowPasteNoServeView: Bool = false {
        didSet {
            BFLog(message: "isShowPasteNoServeView = \(isShowPasteNoServeView)")
        }
    }
    /// 是否正在展示广告view
    public var isLoadingSplashAdView: Bool = false {
        didSet {
            BFLog(message: "isLoadingSplashAdView = \(isLoadingSplashAdView)")
        }
    }
    public var cutBoardInfo: String? // 剪切板信息
    public var cutBoard: String? // 剪切板来源cutBoardInfo
    public var isPushLoading: Bool = false // 是否正在加载推送数据

    public var coldLaunchType: coldLaunchType?
    public var isColdLaunch: Bool = false // 冷启动
    public var coldLaunchStatus: Int = 0 // 1-请求中 2-请求成功 3-请求失败
    public var deviceToken: String = "" // 推送deviceToken
//    var activityData: PQActivityModel? // 活动数据信息
    public var isShowTodaySuccess: Bool = false // 是否已经展示过今日已完成
    public var sessionId: String = getUniqueId(desc: "sessionId")
    public var subSessionid: String?
    public var isDefaultAtten: Bool = false // 是否默认进入关注
    public var isDefaultMineSingleVideoDetail: Bool = false // 是否默认进入我的单个视频详情
    public var playCount: Int = 0 // 播放次数
    public var uploadDatas: [BFVideoItemProtocol] = Array<BFVideoItemProtocol>.init() // 正在上传的视频集合
    public var isShowUpSlideGuide: Bool = false // 是否展示了滑动向上的提示
    public var showUpSlideData: PQVideoListModel? // 是否展示了滑动向上的提示
    public var makeVideoProjectId: String? // 制作视频项目Id
    public var draftboxId: String? // 草稿箱ID
    // add by ak 结构化数据 saveDraft api 要 用到的参数
    public var sdata: String?
    // add by ak 结构化数据 saveDraft api 要 用到的参数
    public var title: String?
    // add by ak 结构化数据 saveDraft api 要 用到的参数
    public var coverUrl: String?

    // 未读数字
//    var unReadInfo: PQMsgReadInfoModel?
    public var abInfoData: [String: Any] = Dictionary<String, Any>.init() // 实验数据
    public var allExportSession: [PHAsset:AVAssetExportSession] = [PHAsset:AVAssetExportSession].init()
    public func updateTabPoint() {
//        let tabBar =
//            rootViewController()?.tabBar
//        if isShowAttendPoint {
//            tabBar?.showPoint(index: 1)
//        } else {
////          tabBar.removePoint(index: 1)
//        }
    }

    /// 创建sessionId
    /// - Returns: <#description#>
   public func createSesstionId() {
        sessionId = getUniqueId(desc: "sessionId")
        subSessionid = sessionId
        BFLog(message: "生成的sessionId = \(sessionId)")
    }

    /// 创建subSessionid
    /// - Returns: <#description#>
    public func createSubSesstionId() {
        subSessionid = getUniqueId(desc: "subSessionid")
        BFLog(message: "生成的subSessionid = \(String(describing: subSessionid))")
    }

    /// 制作视频项目Id
    /// - Returns: <#description#>
    public func createMakeVideoProjectId() {
        makeVideoProjectId = cProjectIdPrefix + getUniqueId(desc: "makeVideoProjectId")
        BFLog(message: "生成的projectId = \(String(describing: makeVideoProjectId))")
    }

    /// 解析abInfoData
    /// - Parameter abInfo: <#abInfo description#>
    /// - Returns: description
    public func parasABinfoData(abInfo: String?) {
        if abInfo != nil, (abInfo?.count ?? 0) > 0, abInfo != "{}" {
            guard let infoDic = jsonStringToDictionary(abInfo!) else { return }
            abInfoData.merge(infoDic, uniquingKeysWith: { (key, _) -> Any in
                key
            })
            BFLog(message: "=====abInfoData = \(abInfoData)")
        }
    }

    override private init() {
        super.init()
        selectedTabIndex = getUserDefaults(key: cSelectedTabIndex) as? String
        if selectedTabIndex == nil {
            saveUserDefaults(key: cSelectedTabIndex, value: "categoryTab")
            selectedTabIndex = "categoryTab"
        }
    }

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

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