//
//  NotificationXButtonModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 9/15/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation
import MVMCore

public class NotificationXButtonModel: ButtonModelProtocol, MoleculeModelProtocol {
    
    public static var identifier: String = "notificationXButton"
    public var id: String = UUID().uuidString
    
    public var backgroundColor: Color?
    public var action: ActionModelProtocol = ActionNoopModel()
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case action
    }
    
    public init(action: ActionModelProtocol = ActionNoopModel()) {
        self.action = action
    }
    
    public required init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        if let action: ActionModelProtocol = try typeContainer.decodeModelIfPresent(codingKey: .action) {
            self.action = action
        }
    }
    
    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.encodeModel(action, forKey: .action)
    }
}
