//
//  BaseItemPickerEntryField.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 2/10/21.
//  Copyright © 2021 Verizon Wireless. All rights reserved.
//

import UIKit

public typealias TextFieldAndPickerDelegate = (UITextFieldDelegate & UIPickerViewDelegate & UIPickerViewDataSource)


open class BaseItemPickerEntryField: BaseDropdownEntryField, UIPickerViewDelegate, UIPickerViewDataSource {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    open lazy var pickerView = UIPickerView.addPicker(to: textField, delegate: self, dismissAction: #selector(dismissFieldInput))
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    /// Closure passed here will run as picker changes items.
    public var observeDropdownChange: ((String?, String) -> ())?
    
    /// Closure passed here will run upon dismissing the selection picker.
    public var observeDropdownSelection: ((String) -> ())?
    
    /// When selecting for first responder, allow initial selected value to appear in empty text field.
    public var setInitialValueInTextField = true
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    @objc open override func setupFieldContainerContent(_ container: UIView) {
        super.setupFieldContainerContent(container)

        textField.hideBlinkingCaret = true
        textField.autocorrectionType = .no
        uiTextFieldDelegate = self
    }
    
    @objc public func setPickerDelegates(delegate: UIPickerViewDelegate & UIPickerViewDataSource) {
        
        pickerView.delegate = delegate
        pickerView.dataSource = delegate
    }
    
    //--------------------------------------------------
    // MARK: - Molecular
    //--------------------------------------------------
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        setPickerDelegates(delegate: self)
    }
    
    //--------------------------------------------------
    // MARK: - Picker Delegate to Override
    //--------------------------------------------------
    
    public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 0 }
    
    public func numberOfComponents(in pickerView: UIPickerView) -> Int { 0 }
}


// MARK: - Accessibility
extension BaseItemPickerEntryField {
    
    @objc open override func setAccessibilityString(_ accessibilityString: String?) {
        
        var accessibilityString = accessibilityString ?? ""
        
        if let textPickerItem = MVMCoreUIUtility.hardcodedString(withKey: "textfield_picker_item") {
            accessibilityString += textPickerItem
        }
        
        textField.accessibilityLabel = "\(accessibilityString) \(textField.isEnabled ? "" : MVMCoreUIUtility.hardcodedString(withKey: "textfield_disabled_state") ?? "")"
    }
}
