//
// © 2017-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

extension UIViewController {
	func prepareModalNavigationBar() {
		if let closeImage = UIImage(named: "btn-close-actionbar"),
			let closeImagePressed = UIImage(named: "btn-close-actionbar-pressed") {
		
			let resizableClose = closeImage.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: closeImage.size.width-1, bottom: 0, right: 0))
			let resizableCloseImagePressed = closeImagePressed.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: closeImagePressed.size.width-1, bottom: 0, right: 0))
			let leftBarButton = UIBarButtonItem(title: " ", style: .plain, target: self, action: #selector(didTapModalClose(_:)))
			leftBarButton.setBackgroundImage(resizableClose, for: .normal, barMetrics: .default)
			leftBarButton.setBackgroundImage(resizableCloseImagePressed, for: .highlighted, barMetrics: .default)
			leftBarButton.makeAccessible(Accessibility.Navigation.modalCloseButton)
			navigationItem.leftBarButtonItem = leftBarButton
		}
	}
	
	@objc func didTapModalClose(_ sender: AnyObject?) {
		if let presenter = presentingViewController {
			presenter.dismiss(animated: true, completion: nil)
		}
	}
}
