//
//  PQEditBaseModel.swift
//  PQSpeed
//
//  Created by ak on 2020/12/19.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import ObjectMapper
import RealmSwift
import BFCommonKit

// 自定义时长转换协议，服务单位为微秒，我们使用 S
public let timeTransform = TransformOf<Float64, Float64>(fromJSON: { (value: Float64?) -> Float64? in
    // transform value from String? to Int?
    (value ?? 0) / 1_000_000
}, toJSON: { (value: Float64?) -> Float64? in
    // transform value from Int? to String?
    if let value = value {
        return value * 1_000_000
    }
    return nil
})

// 自定义音量转换协议，服务器单位0.0-1.0，我们使用 0-100
public let volumeGainTransform = TransformOf<Float64, Float64>(fromJSON: { (value: Float64?) -> Float64? in
    BFLog(message: "value is \(String(describing: value))")
    return (value ?? 0.0) * 100.0
}, toJSON: { (value: Float64?) -> Float64? in
    if let value = value {
        return value / 100.0
    }
    return nil
})
// 自定义 sectionIndex 转换协议服务器是从 1 开始，我们是从0开始
public let sectionIndexTransform = TransformOf<Int, Int>(fromJSON: { (value: Int?) -> Int? in
    (value ?? 0) - 1
}, toJSON: { (value: Int?) -> Int? in
    if let value = value {
        return value + 1
    }
    return nil
})

open class PQEditBaseModel: Object, Mappable ,NSCopying{
    @objc dynamic public var uniqueId: String = getUniqueId(desc: "uniqueId")
    public func mapping(map _: Map) {}

    required convenience public init?(map _: Map) {
        self.init()
    }

    override required public init() {
        super.init()
    }
    
    public func copy(with zone: NSZone? = nil) -> Any {
            let editBaseModel = PQEditBaseModel()
            editBaseModel.uniqueId = uniqueId
            return editBaseModel
        }
}
