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

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

@implementation WXCOUtility

+ (void)initialize {
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];// 整个后面的背景选择
    [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];// 弹出框颜色
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];// 弹出框内容颜色
    [SVProgressHUD setFont:[UIFont systemFontOfSize:13]];//字体
    [SVProgressHUD setMinimumDismissTimeInterval:1.5];
    [SVProgressHUD setMaximumDismissTimeInterval:3.0];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
}

+ (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)showAlertWithMessage:(NSString *)message {
    [SVProgressHUD setCornerRadius:20.0f];
    [SVProgressHUD showImage:nil status:message];
}

+ (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;
}

@end
