//
//  DropDownFilterTableViewCell.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 9/6/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit

@objcMembers public class DropDownFilterTableViewCell: TableViewCell {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    let dropDown = ItemDropdownEntryField()
    var delegateObject: MVMCoreUIDelegateObject?
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    override public func setupView() {
        super.setupView()
                
        addMolecule(dropDown)
        dropDown.observeDropdownChange = { [weak self] oldValue, newValue in
            
            guard newValue != oldValue,
                let self = self,
                let model = self.listItemModel as? DropDownListItemModel
                else { return }
            MVMCoreDispatchUtility.performBlock(inBackground: {
                if let oldValue = oldValue,
                   oldValue.count > 0 {
                    MVMCoreActionHandler.shared()?.syncHandleAction(with: RemoveMoleculesActionModel(.fade), additionalData: [KeySourceModel: model], delegateObject: self.delegateObject)
                }
                MVMCoreActionHandler.shared()?.syncHandleAction(with: AddMoleculesActionModel(.fade), additionalData: [KeySourceModel: model], delegateObject: self.delegateObject)
            })
        }
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        self.delegateObject = delegateObject
        super.set(with: model, delegateObject, additionalData)
        if let dropDownListItemModel = model as? DropDownListItemModel {
            dropDown.set(with: dropDownListItemModel.dropDown, delegateObject, additionalData)
        }
        dropDown.observingTextFieldDelegate = delegateObject?.uiTextFieldDelegate as? ObservingTextFieldDelegate
        dropDown.uiTextFieldDelegate = delegateObject?.uiTextFieldDelegate
    }
    
    public override func reset() {
        super.reset()
        bottomSeparatorView?.setStyle(.none)
    }
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 80
    }
}
