//
//  SectionListTemplateModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 9/25/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


public protocol SectionListHeaderFooterModel { }


@objcMembers open class SectionModel: Codable {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var header: (SectionListHeaderFooterModel & MoleculeModelProtocol)?
    public var footer: (SectionListHeaderFooterModel & MoleculeModelProtocol)?
    public var rows: [ListItemModelProtocol & MoleculeModelProtocol]
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case header
        case footer
        case rows
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        header = try typeContainer.decodeModelIfPresent(codingKey: .header)
        footer = try typeContainer.decodeModelIfPresent(codingKey: .footer)
        rows = try typeContainer.decodeModels(codingKey: .rows)
    }
    
    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeModelIfPresent(header, forKey: .header)
        try container.encodeModelIfPresent(footer, forKey: .footer)
        try container.encodeModels(rows, forKey: .rows)
    }
}

@objcMembers open class SectionListTemplateModel: ListPageTemplateModel {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open override class var identifier: String { "sectionList" }
    
    public var sections: [SectionModel]
    
    override func validateModelHasContent() throws {
        if header == nil,
           sections.count == 0,
           footer == nil {
            throw ModelRegistry.Error.decoderOther(message: "Section List template requires atleast one of the following: header, footer, sections")
        }
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case sections
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        sections = try typeContainer.decode([SectionModel].self, forKey: .sections)
        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(sections, forKey: .sections)
    }
}
