//
//  ThreeLayerPageTemplateModel.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 11/22/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


@objcMembers public class ThreeLayerPageTemplateModel: ThreeLayerModelBase {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public override class var identifier: String { "threeLayer" }
    public var middle: MoleculeModelProtocol?
    
    public override var rootMolecules: [MoleculeModelProtocol] {
        if let middle = middle {
            return super.rootMolecules + [middle]
        }
        return super.rootMolecules
    }
    
    //--------------------------------------------------
    // MARK: - Init
    //--------------------------------------------------
    
    public init(pageType: String, header: MoleculeModelProtocol?, middle: MoleculeModelProtocol?, footer: MoleculeModelProtocol?) {
        super.init(pageType: pageType)
        self.header = header
        self.middle = middle
        self.footer = footer
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------

    private enum CodingKeys: String, CodingKey {
        case middle
    }

    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        middle = try typeContainer.decodeModelIfPresent(codingKey: .middle)
        try super.init(from: decoder)
    }

    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeModelIfPresent(header, forKey: .middle)
    }
}
