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

import Foundation
import VDS
@objcMembers open class NotificationMoleculeView: VDS.Notification, VDSMoleculeViewProtocol {
  
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open var viewModel: NotificationMoleculeModel!
    public var delegateObject: MVMCoreUIDelegateObject?
    public var additionalData: [AnyHashable: Any]?
    
    //--------------------------------------------------
    // MARK: - VDSMoleculeViewProtocol
    //--------------------------------------------------
    
    open func viewModelDidUpdate() {
        surface = viewModel.surface
        title = viewModel.headline.text
        subTitle = viewModel.body?.text
        
        if let button = viewModel.button {
            primaryButtonModel = .init(text: button.title, onClick: {[weak self] _ in
                guard let self else { return }
                self.executeAction(model: button, delegateObject: self.delegateObject, additionalData: self.additionalData)
            })
        }
        
        if let secondaryButton = viewModel.secondaryButton {
            secondaryButtonModel = .init(text: secondaryButton.title, onClick: {[weak self] _ in
                guard let self else { return }
                self.executeAction(model: secondaryButton, delegateObject: self.delegateObject, additionalData: self.additionalData)
            })
        }
        
        if let accessibilityIdentifier = viewModel.accessibilityIdentifier {
            self.accessibilityIdentifier = accessibilityIdentifier
        }
        
        if let closeButton = viewModel.closeButton {
            onCloseClick = { [weak self] _ in
                guard let self else { return }
                self.executeAction(model: closeButton, delegateObject: self.delegateObject, additionalData: self.additionalData)            }
        }
        
        hideCloseButton = viewModel.closeButton == nil
        style = viewModel.style.toVDSStyle
    }
    
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    // Legacy constant
    private static let viewHeight: CGFloat = 96.0
    
    //--------------------------------------------------
    // MARK: - Life Cycle
    //--------------------------------------------------
    open override func updateAccessibility() {
        super.updateAccessibility()
        Self.amendAccesibilityLabel(for: titleLabel)
        Self.amendAccesibilityLabel(for: subTitleLabel)
        Self.amendAccesibilityLabel(for: primaryButton)
        Self.amendAccesibilityLabel(for: secondaryButton)
        Self.amendAccesibilityLabel(for: closeButton)
    }
    
    //--------------------------------------------------
    // MARK: - Molecule
    //--------------------------------------------------
    public func updateView(_ size: CGFloat) { }

    open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return viewHeight
    }
        
    /// Formats the accessibilityLabel so voice over users know it's in the notification.
    public class func amendAccesibilityLabel(for view: UIView?) {
        guard let view,
              let amendment = MVMCoreUIUtility.hardcodedString(withKey: "top_alert_notification")
        else { return }
        view.amendAccesibilityLabel(with: amendment)
    }
}

extension NotificationMoleculeView: AccessibilityProtocol {
    public func getAccessibilityLayoutChangedArgument() -> Any? {
        return titleLabel
    }
}

extension UIView {
    /// Formats the accessibilityLabel so voice over users know it's in the notification.
    public func amendAccesibilityLabel(with amendment: String) {
        guard let accessibilityLabel, !accessibilityLabel.hasPrefix(amendment) else { return }
        self.accessibilityLabel = "\(amendment) - \(accessibilityLabel)"
    }
}
