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

@objcMembers public class RadioBoxesModel: MoleculeModelProtocol, FormFieldProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "radioBoxes"
    public var boxes: [RadioBoxModel]
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var selectedAccentColor: Color?
    public var boxesColor: Color?
    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 box, otherwise the text of the selected box.
    public func formFieldValue() -> AnyHashable? {
        guard enabled else { return nil }
        let selectedBox = boxes.first { (box) -> Bool in
            return box.selected
        }
        return selectedBox?.fieldValue ?? selectedBox?.text
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case enabled
        case readOnly
        case selectedAccentColor
        case backgroundColor
        case accessibilityIdentifier
        case boxesColor
        case boxes
        case fieldKey
        case groupName
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        selectedAccentColor = try typeContainer.decodeIfPresent(Color.self, forKey: .selectedAccentColor)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        boxesColor = try typeContainer.decodeIfPresent(Color.self, forKey: .boxesColor)
        boxes = try typeContainer.decode([RadioBoxModel].self, forKey: .boxes)
        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.encode(boxes, forKey: .boxes)
        try container.encodeIfPresent(selectedAccentColor, forKey: .selectedAccentColor)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encodeIfPresent(fieldKey, forKey: .fieldKey)
        try container.encode(groupName, forKey: .groupName)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(readOnly, forKey: .readOnly)
    }
}
