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

import Foundation
import CoreData
import UIKit

@objc(GameSeries)
public class GameSeries: NSManagedObject, Identifiable {

    public var id : String {
        return objectID.uriRepresentation().absoluteString
    }
    
    public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
        super.init(entity: entity, insertInto: context)
        
        self.uuid = UUID()
    }
}

extension GameSeries : Encodable {
    
    enum CodingKeys: String, CodingKey {
        case uuid
        case name
        case games
        case cover
        case cover_icloud_path
    }
    
    public func encode(to encoder: Encoder) throws {
        var container  = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(uuid, forKey: .uuid)
        try container.encode(name, forKey: .name)
        try container.encode(cover_icloud_path, forKey: .cover_icloud_path)
        
        var gamesList : [String] = []
        for game in games {
            if let game = game as? Game {
                gamesList.append(game.objectID.uriRepresentation().absoluteString)
            }
        }
        try container.encode(gamesList, forKey: .games)
        
    }
}
