//
//  LabelToggle.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 1/15/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

public class LabelToggleModel: MoleculeModelProtocol {
    public static var identifier: String = "labelToggle"
    public var moleculeName: String = LabelToggleModel.identifier
    public var backgroundColor: Color?
    public var label: LabelModel
    public var toggle: ToggleModel

    init(_ label: LabelModel, _ toggle: ToggleModel) {
        self.label = label
        self.toggle = toggle
    }
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case label
        case toggle
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey:.backgroundColor)
        label = try typeContainer.decode(LabelModel.self, forKey:.label)
        toggle = try typeContainer.decode(ToggleModel.self, forKey:.toggle)
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encode(label, forKey: .label)
        try container.encode(toggle, forKey: .toggle)
    }
}
