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

import UIKit

open class PQBaseModel: NSObject {
    @objc dynamic public var uniqueId: String? // 唯一ID
    @objc dynamic public var videoId: Int = 0 // 视频ID
    @objc dynamic public var eventId: String? // 事件ID
    @objc dynamic public var title: String? // 标题
    @objc dynamic public var attributedTitle: NSMutableAttributedString? // 富文本标题
    @objc dynamic public var summary: String? // 描述
    @objc dynamic public var imageUrl: String = "" // 图片地址
    @objc dynamic public var selectedImage: String = "" // 图片地址
    @objc dynamic public var isSelected: Bool = false
    @objc dynamic public var recommendLogVO: String? // 推荐日志对象
    @objc dynamic public var abInfoData: String? // AB
    @objc dynamic public var pageCategoryId: Int = 0 // 页面分类ID
    @objc dynamic public var version: String = versionName // 版本号
    @objc dynamic public var mid = getMachineCode() // 设备ID
    @objc dynamic public var date: Int = 0 // 当前时间戳  CGFloat(Date.init().timeIntervalSince1970) * 1000
    @objc dynamic public var itemWidth: Float = 0 // cell宽
    @objc dynamic public var primaryKeys: String? // 区分存储唯一值

    public override required  init() {
        super.init()
        uniqueId = getUniqueId(desc: "uniqueId")
    }


    @objc func toString() -> String {
        var json: [String: Any] = [
            "version": version,
            "mid": mid,
            "pageCategoryId": pageCategoryId,
            "selectedImage": selectedImage,
            "isSelected": isSelected,
            "imageUrl": imageUrl,
        ]
        if uniqueId != nil {
            json["uniqueId"] = uniqueId
            json["videoId"] = videoId
        }
        if eventId != nil {
            json["eventId"] = eventId
        }
        if title != nil {
            json["title"] = title
        }
        if summary != nil {
            json["summary"] = summary
        }
        if recommendLogVO != nil {
            json["recommendLogVO"] = recommendLogVO
        }
        return dictionaryToJsonString(json) ?? ""
    }

    public init(jsonDict: [String: Any]) {
        super.init()
        if jsonDict.keys.contains("id") {
            uniqueId = "\(jsonDict["id"] ?? "")"
            videoId = Int(uniqueId ?? "0") ?? 0
        }
        if jsonDict.keys.contains("uniqueId") {
            uniqueId = "\(jsonDict["uniqueId"] ?? "")"
            videoId = Int(uniqueId ?? "0") ?? 0
        }
        if jsonDict.keys.contains("eventId") {
            eventId = "\(jsonDict["eventId"] ?? "")"
        }
        if jsonDict.keys.contains("title") {
            title = "\(jsonDict["title"] ?? "")"
        }
        if jsonDict.keys.contains("summary") {
            summary = "\(jsonDict["summary"] ?? "")"
        }
        if jsonDict.keys.contains("imageUrl") {
            imageUrl = "\(jsonDict["imageUrl"] ?? "")"
        }
        if jsonDict.keys.contains("selectedImage") {
            selectedImage = "\(jsonDict["selectedImage"] ?? "")"
        }
        if jsonDict.keys.contains("isSelected") {
            isSelected = jsonDict["isSelected"] as! Bool
        }
        if jsonDict.keys.contains("recommendLogVO") {
            recommendLogVO = "\(jsonDict["recommendLogVO"] ?? "")"
        }
        if jsonDict.keys.contains("pageCategoryId") {
            pageCategoryId = Int("\(jsonDict["pageCategoryId"] ?? "0")") ?? 0
        }
        if jsonDict.keys.contains("version") {
            version = "\(jsonDict["version"] ?? "")"
        }
        if jsonDict.keys.contains("mid") {
            mid = "\(jsonDict["mid"] ?? "")"
        }
    }
}

// MARK: - 当前应用本地存储的model

/// 当前应用本地存储的model
public  class PQLocalStoreModel: PQBaseModel {
    @objc dynamic public var currentDate: String?
    @objc required init() {
        super.init()
        currentDate = systemCurrentDate()
    }
}

// MARK: - oss上传model

/// oss上传model
public class PQOssUploadModel: NSObject {
    public var accessKeyId: String?
    public var secretKeyId: String?
    public var securityToken: String?
    public var endpoint: String?
    public var endpoints: [String]?
    public var bucketName: String?
    public var fileName: String?
    public var uploadID: String?
    public var expiration: String? // 过期时间
    public init(jsonDict: [String: Any]) {
        super.init()

        if jsonDict.keys.contains("AccessKeyId") {
            accessKeyId = "\(jsonDict["AccessKeyId"] ?? "")"
        }
        if jsonDict.keys.contains("AccessKeySecret") {
            secretKeyId = "\(jsonDict["AccessKeySecret"] ?? "")"
        }
        if jsonDict.keys.contains("SecurityToken") {
            securityToken = "\(jsonDict["SecurityToken"] ?? "")"
        }
        if jsonDict.keys.contains("Hosts") {
            endpoints = jsonDict["Hosts"] as? [String]
        }
        if jsonDict.keys.contains("Host") {
            endpoint = "\(jsonDict["Host"] ?? "")"
            if endpoint != nil {
                if endpoints == nil {
                    endpoints = [endpoint!]
                } else {
                    endpoints?.append(endpoint!)
                }
            }
        }
        if jsonDict.keys.contains("Bucket") {
            bucketName = "\(jsonDict["Bucket"] ?? "")"
        }
        if jsonDict.keys.contains("FileName") {
            fileName = "\(jsonDict["FileName"] ?? "")"
        }
        if jsonDict.keys.contains("Upload") {
            uploadID = "\(jsonDict["Upload"] ?? "")"
        }
        if jsonDict.keys.contains("Expiration") {
            expiration = "\(jsonDict["Expiration"] ?? "")"
        }
    }
}

// MARK: - 空白页面model

/// 空白页面model
public class PQEmptyModel: NSObject {
    public var title: String? // 标题
    public var summary: String? // 描述
    public var emptyImage: String? // 空白提示图
    public var isRefreshHidden: Bool = true // 是否隐藏刷新按钮
    public var refreshImage: String? // 刷新按钮图片
    public var refreshTitle: NSMutableAttributedString? // 刷新按钮文字
    public var refreshBgColor: UIColor? // 刷新按钮背景颜色
}
