//
//  RadioButtonModel.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 5/14/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


@objcMembers public class RadioButtonSelectionHelper: FormFieldProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var fieldKey: String?
    public var groupName: String = FormValidator.defaultGroupName
    private var selectedRadioButton: RadioButton?
    private var selectedRadioButtonModel: RadioButtonModel?
    public var baseValue: AnyHashable?
    public var enabled: Bool = true
    public var readOnly: Bool = false
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public func set(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton) {
        self.fieldKey = radioButtonModel.fieldKey
        self.groupName = radioButtonModel.groupName
        
        if radioButtonModel.state {
            if self.baseValue == nil,
                let selected = radioButtonModel.baseValue as? Bool, selected {
                self.baseValue = radioButtonModel.fieldValue
            }
            selectedRadioButtonModel = radioButtonModel
            
            // Below code is needed for cell resuse scenario.
            radioButton.isSelected = true
            selectedRadioButton = radioButton
        } else {
            radioButton.isSelected = false
        }
        self.enabled = radioButtonModel.enabled
        self.readOnly = radioButtonModel.readOnly
    }
    
    //--------------------------------------------------
    // MARK: - Functions
    //--------------------------------------------------
    
    public static func setupForRadioButtonGroup(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton, delegateObject: MVMCoreUIDelegateObject?) {
        
        guard let groupName = radioButtonModel.fieldKey,
            let formValidator = delegateObject?.formHolderDelegate?.formValidator
            else { return }
        
        let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper()
        radioButtonSelectionHelper.set(radioButtonModel, radioButton)
        formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper
        FormValidator.setupValidation(for: radioButtonSelectionHelper, delegate: delegateObject?.formHolderDelegate)
    }
    
    public func selected(_ radioButton: RadioButton) {
        
        // Checks because the view could be reused
        if selectedRadioButton?.radioModel === selectedRadioButtonModel {
            selectedRadioButton?.isSelected = false
        } else {
            selectedRadioButtonModel?.state = false
        }
        
        selectedRadioButton = radioButton
        selectedRadioButton?.isSelected = true
        selectedRadioButtonModel = selectedRadioButton?.radioModel
    }
}

// MARK: - FormValidationFormFieldProtocol
extension RadioButtonSelectionHelper {
    
    public func formFieldValue() -> AnyHashable? {
        guard enabled else { return nil }
        return selectedRadioButtonModel?.fieldValue
    }
}
