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

import Foundation

@objcMembers open class CollapsableNotificationTopView: View {
    public let label: Label = {
        let label = Label(fontStyle: .BoldBodySmall)
        label.textAlignment = .center
        label.setContentHuggingPriority(.defaultHigh, for: .vertical)
        return label
    }()
    
    public let button: Button = {
        let button = Button(type: .custom)
        button.backgroundColor = .clear
        button.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
        button.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        return button
    }()
    
    public override func setupView() {
        super.setupView()
        
        addSubview(label)
        NSLayoutConstraint.constraintPinSubview(label, pinTop: true, topConstant: 0, pinBottom: true, bottomConstant: 0, pinLeft: true, leftConstant: PaddingThree, pinRight: true, rightConstant: PaddingThree)
        
        addSubview(button)
        NSLayoutConstraint.constraintPinSubview(button, pinTop: true, topConstant: 0, pinBottom: true, bottomConstant: -5, pinLeft: true, leftConstant: 0, pinRight: true, rightConstant: 0)

        // Listen for status bar touches.
        NotificationCenter.default.addObserver(self, selector: #selector(pressed(_:)), name: NSNotification.Name(rawValue: NotificationStatusBarTouched), object: nil)
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        label.updateView(size)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
        guard let model = model as? CollapsableNotificationModel else { return }
        label.set(with: model.topLabel, delegateObject, additionalData)
        button.set(with: model.topAction, delegateObject: delegateObject, additionalData: additionalData)
        updateAccessibility()
    }
    
    open override func reset() {
        super.reset()
        label.setFontStyle(.BoldBodySmall)
        label.textColor = .white
        label.textAlignment = .center
    }
    
    open func updateAccessibility() {
        isAccessibilityElement = true
        accessibilityLabel = label.text
        accessibilityTraits = (button.isUserInteractionEnabled && button.actionModel != nil) ? .button : .none
        MVMCoreUITopAlertBaseView.amendAccesibilityLabel(for: self)
    }
    
    @objc func pressed(_ sender: Notification) {
        button.callActionBlock(button)
    }
}
