//
//  Accessory+CoreDataClass.swift
//  Zockerhoehle
//
//  Created by Julian-Steffen Müller on 06.07.19.
//  Copyright © 2019 Julian-Steffen Müller. All rights reserved.
//
//

import Foundation
import CoreData

@objc(Accessory)
public class Accessory: NSManagedObject, Identifiable {
    @nonobjc public class func fetchRequest(console : Console) -> NSFetchRequest<Accessory> {
        let fetchRequest = NSFetchRequest<Accessory>(entityName: "Accessory")
        fetchRequest.predicate = NSPredicate(format: "console == %@", console)
        return fetchRequest
    }
}

/*
 @NSManaged public var color: String?
 @NSManaged public var manufacturer: String?
 @NSManaged public var name: String
 @NSManaged public var inWishlist: Bool
 @NSManaged public var console: Console?
 @NSManaged public var lentTo : String?
 @NSManaged public var uuid : UUID
 */

extension Accessory : Encodable {
    
    enum CodingKeys: String, CodingKey {
        case uuid
        case color
        case manufacturer
        case name
        case inWishlist
        case console
        case lentTo
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(uuid, forKey: .uuid)
        try container.encode(color, forKey: .color)
        try container.encode(manufacturer, forKey: .manufacturer)
        try container.encode(name, forKey: .name)
        try container.encode(inWishlist, forKey: .inWishlist)
        try container.encode(console?.uuid.uuidString ?? "", forKey: .console)
        try container.encode(lentTo, forKey: .lentTo)
    }
}
