//
//  NotificationXButton.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 9/17/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

@objcMembers open class NotificationXButton: Button {
    
    open func closeTopAlert() {
        if let delegate = MVMCoreUITopAlertView.sharedGlobal()?.animationDelegate {
            delegate.topAlertCloseButtonPressed()
        } else {
            MVMCoreUISession.sharedGlobal()?.topAlertView?.hideAlertView(true, completionHandler: nil)
        }
    }
    
    open override func setupView() {
        if let image = MVMCoreUIUtility.imageNamed("nav_close")?.withRenderingMode(.alwaysTemplate) {
            setImage(image, for: .normal)
        }
        tintColor = .white
        adjustsImageWhenHighlighted = false
        accessibilityLabel = MVMCoreUIUtility.hardcodedString(withKey: "AccCloseButton")
        setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        heightAnchor.constraint(equalToConstant: 16.0).isActive = true
        widthAnchor.constraint(equalToConstant: 16.0).isActive = true
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? NotificationXButtonModel else { return }
        tintColor = model.color?.uiColor ?? .white
        
        // TODO: Temporary, consider action for dismissing top alert
        if model.action.actionType == ActionNoopModel.identifier {
            addActionBlock(event: .touchUpInside) { (button) in
                (button as? NotificationXButton)?.closeTopAlert()
            }
        }
    }
}
