//
//  MVMCoreUITopAlertBaseView.m
//  mobilefirst
//
//  Created by Scott Pfeil on 9/6/16.
//  Copyright © 2016 Verizon Wireless. All rights reserved.
//

#import "MVMCoreUITopAlertBaseView.h"
#import <MVMCore/MVMCoreActionHandler.h>
#import "MVMCoreUISplitViewController.h"
#import <MVMCore/MVMCoreLoadObject.h>
#import <MVMCore/MVMCoreRequestParameters.h>
#import "MVMCoreUITopAlertView.h"
#import <MVMCore/MVMCoreJSONConstants.h>
#import <MVMCore/NSDictionary+MFConvenience.h>
#import "UIColor+MFConvenience.h"
#import "MVMCoreUICommonViewsUtility.h"
#import "MFStyler.h"
#import "MVMCoreUISession.h"
#import <MVMCoreUI/MVMCoreUI-Swift.h>
#import "MVMCoreTopAlertDelegateProtocol.h"

@implementation MVMCoreUITopAlertBaseView

+ (void)addActionToButton:(nonnull Button *)button actionMap:(nullable NSDictionary *)actionMap additionalData:(nullable NSDictionary *)additionalData {
    [button addActionBlockWithEvent:UIControlEventTouchUpInside :^(Button * _Nonnull button) {
        // Check with the current screen if we should perform the action (not if it's the same page type)
        BOOL performAction = YES;
        UIViewController *topViewController = [MVMCoreUISession sharedGlobal].splitViewController.getCurrentDetailViewController;
        if ([topViewController respondsToSelector:@selector(shouldLoadTopAlertAction:additionalData:)]) {
            performAction = [((NSObject <MVMCoreTopAlertDelegateProtocol>*)topViewController) shouldLoadTopAlertAction:actionMap additionalData:additionalData];
        }
        
        if (performAction) {
            [[MVMCoreActionHandler sharedActionHandler] handleActionWithDictionary:actionMap additionalData:additionalData delegateObject:[MVMCoreUIDelegateObject createWithDelegateForAll:[MVMCoreUISession sharedGlobal].topAlertView]];
        }
    }];
}

+ (UILabel *)topAlertLabel {
    
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [label setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    label.textColor = [UIColor whiteColor];
    label.font = [MFStyler fontB1];
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    return label;
}

+ (nullable NSAttributedString *)getStringForMessage:(nullable NSString *)message subMessage:(nullable NSString *)subMessage color:(nullable UIColor *)color {
    
    UIColor *textColor = color ?: [UIColor whiteColor];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    if (message.length > 0) {
        [string appendAttributedString:[MFStyler styleGetAttributedString:message font:[MFStyler fontB1] color:textColor]];
    }
    
    if (subMessage.length > 0) {
        
        NSString *subMessageToAppend = nil;
        if (string.length > 0) {
            subMessageToAppend = [NSString stringWithFormat:@"\n%@",subMessage];
        } else {
            subMessageToAppend = subMessage;
        }
        [string appendAttributedString:[MFStyler styleGetAttributedString:subMessageToAppend font:[MFStyler fontB2] color:textColor]];
    }
    return string;
}

- (nonnull Button *)addCloseButtonWithAnimationDelegate:(nullable id <MVMCoreTopAlertAnimationDelegateProtocol>)animationDelegate {
    Button *closeButton = [MVMCoreUICommonViewsUtility addCloseButtonTo:self action:^(Button * _Nonnull button) {
        if (animationDelegate) {
            [animationDelegate topAlertCloseButtonPressed];
        } else {
            [[MVMCoreUISession sharedGlobal].topAlertView hideAlertView:YES completionHandler:nil];
        }
    } centeredVertically:YES];
    [closeButton.heightAnchor constraintEqualToConstant:16.0].active = YES;
    [closeButton.widthAnchor constraintEqualToConstant:16.0].active = YES;
    [MVMCoreUITopAlertBaseView amendAccesibilityLabelForView:closeButton];
    return closeButton;
}

- (void)updateView:(CGFloat)size {}

- (void)handleAccessibility {}

+ (void)amendAccesibilityLabelForView:(nonnull UIView *)view {
    NSString *amendment = [MVMCoreUIUtility hardcodedStringWithKey:@"top_alert_notification"];
    NSString *accessibilityLabel = view.accessibilityLabel;
    if (accessibilityLabel && ![accessibilityLabel hasPrefix:amendment]) {
        view.accessibilityLabel = [NSString stringWithFormat:@"%@ - %@", amendment, accessibilityLabel];
    }
}

@end
