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


@objcMembers open class ListPageTemplateModel: ThreeLayerModelBase {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open override class var identifier: String { "list" }
    
    public var molecules: [ListItemModelProtocol & MoleculeModelProtocol]?
    public var line: LineModel?
    public var scrollToRowIndex: Int?
    public var singleCellSelection: Bool = false
    
    public override var rootMolecules: [MoleculeModelProtocol] {
        if let molecules = molecules {
            return super.rootMolecules + molecules
        }
        return super.rootMolecules
    }
    
    /// This template requires content.
    func validateModelHasContent() throws {
        if header == nil,
           molecules?.count ?? 0 == 0,
           footer == nil {
            throw ModelRegistry.Error.decoderOther(message: "List template requires atleast one of the following: header, footer, molecules")
        }
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(pageType: String, screenHeading: String?, molecules: [ListItemModelProtocol & MoleculeModelProtocol]) {
        super.init(pageType: pageType)
        self.screenHeading = screenHeading
        self.molecules = molecules
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case molecules
        case line
        case scrollToRowIndex
        case singleCellSelection
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        molecules = try typeContainer.decodeModelsIfPresent(codingKey: .molecules)
        line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line)
        scrollToRowIndex = try typeContainer.decodeIfPresent(Int.self, forKey: .scrollToRowIndex)
        if let singleCellSelection = try typeContainer.decodeIfPresent(Bool.self, forKey: .singleCellSelection) {
            self.singleCellSelection = singleCellSelection
        }
        try super.init(from: decoder)
        try validateModelHasContent()
    }
    
    open override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeModelsIfPresent(molecules, forKey: .molecules)
        try container.encode(line, forKey: .line)
        try container.encode(singleCellSelection, forKey: .singleCellSelection)
        try container.encodeIfPresent(scrollToRowIndex, forKey: .scrollToRowIndex)
    }
}
