//
//  MoleculeContainerModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 1/6/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


open class MoleculeContainerModel: ContainerModel, MoleculeContainerModelProtocol, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open class var identifier: String { "container" }
    public var backgroundColor: Color?
    public var molecule: MoleculeModelProtocol
    
    public var children: [MoleculeModelProtocol] {
        return [molecule]
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case molecule
        case backgroundColor
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(with moleculeModel: MoleculeModelProtocol) {
        molecule = moleculeModel
        super.init()
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        molecule = try typeContainer.decodeModel(codingKey: .molecule)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        try super.init(from: decoder)
    }
    
    open override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeModel(molecule, forKey: .molecule)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
    }
}
