//
//  Untis.m
//  YHScrollView_Test
//
//  Created by Jim on 2021/9/3.
//

#import "Untis.h"
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAssetImageGenerator.h>
#import <AVFoundation/AVAsset.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@implementation Untis

+ (BOOL)isIPhoneXSeries{
    BOOL iPhoneXSeries = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return iPhoneXSeries;
    }

    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            iPhoneXSeries = YES;
        }
    }
    return iPhoneXSeries;
}


// 判断是否有相机权限
+(void)judgeIsHaveCamerCompleteBlock:(void(^)(BOOL granted))authorizationBlock{
     AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)) {
        
        authorizationBlock(NO);
    }else if (authStatus == AVAuthorizationStatusNotDetermined){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
              authorizationBlock(YES);
            }else{
                authorizationBlock(NO);
            }
        }];
    }else {
         authorizationBlock(YES);
    }
}

// 判断是否有相册权限
+(void) judgeIsHaveAlbumCompleteBlock:(void(^)(BOOL granted))authorizationBlock{
    // 请求权限，需注意 limited 权限仅在 accessLevel 为 readAndWrite 时生效
    if (@available(iOS 14, *)) {
        PHAccessLevel level = PHAccessLevelReadWrite;
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:level];
          switch (status) {
              case PHAuthorizationStatusLimited:
                  authorizationBlock(YES);
                  break;
              case PHAuthorizationStatusDenied:
                  authorizationBlock(NO);
                  break;
              case PHAuthorizationStatusAuthorized:
                  authorizationBlock(YES);
                  break;
              case PHAuthorizationStatusRestricted:
                  authorizationBlock(NO);
                  break;
              case PHAuthorizationStatusNotDetermined: {
                  [PHPhotoLibrary requestAuthorizationForAccessLevel:level handler:^(PHAuthorizationStatus status) {
                    switch (status) {
                        case PHAuthorizationStatusLimited:
                            authorizationBlock(YES);
                            break;
                        case PHAuthorizationStatusDenied:
                            authorizationBlock(NO);
                            break;
                        case PHAuthorizationStatusAuthorized:
                            authorizationBlock(YES);
                            break;
                        case PHAuthorizationStatusRestricted:
                            authorizationBlock(NO);
                            break;
                        default:
                            break;
                    }
                  }];
              }
                  break;
              default:
                  break;
          }
    } else {
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusRestricted ||
            status == PHAuthorizationStatusDenied) {
            authorizationBlock(NO);
        }else if (status == PHAuthorizationStatusNotDetermined){
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status == PHAuthorizationStatusAuthorized){//允许
                    authorizationBlock(YES);
                }else{//拒绝
                    authorizationBlock(NO);
                }
            }];
        }else{
            authorizationBlock(YES);
        }
    }
        
}

+(void)judgeIsHaveMicrophoneCompleteBlock:(void(^)(BOOL granted))authorizationBlock{
    AVAuthorizationStatus microPhoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (microPhoneStatus) {
        case AVAuthorizationStatusDenied:
            {
              
                // 被拒绝
                authorizationBlock(NO);
            }
                break;
        case AVAuthorizationStatusRestricted:
        {
          
            // 被拒绝
            authorizationBlock(NO);
        }
            break;
        case AVAuthorizationStatusNotDetermined:
        {
            // 没弹窗
             [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
                if (granted) {
                  authorizationBlock(YES);
                }else{
                authorizationBlock(NO);
                }
             }];
        }
            break;
        case AVAuthorizationStatusAuthorized:
        {
            // 有授权
            authorizationBlock(YES);
        }
            break;

        default:
            break;
    }
}


//判断是否有中文
+(BOOL)Fish_haveChinese:(NSString *)str {
    
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff){
            return YES;
        }
    } return NO;
}
+ (BOOL)Fish_haveEmoji:(NSString *)string{
    __block BOOL isEomji = NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
         const unichar hs = [substring characterAtIndex:0];
         //         NSLog(@"hs++++++++%04x",hs);
         if (0xd800 <= hs && hs <= 0xdbff) {
             if (substring.length > 1) {
                 const unichar ls = [substring characterAtIndex:1];
                 const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                 if (0x1d000 <= uc && uc <= 0x1f77f)
                 {
                     isEomji = YES;
                 }
                 
             }
         } else if (substring.length > 1) {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0x20e3|| ls ==0xfe0f) {
                 isEomji = YES;
             }
             //             NSLog(@"ls++++++++%04x",ls);
         } else {
             if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
                 isEomji = YES;
             } else if (0x2B05 <= hs && hs <= 0x2b07) {
                 isEomji = YES;
             } else if (0x2934 <= hs && hs <= 0x2935) {
                 isEomji = YES;
             } else if (0x3297 <= hs && hs <= 0x3299) {
                 isEomji = YES;
             } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50|| hs == 0x231a ) {
                 isEomji = YES;
             }
         }
         
     }];
    return isEomji;
}

+(BOOL)Fish_checkPassword:(NSString*)password{//有特殊字符，有大写字母，有数字
    
    NSString *str =@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";//有字母，有数字，8-16位
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [emailTest evaluateWithObject:password];//是否符合规范;
  
}



+ (NSString *)AFPercentEscapedStringFromString:(NSString *)string{
    
    NSString *kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4
    NSString *kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;=";
    
    NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
    [allowedCharacterSet removeCharactersInString:[kAFCharactersGeneralDelimitersToEncode stringByAppendingString:kAFCharactersSubDelimitersToEncode]];
    // FIXME: https://github.com/AFNetworking/AFNetworking/pull/3028
    // return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
    
    NSUInteger batchSize = 50;
    
    NSUInteger index = 0;
    NSMutableString *escaped = @"".mutableCopy;
    
    while (index < string.length) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wgnu"
        NSUInteger length = MIN(string.length - index, batchSize);
#pragma GCC diagnostic pop
        NSRange range = NSMakeRange(index, length);
        
        // To avoid breaking up character sequences such as 👴🏻👮🏽
        range = [string rangeOfComposedCharacterSequencesForRange:range];
        
        NSString *substring = [string substringWithRange:range];
        NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
        [escaped appendString:encoded];
        
        index += range.length;
    }
    
    return [escaped copy];
}


@end
