//
//  UIViewController+Extension.m
//  SLAiELTS
//
//  Created by Gusont on 2023/2/27.
//

#import "UIViewController+Extension.h"

@implementation UIViewController (Extension)

//static var loadViewControllewWithNib: UIViewController {
//    return self.init(nibName: self.className, bundle: nil)
//}

+ (NSString *)className
{
    return NSStringFromClass(self.class);
}

+ (UIViewController *)loadViewControllewWithNib
{
    return [[self alloc] initWithNibName:[self className] bundle:nil];
}

+ (UIViewController *)getCurrentShowVC
{
    if (![[NSThread currentThread] isMainThread]) {
        return nil;
    }
    UIWindow* window = nil;
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes)
        {
            if (windowScene.activationState == UISceneActivationStateForegroundActive)
            {
                window = windowScene.windows.firstObject;
                break;
            }
        }
    }else {
        window = [[UIApplication sharedApplication].delegate window];
    }
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    UIViewController *topViewController = [window rootViewController];
    
    while (true) {
        
        if (topViewController.presentedViewController) {
            
            topViewController = topViewController.presentedViewController;
            
        } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
            
            topViewController = [(UINavigationController *)topViewController topViewController];
            
        } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
            
            UITabBarController *tab = (UITabBarController *)topViewController;
            topViewController = tab.selectedViewController;
            
        }else if ([topViewController isKindOfClass:[SLBaseTabBarController class]]) {
            
            SLBaseTabBarController *tab = (SLBaseTabBarController *)topViewController;
            topViewController = tab.selectedViewController;
            
        } else {
            break;
        }
    }
    return topViewController;
}

- (void)navPushViewController:(UIViewController *)vc animated:(BOOL)animated {
    
}


@end
