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

import Foundation
import MVMCore

@objcMembers public class TagModel: MoleculeModelProtocol {
    public static var identifier: String = "tag"
    public var id: String = UUID().uuidString
    
    public var label: LabelModel
    public var action: ActionModelProtocol?
    public var backgroundColor: Color?
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case label
        case action
        case backgroundColor
    }
    
    public func setDefaults() {
        if label.numberOfLines == nil {
            label.numberOfLines = 1
        }
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(label: LabelModel) {
        self.label = label
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        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(id, forKey: .id)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(label, forKey: .label)
        try container.encodeModelIfPresent(action, forKey: .action)
    }
}
