//
//  ItemDropdownEntryField.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 11/14/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit


open class ItemDropdownEntryField: BaseItemPickerEntryField {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open var pickerData: [String] = []
    
    public var itemDropdownEntryFieldModel: ItemDropdownEntryFieldModel? {
        model as? ItemDropdownEntryFieldModel
    }
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    @objc public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    @objc public convenience init() {
        self.init(frame: .zero)
    }
    
    @objc public convenience init(pickerData: [String]) {
        self.init(frame: .zero)
        self.pickerData = pickerData
    }
    
    @objc required public init?(coder: NSCoder) {
        fatalError("ItemDropdownEntryField init(coder:) has not been implemented")
    }
    
    required public init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.init(model: model, delegateObject, additionalData)
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    /// Sets the textField with the first value of the available picker data.
    @objc private func setInitialValueFromPicker() {
        
        guard !pickerData.isEmpty else { return }
        
        if setInitialValueInTextField {
            let pickerIndex = pickerView.selectedRow(inComponent: 0)
            itemDropdownEntryFieldModel?.selectedIndex = pickerIndex
            observeDropdownChange?(text, pickerData[pickerIndex])
            text = pickerData[pickerIndex]
        }
    }
    
    @objc override func startEditing() {
        super.startEditing()
        
        setInitialValueFromPicker()
    }
    
    @objc override func endInputing() {
        super.endInputing()
        
        guard !pickerData.isEmpty else { return }
        
        observeDropdownSelection?(pickerData[pickerView.selectedRow(inComponent: 0)])
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ItemDropdownEntryFieldModel else { return }
        
        pickerData = model.options
        
        if let index = model.selectedIndex {
            self.pickerView.selectRow(index, inComponent: 0, animated: false)
            self.pickerView(pickerView, didSelectRow: index, inComponent: 0)
        }
    }

    //--------------------------------------------------
    // MARK: - Picker Delegate
    //--------------------------------------------------
    
    @objc public override func numberOfComponents(in pickerView: UIPickerView) -> Int { 1 }
    
    @objc public override func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        pickerData.count
    }
    
    @objc public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        guard !pickerData.isEmpty else { return nil }
        
        return pickerData[row]
    }
    
    @objc public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        guard !pickerData.isEmpty else { return }
        
        itemDropdownEntryFieldModel?.selectedIndex = row
        observeDropdownChange?(text, pickerData[row])
        text = pickerData[row]
    }
}
