//
//  RadioSwatchModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 4/17/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers public class RadioSwatchModel: MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "radioSwatch"
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var color: Color = Color(uiColor: .mvmBlue)
    public var text: String?
    public var selected: Bool = false
    public var enabled: Bool = true
    public var readOnly: Bool = false
    public var strikethrough: Bool = false
    public var fieldValue: String?
    public var action: ActionModelProtocol?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case color
        case text
        case selected
        case enabled
        case strikethrough
        case fieldValue
        case action
        case readOnly
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .color) {
            self.color = color
        }
        
        text = try typeContainer.decodeIfPresent(String.self, forKey: .text)
        
        if let selected = try typeContainer.decodeIfPresent(Bool.self, forKey: .selected) {
            self.selected = selected
        }

        if let strikethrough = try typeContainer.decodeIfPresent(Bool.self, forKey: .strikethrough) {
            self.strikethrough = strikethrough
        }
        
        fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue)
        action = try typeContainer.decodeModelIfPresent(codingKey: .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.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encode(color, forKey: .color)
        try container.encodeIfPresent(text, forKey: .text)
        try container.encode(selected, forKey: .selected)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(strikethrough, forKey: .strikethrough)
        try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
        try container.encodeModelIfPresent(action, forKey: .action)
        try container.encode(readOnly, forKey: .readOnly)
    }
}
