//
//  AppTools.m
//  fastservice
//
//  Created by xuning on 5/5/25.
//  Copyright © 2025 FastService. All rights reserved.
//

#import "AppTools.h"
#import "CustomMethod.h"

@interface AppTools()
@property (nonatomic,assign) int version_int;
@end

@implementation AppTools
DEF_SINGLETION(AppTools);

- (int)versionToIntValue{
    if (self.version_int == 0) {
        //将版本号点去除，拼接成int型传给服务器
        NSArray *vs = [APP_VERSION componentsSeparatedByString:@"."];
        NSMutableString *ver = [NSMutableString string];
        for (NSString *string in vs) {
            [ver appendString:string];
        }
        self.version_int = [ver intValue];
    }
    return self.version_int;
}

//json串 unicode转中文
+ (NSString *)replaceUnicode:(NSString *)unicodeStr {
    if (StringIsNullOrEmpty(unicodeStr)) {
        return @"空数据";
    }else{
        NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
        NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
        NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
        NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
        NSString* returnStr = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:NULL];
        return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
    }
}

//判断电话合法性
+ (BOOL)isValidMobeilTelWithString:(NSString *)string{
    BOOL isValisMobeil = NO;
    
    //手机号码
    NSArray *mobeils = [CustomMethod addPhoneNumArr:string];
    if (mobeils.count > 0 && [[mobeils firstObject] length] > 0) {
        isValisMobeil = YES;
    }else{
        //电话号码正则
        NSString * PHS = @"";
        if ([string hasPrefix:@"400"]) {
            PHS = @"^400[0-9]{7}";
        }
        else if ([string hasPrefix:@"800"]) {
            PHS = @"^800[0-9]{7}";
        }
        else{
            PHS = @"^(0[0-9]{2})\\d{8}$|^(0[0-9]{3}(\\d{7,8}))$";
        }
        NSPredicate *regextesTel = [NSPredicate predicateWithFormat:@"SELF     MATCHES %@", PHS];
        if ([regextesTel evaluateWithObject:string]) {
            isValisMobeil = YES;
        }else{
            showAlertMessage(S(@"电话号码不合法:%@",string));
            isValisMobeil = NO;
        }
    }
    return isValisMobeil;
}


+ (NSString *)kfwUUID{
    CFUUIDRef uuidObj = CFUUIDCreate(nil);
    NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
    CFRelease(uuidObj);
    return uuidStr;
}


//获取验证码时 加密key
+ (void)ccodeBase64Encode:(NSMutableDictionary *)pDic {
    NSString *ccode_key = @"kfw123456";
    NSString *time_since = S(@"%d",(int)[[NSDate date] timeIntervalSince1970]);
    NSString *ccode = S(@"%@%@",ccode_key,[time_since substringWithRange:NSMakeRange(time_since.length - 4, 4)]);
    ccode = [ccode base64Encode];
    [pDic setAnyObject:ccode forKey:@"ccode"];//key加时间  base 64
    [pDic setAnyObject:time_since forKey:@"tcode"];
//    DebugLog(@"ccode_key:%@",ccode_key);
}

@end
