//
//  MVMCoreUICommonViewsUtility+Extension.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 3/23/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

public extension MVMCoreUICommonViewsUtility {
    
    /// Add the close button (x) to the top right of the view. TODO: update this logic.
    @objc static func addCloseButton(to view: UIView, action: @escaping ButtonAction, centeredVertically: Bool = false) -> Button {
        let button = Button()
        if let image = MVMCoreUIUtility.imageNamed("nav_close")?.withRenderingMode(.alwaysTemplate) {
            button.setImage(image, for: .normal)
        }
        button.tintColor = .black
        button.accessibilityLabel = MVMCoreUIUtility.hardcodedString(withKey: "AccCloseButton")
        button.addActionBlock(event: .touchUpInside, action)
        
        view.addSubview(button)
        var constraints: [NSLayoutConstraint] = []
        if centeredVertically {
            constraints.append(view.rightAnchor.constraint(equalTo: button.rightAnchor, constant: Padding.Component.horizontalPaddingForApplicationWidth))
            constraints.append(view.centerYAnchor.constraint(equalTo: button.centerYAnchor))
        } else {
            constraints.append(button.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant:  PaddingOne))
            constraints.append(view.safeAreaLayoutGuide.rightAnchor.constraint(equalTo: button.rightAnchor, constant: Padding.Component.horizontalPaddingForApplicationWidth))
        }
        NSLayoutConstraint.activate(constraints)
        return button
    }
    
    static func getToolbarWithDoneButton(delegate: ObservingTextFieldDelegate) -> UIToolbar {
        
        let toolbar = self.makeEmptyToolbar()
        let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let button = UIBarButtonItem(barButtonSystemItem: .done, target: delegate, action: #selector(ObservingTextFieldDelegate.dismissFieldInput))
        button.tintColor = .black
        toolbar.setItems([space, button], animated: false)
        
        return toolbar
    }
    
    static func getView(with height: CGFloat) -> UIView {
        let view = self.commonView()
        view.heightAnchor.constraint(equalToConstant: height).isActive = true
        return view
    }
}
