//
//  RadioBoxModel.swift
//  MVMCoreUI
//
//  Created by Dhamodaram Nandi on 31/03/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

@objcMembers public class RadioBoxModel: MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "radioBox"
    public var text: String
    public var subText: String?
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var selectedAccentColor: Color?
    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 text
        case subText
        case selectedAccentColor
        case backgroundColor
        case accessibilityIdentifier
        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)
        text = try typeContainer.decode(String.self, forKey: .text)
        subText = try typeContainer.decodeIfPresent(String.self, forKey: .subText)
        selectedAccentColor = try typeContainer.decodeIfPresent(Color.self, forKey: .selectedAccentColor)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        
        if let isSelected = try typeContainer.decodeIfPresent(Bool.self, forKey: .selected) {
            selected = isSelected
        }
        
        enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
        readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false
        
        if let isStrikeTrough = try typeContainer.decodeIfPresent(Bool.self, forKey: .strikethrough) {
            strikethrough = isStrikeTrough
        }
        
        fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue)
        action = try typeContainer.decodeModelIfPresent(codingKey: .action)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(text, forKey: .text)
        try container.encodeIfPresent(subText, forKey: .subText)
        try container.encodeIfPresent(selectedAccentColor, forKey: .selectedAccentColor)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encode(selected, forKey: .selected)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(readOnly, forKey: .readOnly)
        try container.encode(strikethrough, forKey: .strikethrough)
        try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
        try container.encodeModelIfPresent(action, forKey: .action)
    }
}
