//
//  MoleculeStackItem.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 10/4/19.
//  Copyright © 2019 Suresh, Kamlesh. All rights reserved.
//


@objcMembers public class MoleculeStackItemModel: MoleculeContainerModel, StackItemModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public override class var identifier: String { "stackItem" }
    public var spacing: CGFloat?
    public var percent: Int?
    public var gone: Bool = false
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case spacing
        case percent
        case gone
        case moleculeName
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    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)
        spacing = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .spacing)
        percent = try typeContainer.decodeIfPresent(Int.self, forKey: .percent)
        if let gone = try typeContainer.decodeIfPresent(Bool.self, forKey: .gone) {
            self.gone = gone
        }
        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.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(spacing, forKey: .spacing)
        try container.encodeIfPresent(percent, forKey: .percent)
        try container.encode(gone, forKey: .gone)
    }
}
