//
//  MVMCoreUITopAlertShortView.m
//  mobilefirst
//
//  Created by Scott Pfeil on 7/20/16.
//  Copyright © 2016 Verizon Wireless. All rights reserved.
//

#import "MVMCoreUITopAlertShortView.h"
#import "MFStyler.h"
#import "NSLayoutConstraint+MFConvenience.h"
#import "MVMCoreUIConstants.h"
#import <MVMCoreUI/MVMCoreUI-Swift.h>

@interface MVMCoreUITopAlertShortView ()

@end

@implementation MVMCoreUITopAlertShortView

- (void)updateView:(CGFloat)size {
    [super updateView:size];
    self.label.font = [MFStyler fontB1];
}

- (nullable instancetype)initWithColor:(nonnull UIColor *)color message:(nullable NSString *)message actionMap:(nullable NSDictionary *)actionMap additionalData:(nullable NSDictionary *)additionalData topAlertObject:(nullable MVMCoreTopAlertObject *)topAlertObject {
 
    if (self = [super init]) {

        self.backgroundColor = color;
        self.translatesAutoresizingMaskIntoConstraints = NO;
        
        UILabel *label = [MVMCoreUITopAlertBaseView topAlertLabel];
        label.numberOfLines = 1;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = message;
        [self addSubview:label];
        [label setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
        [NSLayoutConstraint constraintPinSubview:label pinTop:YES topConstant:0 pinBottom:YES bottomConstant:4 pinLeft:YES leftConstant:PaddingThree pinRight:YES rightConstant:PaddingThree];
        self.label = label;
        
        // Sets up to use a button action.
        Button *button = [Button buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        button.translatesAutoresizingMaskIntoConstraints = NO;
        [button setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
        [button setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        [self addSubview:button];
        [NSLayoutConstraint constraintPinSubview:button pinTop:YES topConstant:0 pinBottom:YES bottomConstant:-5 pinLeft:YES leftConstant:0 pinRight:YES rightConstant:0];
        if (actionMap) {
            [MVMCoreUITopAlertBaseView addActionToButton:button actionMap:actionMap additionalData:additionalData];
        }
        self.button = button;
        
        // Listen for status bar touches.
        [[NSNotificationCenter defaultCenter] addObserverForName:NotificationStatusBarTouched object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            [button sendActionsForControlEvents:UIControlEventAllEvents];
        }];
    }
    return self;
}

- (void)handleAccessibility {
    UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self.label);
}

@end
