//
//  RadioButtonModel.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 2/26/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import MVMCore


open class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "radioButton"
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var state: Bool = false
    public var enabled: Bool = true
    public var readOnly: Bool = false
    
    /// The specific value to send to server. TODO: update this to be more generic.
    public var fieldValue: String?
    
    public var baseValue: AnyHashable?
    public var groupName: String = FormValidator.defaultGroupName
    public var fieldKey: String?
    public var action: ActionModelProtocol?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case state
        case enabled
        case fieldValue
        case fieldKey
        case groupName
        case action
        case readOnly
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(_ state: Bool) {
        self.state = state
        baseValue = state
    }
    
    //--------------------------------------------------
    // MARK: - Validation
    //--------------------------------------------------
    
    public func formFieldValue() -> AnyHashable? {
        guard enabled else { return nil }
        return fieldValue
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        if let state = try typeContainer.decodeIfPresent(Bool.self, forKey: .state) {
            self.state = state
        }

        enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
        readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        
        baseValue = state
        fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey)
        if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
            self.groupName = groupName
        }
        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.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(state, forKey: .state)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(readOnly, forKey: .readOnly)
        try container.encodeIfPresent(fieldKey, forKey: .fieldKey)
        try container.encodeIfPresent(groupName, forKey: .groupName)
        try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
        try container.encodeModelIfPresent(action, forKey: .action)
    }
}
