//
//  BarButtonItem.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 5/18/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

public typealias BarButtonAction = (BarButtonItem) -> ()


@objc class ActionDelegate: NSObject {
    var buttonAction: BarButtonAction?
    @objc func callActionBlock(_ sender: BarButtonItem) {
        buttonAction?(sender)
    }
}

@objcMembers open class BarButtonItem: UIBarButtonItem, MFButtonProtocol {
    //--------------------------------------------------
    // MARK: - Delegate
    //--------------------------------------------------
    open var model: (MoleculeModelProtocol & NavigationButtonModelProtocol)?
    open weak var buttonDelegate: ButtonDelegateProtocol?
    var actionDelegate: ActionDelegate?

    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    open func set(with model: MoleculeModelProtocol & NavigationButtonModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
        self.model = model
        buttonDelegate = delegateObject?.buttonDelegate
        actionDelegate?.buttonAction = { sender in
            let additionalDataWithSource = additionalData.dictionaryAdding(key: KeySourceModel, value: model)
            Button.performButtonAction(with: model.action, button: sender, delegateObject: delegateObject, additionalData: additionalDataWithSource)
        }
    }
}
