//
//  MVMCoreTopAlertObject.m
//  mobilefirst
//
//  Created by Scott Pfeil on 5/24/16.
//  Copyright © 2016 Verizon Wireless. All rights reserved.
//

#import "MVMCoreTopAlertObject.h"
#import <MVMCore/NSDictionary+MFConvenience.h>
#import <MVMCore/MVMCoreGetterUtility.h>
#import <MVMCore/MVMCoreJSONConstants.h>
#import "MVMCoreAlertHandler.h"

NSUInteger const TopAlertDismissTime = 5;

@implementation MVMCoreTopAlertObject

- (nullable instancetype)initWithResponseInfo:(nullable NSDictionary *)responseInfo {
    if (self = [super init]) {
        
        self.type = [responseInfo string:KeyType];
        self.persistent = [[responseInfo stringForKey:KeyMessageStyle] isEqualToString:ValueMessageStyleTopPersistent];
        
        self.title = [responseInfo string:KeyMessage];
        self.message = [responseInfo string:KeyUserMessage];
        self.buttonMap = [responseInfo dict:KeyButtonMap];
        self.topMessage = [responseInfo string:KeyTopMessage];
        self.imageNameOrURL = [responseInfo string:@"topAlertImageUrl"];
        self.aboveTextImageString = [responseInfo string:@"topAlertImageURLAboveText"];
        
        NSString *color = [responseInfo string:@"topAlertColor"];
        if (color) {
            self.backgroundColor = [MVMCoreGetterUtility getColorForHexString:color];
        }
        color = [responseInfo string:@"messageColor"];
        if (color) {
            self.textColor = [MVMCoreGetterUtility getColorForHexString:color];
        }
        
        // The default is yes if not sent by server (for legacy to work as is)
        NSNumber *closeButton = [responseInfo optionalNumberForKey:KeyCloseButton];
        if (closeButton != nil) {
            self.useCloseButton = [closeButton boolValue];
        } else {
            self.useCloseButton = YES;
        }
        
        self.useNewStyle = [responseInfo boolForKey:@"newTopAlertStyle"];
        
        // Server driven dismiss time.
        NSNumber *topAlertTime = [responseInfo optionalNumberForKey:@"topAlertTime"];
        if (topAlertTime != nil) {
            self.topAlertDismissTime = [topAlertTime integerValue];
        }
    }
    return self;
}

- (nullable instancetype)initWithType:(nullable NSString *)type message:(nullable NSString *)message {
    return [self initWithType:type message:nil subMessage:message persistent:NO actionMap:nil additionalData:nil];
}

- (nullable instancetype)initWithType:(nullable NSString *)type message:(nullable NSString *)message subMessage:(nullable NSString *)subMessage persistent:(BOOL)persistent actionMap:(nullable NSDictionary *)actionMap additionalData:(nullable NSDictionary *)additionalData {
    
    if (self = [super init]) {
        
        self.type = type;
        self.persistent = persistent;
        
        self.title = message;
        self.message = subMessage;
        self.buttonMap = actionMap;
        self.additionalData = additionalData;
    }
    return self;
}

- (nullable instancetype)initWithType:(nullable NSString *)type message:(nullable NSString *)message subMessage:(nullable NSString *)subMessage persistent:(BOOL)persistent buttonTitle:(nullable NSString *)buttonTitle userActionHandler:(nullable void (^)(id  _Nonnull sender))userActionHandler {
    
    if (self = [super init]) {
        
        self.type = type;
        self.persistent = persistent;
        
        self.title = message;
        self.message = subMessage;
        self.buttonTitle = buttonTitle;
        self.userActionHandler = userActionHandler;
    }
    return self;
}

@end
