//
//  closeAD.m
//  playbtest
//
//  Created by 忆海16 on 2024/11/6.
//

#import "closeAD.h"

@implementation closeAD
+ (void)removeADVCByDelayTime:(NSInteger)delayTime {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime / 1000 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [closeAD removeVungleFullScreenPresenter];
        [closeAD closeADWindow];
    });
}

//+ (void)closeADWindow {
//    NSLog(@"已经执行closeADWindow.....");
//    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
//    [keyWindow.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        UIViewController *vc = obj.subviews.firstObject.nextResponder;
//        if ([vc isKindOfClass:[NSClassFromString(@"ALAppLovinVideoViewController") class]]) {
//            if ([vc respondsToSelector:@selector(skipVideo)]) {
//                NSLog(@"执行了skipVideo.....");
//                [vc performSelector:@selector(skipVideo) withObject:nil];
//            }
//            if ([vc respondsToSelector:@selector(handleCloseButton)]) {
//                NSLog(@"执行了handleCloseButton.....");
//                [vc performSelector:@selector(handleCloseButton) withObject:nil];
//            }
//            
//            [closeAD checkCloseWindown];
//            
//        }
//        else if ([vc isKindOfClass:[NSClassFromString(@"ALVASTVideoViewController") class]]) {
//            [vc performSelector:@selector(dismiss) withObject:nil];
//            
//            [closeAD checkCloseWindown];
//            [closeAD removeVungleFullScreenPresenter];
//        }
//    }];
//    
//    
//}

+ (void)closeADWindow {
    NSLog(@"Executing closeADWindow - First Pass");
    [self performCloseADWindowActions];
    
    NSLog(@"Executing closeADWindow - Second Pass");
    [self performCloseADWindowActions];
}

+ (void)performCloseADWindowActions {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if (!keyWindow) return;  // Ensure keyWindow is non-nil
    
    [keyWindow.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIViewController *vc = obj.subviews.firstObject.nextResponder;
        if (!vc || ![vc isKindOfClass:[UIViewController class]]) return; // Check for non-nil vc
        
        if ([vc isKindOfClass:[NSClassFromString(@"ALAppLovinVideoViewController") class]]) {
            if ([vc respondsToSelector:@selector(skipVideo)]) {
                NSLog(@"Executing skipVideo...");
                [vc performSelector:@selector(skipVideo) withObject:nil];
            }
            if ([vc respondsToSelector:@selector(handleCloseButton)]) {
                NSLog(@"Executing handleCloseButton...");
                [vc performSelector:@selector(handleCloseButton) withObject:nil];
            }
            
//            [closeAD checkCloseWindown];
            [closeAD removeVungleFullScreenPresenter];
            
        } else if ([vc isKindOfClass:[NSClassFromString(@"ALVASTVideoViewController") class]]) {
            if ([vc respondsToSelector:@selector(dismiss)]) {
                [vc performSelector:@selector(dismiss) withObject:nil];
            }
            
//            [closeAD checkCloseWindown];
            [closeAD removeVungleFullScreenPresenter];
        }
        [closeAD removeVungleFullScreenPresenter];
    }];
}

//+ (void)checkCloseWindown {
//    static NSInteger closeCounter = 0;
//    if (closeCounter >= 2) {
//        NSLog(@"Stopping recursive closeADWindow calls");
//        closeCounter = 0;
//        return;
//    }
//    closeCounter++;
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [closeAD closeADWindow];
//    });
//}

+ (void)removeVungleFullScreenPresenter {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    UIViewController *foundViewController = [self findFullScreenPresenterInViewController:keyWindow.rootViewController];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *config = [userDefaults dictionaryForKey:@"xyzshell_hooks_settings_hook"];
    if (foundViewController) {
        NSLog(@"Found FullScreenPresenter instance: %@", foundViewController);
        
        // 关闭或移除 FullScreenPresenter
        [foundViewController dismissViewControllerAnimated:YES completion:^{
            NSLog(@"FullScreenPresenter dismissed.");
        }];
    } else {
        NSLog(@"FullScreenPresenter not found.");
    }
}

+ (UIViewController *)findFullScreenPresenterInViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:NSClassFromString(@"VungleAdsSDK.FullScreenPresenter")]) {
        return viewController;
    }
    
    // 递归遍历所有子控制器
    for (UIViewController *childViewController in viewController.childViewControllers) {
        UIViewController *foundViewController = [self findFullScreenPresenterInViewController:childViewController];
        if (foundViewController) {
            return foundViewController;
        }
    }
    
    // 检查 presentedViewController
    if (viewController.presentedViewController) {
        return [self findFullScreenPresenterInViewController:viewController.presentedViewController];
    }
    
    return nil;
}

@end

