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

import Foundation

@objcMembers open class NotificationView: Container {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let headline = Label(fontStyle: .BoldBodySmall)
    public let body = Label(fontStyle: .RegularBodySmall)
    public let button = PillButton(asPrimaryButton: false, makeTiny: true)
    public let closeButton = NotificationXButton()
    public var labelStack: Stack<StackModel>!
    public var horizontalStack: Stack<StackModel>!
    
    // Legacy constant
    private static let viewHeight: CGFloat = 96.0
    
    //--------------------------------------------------
    // MARK: - Life Cycle
    //--------------------------------------------------
    
    public override func setupView() {
        super.setupView()
        reset()
        
        // Buttons should have highest priority, then headline, then body
        headline.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 500), for: .horizontal)
        headline.setContentHuggingPriority(.required, for: .vertical)
        body.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 500), for: .horizontal)
        body.setContentHuggingPriority(.required, for: .vertical)
        headline.setContentCompressionResistancePriority(UILayoutPriority(rawValue: body.contentCompressionResistancePriority(for: .vertical).rawValue + 40), for: .vertical)
        headline.lineBreakMode = .byTruncatingTail
        body.lineBreakMode = .byTruncatingTail
        button.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)

        labelStack = Stack<StackModel>.createStack(with: [headline, body], spacing: 0)
        horizontalStack = Stack<StackModel>.createStack(with: [(view: labelStack, model: StackItemModel()),(view: button, model: StackItemModel(horizontalAlignment: .fill)),(view: closeButton, model: StackItemModel(horizontalAlignment: .fill))], axis: .horizontal)
        addAndContain(horizontalStack)
        labelStack.restack()
        horizontalStack.restack()
        
        heightAnchor.constraint(equalToConstant: Self.viewHeight).isActive = true
    }
    
    open override func reset() {
        super.reset()
        backgroundColor = .mvmGreen()
        headline.textColor = .white
        body.textColor = .white
    }
    
    //--------------------------------------------------
    // MARK: - Molecule
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? NotificationModel else { return }
        labelStack.updateContainedMolecules(with: [model.headline, model.body], delegateObject, nil)
        horizontalStack.updateContainedMolecules(with: [labelStack.stackModel, model.button, model.closeButton], delegateObject, nil)
        updateAccessibility()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return viewHeight
    }
    
    open func updateAccessibility() {
        MVMCoreUITopAlertBaseView.amendAccesibilityLabel(for: headline)
        MVMCoreUITopAlertBaseView.amendAccesibilityLabel(for: body)
        MVMCoreUITopAlertBaseView.amendAccesibilityLabel(for: button)
    }
}

extension NotificationView: AccessibilityProtocol {
    public func getAccessibilityLayoutChangedArgument() -> Any? {
        return headline
    }
}
