//
//  CDVInAppHandler.m
//  Indigitall
//
//  Created by indigitall on 13/12/23.
//

#import "CDVInAppHandler.h"
#import "CDVInAppUtils.h"
#import "CDVInAppViewController.h"

@implementation CDVInAppHandler

- (void) showPopup:(NSString *)popUpCode closePopupWhenClicked:(BOOL)closePopupWhenClicked closeIcon:(nullable UIButton *)closeIcon closeIconDisabled:(Boolean)closeIconDisabled didAppear:(nullable void(^)(void))didAppear didCancel:(nullable void(^)(void))didCancel didTouchWithAction:(nullable void(^)(INInAppAction *inAppAction))action didDismissed:(nullable void(^)(void))didDismissed onShowTimeFinished:(nullable void(^)(INInApp *inApp, int showTime))onShowTimeFinished didExpired:(nullable void(^)(INInApp *inApp, INError *error))didExpired didShowManyTimes:(nullable void(^)(INInApp *inApp, INError *error))didShowManyTimes didClickOut:(nullable void(^)(INInApp *inApp, INError *error))didClickOut didDismissForever:(nullable void(^)(INInApp *inApp, INError *error))didDismissForever failed:(nullable void(^)(INError *error))failed formFailed:(nullable void(^)(NSArray <INInAppError *>*error))formFailed formSubmit:(nullable void(^)(INInApp *inApp, NSDictionary *json))formSubmit{
    [CDVInAppUtils inAppWasGot:popUpCode closePopupWhenClicked:closePopupWhenClicked didFound:^(INInApp * _Nonnull inApp) {
        [self handlePopUp:inApp popupCode:popUpCode closePopupWhenClicked:closePopupWhenClicked closeIcon:closeIcon closeIconDisabled:closeIconDisabled didAppear:didAppear didCancel:didCancel didTouchWithAction:action didDismissed:didDismissed onShowTimeFinished:onShowTimeFinished didExpired:didExpired didShowManyTimes:didShowManyTimes didClickOut:didClickOut didDismissForever:didDismissForever failed:failed formFailed:formFailed formSubmit:formSubmit];
    } notFound:^{
        [CDVInAppHandler getInApp:popUpCode success:^(INInApp *inApp) {
            [self handlePopUp:inApp popupCode:popUpCode closePopupWhenClicked:closePopupWhenClicked closeIcon:closeIcon closeIconDisabled:closeIconDisabled didAppear:didAppear didCancel:didCancel didTouchWithAction:action didDismissed:didDismissed onShowTimeFinished:onShowTimeFinished didExpired:didExpired didShowManyTimes:didShowManyTimes didClickOut:didClickOut didDismissForever:didDismissForever failed:failed formFailed:formFailed formSubmit:formSubmit];
        } errorLoad:^(INError *error) {
            if (failed != nil) failed(error);
        }];
    } isPopUp:true view:nil closeIcon:closeIcon closeIconDisabled:closeIconDisabled didAppear:didAppear didCancel:didCancel didTouchWithAction:action didDismissed:didDismissed onShowTimeFinished:onShowTimeFinished didExpired:didExpired didShowManyTimes:didShowManyTimes didClickOut:didClickOut didDismissForever:didDismissForever success:nil failed:failed formFailed:formFailed formSubmit:formSubmit];
}

- (void) handlePopUp: (INInApp *)inApp popupCode:(NSString *)popUpCode closePopupWhenClicked:(BOOL)closePopupWhenClicked  closeIcon:(nullable UIButton *)closeIcon closeIconDisabled:(Boolean)closeIconDisabled didAppear:(nullable void(^)(void))didAppear didCancel:(nullable void(^)(void))didCancel didTouchWithAction:(nullable void(^)(INInAppAction *inAppAction))action didDismissed:(nullable void(^)(void))didDismissed onShowTimeFinished:(nullable void(^)(INInApp *inApp, int showTime))onShowTimeFinished didExpired:(nullable void(^)(INInApp *inApp, INError *error))didExpired didShowManyTimes:(nullable void(^)(INInApp *inApp, INError *error))didShowManyTimes didClickOut:(nullable void(^)(INInApp *inApp, INError *error))didClickOut didDismissForever:(nullable void(^)(INInApp *inApp, INError *error))didDismissForever failed:(nullable void(^)(INError *error))failed formFailed:(nullable void(^)(NSArray <INInAppError *>*error))formFailed formSubmit:(nullable void(^)(INInApp *inApp, NSDictionary *json))formSubmit{
    [INInAppHandler loadContent:inApp success:^(NSString * _Nonnull content, INInApp * _Nonnull inApp) {
        [self createPopUp:content inApp:inApp popUpId:popUpCode closePopupWhenClicked:closePopupWhenClicked closeIcon:closeIcon closeIconDisabled:closeIconDisabled didAppear:didAppear didCancel:didCancel didClicked:^{
            [INInAppHandler inAppWasClicked:inApp action:inApp.properties.action];
            if (action) action(inApp.properties.action);
        } didDismissed:didDismissed onShowTimeFinished:onShowTimeFinished failed:formFailed formSubmit:formSubmit];
    } failed:failed];
}

- (void)createPopUp:(NSString *)content inApp:(INInApp *)inApp popUpId:(NSString *)popUpId closePopupWhenClicked:(BOOL)closePopupWhenClicked closeIcon:(nullable UIButton *)closeIcon closeIconDisabled:(Boolean)closeIconDisabled didAppear:(void(^)(void))didAppear didCancel:(void(^)(void))didCancel didClicked:(void(^)(void))didClicked didDismissed:(nullable void(^)(void))didDismissed onShowTimeFinished:(void(^)(INInApp *inApp, int showTime))onShowTimeFinished failed:(void(^)(NSArray<INInAppError*> *error))failed formSubmit:(nullable void(^)(INInApp *inApp, NSDictionary *json))formSubmit{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        CDVInAppViewController *viewController = [[CDVInAppViewController alloc] initWithContent:content inApp:inApp closeIcon:closeIcon closeIconDisabled:closeIconDisabled failed:failed formSubmit:formSubmit];
        if (didAppear != nil){
            viewController.didAppear = didAppear;
        }
        if (didCancel != nil){
            viewController.didCancel = didCancel;
            if (inApp.properties.dismissForever) [CDVInAppUtils addNewInAppToDismissForever:inApp];
        }
        if (didDismissed != nil){
            viewController.didDismissed = didDismissed;
        }
        if (onShowTimeFinished != nil){
            viewController.onShowTimeFinished = onShowTimeFinished;
        }
        viewController.didTouch = ^{
            if (didClicked != nil) didClicked();
            if (closePopupWhenClicked) [viewController dismissViewController];
        };
        UIViewController *topViewController = [[INUIApplicationExtension alloc]mostTopViewController];
        if (topViewController == nil){
            INError *error = [[INInAppError alloc]initWithErrorCode:INAPP_POPUP_ERROR descriptionMessage:nil];
            if (failed) {
                NSMutableArray<INError *> *errors = [NSMutableArray new];
                [errors addObject:error];
                failed([errors copy]);
            }
            return;
        }
        viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [topViewController presentViewController:viewController animated:true completion:nil];
    });
}
@end
