//
//  MVMCoreUIActionHandler.m
//  MVMCoreUI
//
//  Created by Scott Pfeil on 10/28/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

#import "MVMCoreUIActionHandler.h"
#import "MVMCoreUIConstants.h"
#import "MVMCoreAlertObject.h"
#import <MVMCoreUI/MVMCoreUIActionDelegateProtocol.h>
#import <MVMCoreUI/MVMCoreUI-Swift.h>
@import MVMCore.MVMCoreActionHandler;
@import MVMCore.NSDictionary_MFConvenience;
@import MVMCore.MVMCoreJSONConstants;
@import MVMCore.MVMCoreCache;

@implementation MVMCoreUIActionHandler

- (BOOL)handleOtherActions:(NSString *)actionType actionInformation:(NSDictionary *)actionInformation additionalData:(NSDictionary *)additionalData delegateObject:(DelegateObject *)delegateObject {
    if ([actionType isEqualToString:KeyActionTypePopup]) {
        [self popupAction:actionInformation additionalData:additionalData delegateObject:delegateObject];
        return YES;
    } else if ([actionType isEqualToString:KeyActionTypeTopAlert]) {
        [self topAlertAction:actionInformation additionalData:additionalData delegateObject:delegateObject];
        return YES;
    } else if ([actionType isEqualToString:KeyActionTypeCollapseNotification]) {
        [self collapseNotificationAction:actionInformation additionalData:additionalData delegateObject:delegateObject];
        return YES;
    } else if ([actionType isEqualToString:KeyActionTypeAlert]) {
        [self showAlert:actionInformation additionalData:additionalData delegateObject:delegateObject];
        return YES;
    } else if ([actionType isEqualToString:KeyActionTypeTopNotification]) {
        [self topNotificationAction:actionInformation additionalData:additionalData delegateObject:delegateObject];
        return YES;
    }
    return [super handleOtherActions:actionType actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject];
}

- (void)openPageAction:(NSDictionary *)actionInformation additionalData:(NSDictionary *)additionalData delegateObject:(DelegateObject *)delegateObject {
    // Cleanup the source model data to prevent it from being accidentally auto-forwarded in openPage network requests by blind additionalData insertions. (https://onejira.verizon.com/browse/CXTDT-135642, https://onejira.verizon.com/browse/CXTDT-136001).
    if (additionalData[KeySourceModel]) {
        NSMutableDictionary *cleanedData = [additionalData mutableCopy];
        [cleanedData removeObjectForKey:KeySourceModel];
        additionalData = cleanedData;
    }
    [super openPageAction:actionInformation additionalData:additionalData delegateObject:delegateObject];
}

- (void)popupAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject {
    // Perform a popup.
    NSString *pageTypeForPopup = [actionInformation stringForKey:KeyPageType];
    [[MVMCoreCache sharedCache] fetchJSONForPageType:pageTypeForPopup queue:nil waitUntilFinished:YES completionHandler:^(NSDictionary * _Nullable jsonDictionary) {
        
        MVMCoreErrorObject *error = nil;
        MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectWithPage:jsonDictionary isGreedy:NO additionalData:additionalData delegateObject:delegateObject error:&error];
        if ([delegateObject.actionDelegate respondsToSelector:@selector(willShowPopupWithAlertObject:alertJson:)]) {
            [((id <MVMCoreUIActionDelegateProtocol>)delegateObject.actionDelegate) willShowPopupWithAlertObject:alertObject alertJson:jsonDictionary];
        }
        
        if (alertObject) {
            [[MVMCoreAlertHandler sharedAlertHandler] showAlertWithAlertObject:alertObject];
        } else {
            [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject ];
        }
    }];
}

- (void)showAlert:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject {
    
    MVMCoreErrorObject *error = nil;
    MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectWithAction:actionInformation additionalData:additionalData delegateObject:delegateObject error:&error];
    if ([delegateObject.actionDelegate respondsToSelector:@selector(willShowPopupWithAlertObject:alertJson:)]) {
        [((id <MVMCoreUIActionDelegateProtocol>)delegateObject.actionDelegate) willShowPopupWithAlertObject:alertObject alertJson:actionInformation];
    }
    
    if (alertObject) {
        [[MVMCoreAlertHandler sharedAlertHandler] showAlertWithAlertObject:alertObject];
    } else {
        [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject];
    }
}

- (void)topAlertAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject {
    // Perform a top alert.
    NSString *pageTypeForTopAlert = [actionInformation stringForKey:KeyPageType];
    [[MVMCoreCache sharedCache] fetchJSONForPageType:pageTypeForTopAlert queue:nil waitUntilFinished:YES completionHandler:^(NSDictionary * _Nullable jsonDictionary) {
        
        NSDictionary *responseInfo = [jsonDictionary dict:KeyResponseInfo];
        if (responseInfo) {
            MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectForPageType:pageTypeForTopAlert responseInfo:responseInfo additionalData:additionalData delegateObject:delegateObject];
            if ([delegateObject.actionDelegate respondsToSelector:@selector(willShowTopAlertWithAlertObject:alertJson:)]) {
                [((id <MVMCoreUIActionDelegateProtocol>)delegateObject.actionDelegate) willShowTopAlertWithAlertObject:alertObject alertJson:actionInformation];
            }
            [alertObject showAlert];
        }
    }];
}

- (void)collapseNotificationAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject {
    // Collapse the current notification.
    if ([[CoreUIObject sharedInstance].globalTopAlertDelegate respondsToSelector:@selector(getTopAlertView)]) {
        [[[CoreUIObject sharedInstance].globalTopAlertDelegate getTopAlertView] collapseNotification];
    }
}

- (void)topNotificationAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject {
    //Handle molecular topnotification
    [[MVMCoreUITopAlertView sharedGlobal] showTopAlertWith:[actionInformation dict:@"topNotification"] ?: @{}];
}

- (void)defaultHandleActionError:(nonnull MVMCoreErrorObject *)error additionalData:(nullable NSDictionary *)additionalData {
    [super defaultHandleActionError:error additionalData:additionalData];
    if (!error.silentError) {
        
        // Show alert
        [MVMCoreDispatchUtility performBlockOnMainThread:^{
            MVMCoreAlertObject *alertObject = [[MVMCoreAlertObject alloc] initPopupAlertWithError:error isGreedy:NO];
            [[MVMCoreAlertHandler sharedAlertHandler] showAlertWithAlertObject:alertObject];
        }];
    }
}

@end
