//
//  GMNetStatusUtils.swift
//  Alamofire
//
//  Created by Locus on 2019/6/12.
//

import Foundation
import Alamofire
import CoreTelephony


public class GMNetworkStatusUtils: NSObject {
    
    @objc public enum GMNetworkStatus : Int {
        case unknown
        case notReachable
        case ethernetOrWifi
        case network2G
        case network3G
        case network4G
        case networkWWAN// 是数据流量，但无法判断是几G的时候返回
    }
    
    @objc(networkStatus)
    public static func networkStatus() -> GMNetworkStatus {
        
        guard let manager = NetworkReachabilityManager.init() else {
            return .notReachable
        }
        if !manager.isReachable {
            return .notReachable
        }
        if manager.isReachableOnEthernetOrWiFi {
            return .ethernetOrWifi
        }
        if manager.isReachableOnWWAN {
            let info = CTTelephonyNetworkInfo.init()
            guard let currentStatus = info.currentRadioAccessTechnology else {
                return .networkWWAN
            }
            let network2G = [CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge, CTRadioAccessTechnologyCDMA1x]
            let network3G = [CTRadioAccessTechnologyWCDMA, CTRadioAccessTechnologyHSDPA, CTRadioAccessTechnologyHSUPA, CTRadioAccessTechnologyCDMAEVDORev0, CTRadioAccessTechnologyCDMAEVDORevA, CTRadioAccessTechnologyCDMAEVDORevB, CTRadioAccessTechnologyeHRPD];
            let network4G = [CTRadioAccessTechnologyLTE];
            
            if network2G.contains(currentStatus) {
                return .network2G
            } else if network3G.contains(currentStatus) {
                return .network3G
            } else if network4G.contains(currentStatus) {
                return .network4G
            } else {
                return .networkWWAN
            }
        }
        return .unknown
    }
}


