//
//  UIDevice+Addtion.m
//  Talk
//
//  Created by 梁汇丰 on 2025/4/16.
//

#import "UIDevice+Addtion.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

//IP地址需求库
#import <sys/socket.h>
#import <sys/sockio.h>
#import <sys/ioctl.h>
#import <net/if.h>
#import <arpa/inet.h>

@implementation UIDevice (Addtion)

/// 顶部安全区高度
+ (CGFloat)safeDistanceTop {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.top;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.top;
    }
    return 0;
}

/// 底部安全区高度
+ (CGFloat)safeDistanceBottom {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.bottom;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.bottom;
    }
    return 0;
}


/// 顶部状态栏高度（包括安全区）
+ (CGFloat)statusBarHeight {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
        return statusBarManager.statusBarFrame.size.height;
    } else {
        return [UIApplication sharedApplication].statusBarFrame.size.height;
    }
}

/// 导航栏高度
+ (CGFloat)navigationBarHeight {
    return 44.0f;
}

/// 状态栏+导航栏的高度
+ (CGFloat)navigationFullHeight {
    return [UIDevice statusBarHeight] + [UIDevice navigationBarHeight];
}

/// 底部导航栏高度
+ (CGFloat)tabBarHeight {
    return 49.0f;
}

/// 底部导航栏高度（包括安全区）
+ (CGFloat)tabBarFullHeight {
    return [UIDevice statusBarHeight] + [UIDevice safeDistanceBottom];
}

+(NSString *)networkInfo{
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    NSString *isoCountryCode = @"";
    
    if (@available(iOS 12.0, *)) {
        NSDictionary *cellularDict = [info serviceSubscriberCellularProviders];
        for (CTCarrier *carrier in cellularDict.allValues) {
            
            isoCountryCode = [carrier isoCountryCode]; // cn
            
            if(isoCountryCode == nil || isoCountryCode.length == 0){
                continue;
            }
            
            NSString *mcc = [carrier mobileCountryCode]; // 国家码 如：460
            NSString *mnc = [carrier mobileNetworkCode]; // 网络码 如：01
            NSString *name = [carrier carrierName]; // 运营商名称，中国联通
           
        //    BOOL allowsVOIP = [carrier allowsVOIP];// YES
            NSDictionary *radioAccessTechnology = info.serviceCurrentRadioAccessTechnology; // 无线连接技术，如CTRadioAccessTechnologyLTE

//            NSDictionary *dict = info.serviceCurrentRadioAccessTechnology;

//            DLog(@"=====>> mcc:%@  mnc:%@  name:%@  isoCountryCode:%@  radioAccessTechnology:%@",mcc,mnc,name,isoCountryCode,radioAccessTechnology);
//            NSString *str = [NSString stringWithFormat:@"=====>> mcc:%@  mnc:%@  name:%@  isoCountryCode:%@  radioAccessTechnology:%@",mcc,mnc,name,isoCountryCode,radioAccessTechnology];

//            [MJKToast showText:str duration:10 onView:kAppDelegate.window];
            
        }

    }else{
        NSDictionary *carrier = [info serviceSubscriberCellularProviders];
//        DLog(@"carrier:%@",carrier);
//        NSString *mcc = [carrier mobileCountryCode]; // 国家码 如：460  📢没卡的情况下这个没有值
//        NSString *mnc = [carrier mobileNetworkCode]; // 网络码 如：01 📢没卡的情况下这个没有值
//        NSString *name = [carrier carrierName]; // 运营商名称，中国联通  *没卡的情况有值
//        isoCountryCode = [carrier isoCountryCode]; // cn  📢没卡的情况下这个没有值
//    
//        NSString *radioAccessTechnology = info.currentRadioAccessTechnology; // 无线连接技术，如CTRadioAccessTechnologyLTE 📢飞行模式下这个没有值
//        NSString *str = [NSString stringWithFormat:@"=====>> mcc:%@  mnc:%@  name:%@  isoCountryCode:%@  radioAccessTechnology:%@",mcc,mnc,name,isoCountryCode,radioAccessTechnology];
//    
//        DLog(@"=====>> mcc:%@  mnc:%@  name:%@  isoCountryCode:%@  radioAccessTechnology:%@",mcc,mnc,name,isoCountryCode,radioAccessTechnology);
    }
        
    return isoCountryCode;
    
}

/// 获取当前的系统语言
+ (NSString *)deviceCurrentLanguage{
    NSString *languageCode = [NSLocale preferredLanguages][0];// 返回的也是国际通用语言Code+国际通用国家地区代码
    
    NSString *countryCode = [NSString stringWithFormat:@"-%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];//国家代码
    if (languageCode) {
        languageCode = [languageCode stringByReplacingOccurrencesOfString:countryCode withString:@""];
    }
//    DLog(@"languageCode : %@", languageCode);
    
    return languageCode;
}

@end
