//
//  TagModel.swift
//  MVMCoreUI
//
//  Created by Dhamodaram Nandi on 29/05/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

@objcMembers public class TagModel: MoleculeModelProtocol {
    public static var identifier: String = "tag"
    public var label: LabelModel
    public var action: ActionModelProtocol?
    public var backgroundColor: Color?
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case label
        case action
        case backgroundColor
    }
    
    public func setDefaults() {
        if label.numberOfLines == nil {
            label.numberOfLines = 1
        }
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        label = try typeContainer.decode(LabelModel.self, forKey: .label)
        action = try typeContainer.decodeModelIfPresent(codingKey: .action)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(label, forKey: .label)
        try container.encodeModelIfPresent(action, forKey: .action)
    }
}
