import Foundation
import RealmSwift
import DifferenceKit

public class AudioRecord: Object, Identifiable, Differentiable, Cloneable {

    @objc public dynamic var path: String = ""
    @objc public dynamic var number: String?
    @objc public dynamic var rawText: String = ""
    @objc private dynamic var addedDate: TimeInterval = 0
    @objc public dynamic var duration: TimeInterval = 0
    @objc public dynamic var event: VehicleEvent?
    
    public var identifier: TimeInterval = 0
    public var id: TimeInterval {
        if self.identifier == 0 {
            self.identifier = self.addedDate
        }
        return self.identifier
    }
    
    public var differenceIdentifier: TimeInterval { id }
    
    public func isContentEqual(to source: AudioRecord) -> Bool {
        return self == source
    }
    
    public init(path: String, number: String?, raw: String, duration: TimeInterval, event: VehicleEvent?) {
        self.path = path
        self.number = number
        self.duration = duration
        self.rawText = raw
        self.event = event
        self.addedDate = Date().timeIntervalSince1970
    }
    
    required init() {
        super.init()
    }
    
    public override static func primaryKey() -> String? {
        return "path"
    }
    
    public override class func ignoredProperties() -> [String] {
        return ["id", "identifier", "differenceIdentifier"]
    }
    
    public func getAddedDate() -> TimeInterval {
        if self.identifier == 0 {
            self.identifier = self.addedDate
        }
        
        return self.addedDate
    }
    
    // MARK: - Cloneable
    
    required public init(copy: AudioRecord) {
        self.path = copy.path
        self.number = copy.number
        self.rawText = copy.rawText
        self.addedDate = copy.addedDate
        self.duration = copy.duration
        self.event = copy.event?.clone()
    }
}
