//
//  RadioSwatchesModel.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 31/03/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


@objcMembers public class RadioSwatchesModel: MoleculeModelProtocol, FormFieldProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "radioSwatches"
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var swatches: [RadioSwatchModel]
    public var fieldKey: String?
    public var groupName: String = FormValidator.defaultGroupName
    public var baseValue: AnyHashable?
    public var enabled: Bool = true
    public var readOnly: Bool = false
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    /// Returns the fieldValue of the selected swatch, otherwise the text of selected swatch.
    public func formFieldValue() -> AnyHashable? {
        guard enabled else { return nil }
        let selectedSwatch = swatches.first { (swatch) -> Bool in
            return swatch.selected
        }
        return selectedSwatch?.fieldValue ?? selectedSwatch?.text
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case swatches
        case fieldKey
        case groupName
        case enabled
        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)
        swatches = try typeContainer.decode([RadioSwatchModel].self, forKey: .swatches)
        fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey)
        if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
            self.groupName = groupName
        }
        enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
        readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false
        baseValue = formFieldValue()
    }
    
    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(swatches, forKey: .swatches)
        try container.encodeIfPresent(fieldKey, forKey: .fieldKey)
        try container.encode(groupName, forKey: .groupName)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(readOnly, forKey: .readOnly)
    }
}
