//
//  WXCOUtility.m
//  WXDownloadingKit
//
//  Created by Davinci.Pi on 2020/3/5.
//  Copyright © 2020 Davinci.Pi. All rights reserved.
//

#import "WXCOUtility.h"
#import <UIKit/UIKit.h>
#import <MBProgressHUD/MBProgressHUD.h>

@implementation WXCOUtility

+ (NSString *)getTimeNow {
    NSDate *date = [NSDate date];
    return [self getTimeString:date];
}

+ (NSString *)getTimeString:(NSDate *)date {
    return [self getTimeString:date withDateFormat:@"yyyy-MM-dd HH:mm:ss"];
}

+ (NSString *)getTimeString:(NSDate *)date withDateFormat:(NSString *)dateFormat {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:dateFormat];
    NSString *timeString = [formatter stringFromDate:date];
    return timeString;
}

// 获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC {
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    return currentVC;
}

// 获取根视图
+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC {
    UIViewController *currentVC;
    if ([rootVC presentedViewController])
        // 视图是被presented出来的
        rootVC = [rootVC presentedViewController];
    
    if ([rootVC isKindOfClass:[UITabBarController class]])
    {
        // 根视图为UITabBarController
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
    }
    else if ([rootVC isKindOfClass:[UINavigationController class]])
    {
        // 根视图为UINavigationController
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
    }
    else
    {
        // 根视图为非导航类
        currentVC = rootVC;
    }
    
    return currentVC;
}

+ (void)showHUDWithMessage:(NSString *)message {
    if (!message || [message isKindOfClass:[NSNull class]] || message.length == 0) {
        return;
    }
    
    [self performOptionOnMainQueue:^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.userInteractionEnabled = NO;
        hud.mode = MBProgressHUDModeText;
        hud.detailsLabel.text = message;
        hud.contentColor = [UIColor whiteColor];
        hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.9f];
        hud.detailsLabel.font = [UIFont systemFontOfSize:15.0f];
        hud.margin = 10.0f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:2.0f];
    }];
}

+ (NSURL *)urlWithString:(NSString *)string {
//    [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"!$&'()*+,-./:;=?@_~%#[]"]];
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
    return [NSURL URLWithString:(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL,kCFStringEncodingUTF8))];
#pragma clang diagnostic pop
}

+ (NSTimeInterval)timestamp {
    NSDate *date = [NSDate date];
    NSTimeInterval timestamp = [date timeIntervalSince1970];
    return timestamp;
}

#pragma mark - Main Queue

+ (void)performOptionOnMainQueue:(void (^)(void))block {
    if ([NSThread isMainThread]) {
        if (block) {
            block();
        }
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block();
            }
        });
    }
}

@end
