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


/// A model for a collection item that is a container for any molecule.
@objcMembers open class MoleculeCollectionItemModel: MoleculeContainerModel, CollectionItemModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open override class var identifier: String { "collectionItem" }
    
    public var action: ActionModelProtocol?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case action
    }

    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    public override func setDefaults() {
        if useHorizontalMargins == nil {
            useHorizontalMargins = true
        }
        if useVerticalMargins == nil {
            useVerticalMargins = true
        }
        if topPadding == nil {
            topPadding = PaddingDefault
        }
        if bottomPadding == nil {
            bottomPadding = PaddingDefault
        }
    }
    
    //--------------------------------------------------
    // MARK: - Initialization
    //--------------------------------------------------
    
    public override init(with moleculeModel: MoleculeModelProtocol) {
        super.init(with: moleculeModel)
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------

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

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