  */
 import UIKit
 import CoreLocation
+import UIColor_Hex_Swift
 
 class ChirpPerformance : NSObject, NSCoding {
     /// Array of `TouchRecord`s to store performance data.

        aCoder.encode(instrument, forKey: PropertyKey.instrumentKey)
        aCoder.encode(UIImagePNGRepresentation(image), forKey: PropertyKey.imageKey)
        aCoder.encode(location, forKey: PropertyKey.locationKey)
    }

    required convenience init?(coder aDecoder: NSCoder) {
        guard let data = aDecoder.decodeObject(forKey: PropertyKey.performanceDataKey) as? [TouchRecord]
            else {return nil}
        guard
            //let data = aDecoder.decodeObject(forKey: PropertyKey.performanceDataKey) as? [TouchRecord],
            let date = aDecoder.decodeObject(forKey: PropertyKey.dateKey) as? Date,
            let performer = aDecoder.decodeObject(forKey: PropertyKey.performerKey) as? String,
            let instrument = aDecoder.decodeObject(forKey: PropertyKey.instrumentKey) as? String,
            let image = UIImage(data: (aDecoder.decodeObject(forKey: PropertyKey.imageKey) as? Data)!)
            else {return nil}
        let location = (aDecoder.decodeObject(forKey: "location") as? CLLocation) ?? CLLocation.init(latitude: 60, longitude: 11)
        
        print("PERF: Decoding", data.count, "notes:", performer, instrument)
        self.init(data: data, date: date, performer: performer, instrument: instrument, image: image, location: location)
    }

    /// Main initialiser
    init(data: [TouchRecord], date: Date, performer: String, instrument: String, image: UIImage, location: CLLocation) {
        self.performanceData = data
        self.date = date
        self.performer = performer
        self.instrument = instrument
        self.image = image
        self.location = location
        super.init()
    }
    
    /// Initialiser with csv of data for the TouchRecords, useful in initialising performances from CloudKit
    convenience init?(csv: String, date: Date, performer: String, instrument: String, image: UIImage, location: CLLocation) {
        var data : [TouchRecord] = []
        let lines = csv.components(separatedBy: "\n")
        // TODO: test this initialiser
        data = lines.flatMap {TouchRecord.init(fromCSVLine: $0)}
        self.init(data: data, date: date, performer: performer, instrument: instrument, image: image, location: location)
    }
    
    
    /// Convenience Initialiser for creating performance with data yet to be added.
    convenience override init() {
        // FIXME: actually detect the proper location
        self.init(data : [], date : Date(), performer : "", instrument : "", image : UIImage(), location: CLLocation.init(latitude: 90.0, longitude: 45.0))
    }

    /// Returns a CSV of the current performance data

    var image : UIImage
    var csvPathURL : URL?
    var location : CLLocation?
    var colour : UIColor = UIColor.blue

    // Static vars
    static let CSV_HEADER = "time,x,y,z,moving\n"
