import Foundation
import RealmSwift
import DifferenceKit

class AudioRecord: Object, Identifiable, Differentiable, Cloneable {

    @objc dynamic var path: String = ""
    @objc dynamic var number: String?
    @objc dynamic var rawText: String = ""
    @objc private dynamic var addedDate: TimeInterval = 0
    @objc dynamic var duration: TimeInterval = 0
    @objc dynamic var event: VehicleEvent?
    
    var identifier: TimeInterval = 0
    var id: TimeInterval {
        if self.identifier == 0 {
            self.identifier = self.addedDate
        }
        return self.identifier
    }
    
    var differenceIdentifier: TimeInterval { id }
    
    func isContentEqual(to source: AudioRecord) -> Bool {
        return self == source
    }
    
    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()
    }
    
    override static func primaryKey() -> String? {
        return "path"
    }
    
    override class func ignoredProperties() -> [String] {
        return ["id", "identifier", "differenceIdentifier"]
    }
    
    func getAddedDate() -> TimeInterval {
        if self.identifier == 0 {
            self.identifier = self.addedDate
            print("getAddedDate: \(self.identifier)")
        }
        
        return self.addedDate
    }
    
    // MARK: - Cloneable
    
    required 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()
    }
}
