//
//  HeartModel.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 07/09/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


open class HeartModel: MoleculeModelProtocol, EnableableModelProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "heart"
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var isActive: Bool = false
    public var activeColor: Color = Color(uiColor: .mvmRed)
    public var inActiveColor: Color = Color(uiColor: .clear)
    public var action: ActionModelProtocol = ActionNoopModel()
    public var enabled: Bool = true
    public var readOnly: Bool = false

    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case isActive
        case activeColor
        case inActiveColor
        case action
        case enabled
        case readOnly
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        if let isActive = try typeContainer.decodeIfPresent(Bool.self, forKey: .isActive) {
            self.isActive = isActive
        }
        
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        
        if let activeColor = try typeContainer.decodeIfPresent(Color.self, forKey: .activeColor) {
            self.activeColor = activeColor
        }
        
        if let inActiveColor = try typeContainer.decodeIfPresent(Color.self, forKey: .inActiveColor) {
            self.inActiveColor = inActiveColor
        }
        
        if let action: ActionModelProtocol = try typeContainer.decodeModelIfPresent(codingKey: .action) {
            self.action = action
        }
        enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
        readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(isActive, forKey: .isActive)
        try container.encode(activeColor, forKey: .activeColor)
        try container.encode(inActiveColor, forKey: .inActiveColor)
        try container.encodeModel(action, forKey: .action)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(readOnly, forKey: .readOnly)
    }
}
