package com.narutohuo.xindazhou.common.networkrx

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build

/**
 * 网络工具类
 * 
 * 提供网络状态检测等功能
 * 
 * 对应 MVVMHabit 的 NetworkUtil
 */
object NetworkUtil {
    
    /**
     * 检查网络是否连接
     * 
     * @param context 上下文
     * @return 是否连接网络
     */
    fun isNetworkConnected(context: Context): Boolean {
        val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val network = connectivityManager.activeNetwork ?: return false
            val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
            return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
        } else {
            // 旧版本 API
            val networkInfo = connectivityManager.activeNetworkInfo ?: return false
            return networkInfo.isConnected
        }
    }
    
    /**
     * 检查是否是 WiFi 连接
     * 
     * @param context 上下文
     * @return 是否是 WiFi 连接
     */
    fun isWifiConnected(context: Context): Boolean {
        val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val network = connectivityManager.activeNetwork ?: return false
            val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
            return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
        } else {
            // 旧版本 API
            val networkInfo = connectivityManager.activeNetworkInfo ?: return false
            return networkInfo.type == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected
        }
    }
    
    /**
     * 检查是否是移动网络连接
     * 
     * @param context 上下文
     * @return 是否是移动网络连接
     */
    fun isMobileConnected(context: Context): Boolean {
        val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val network = connectivityManager.activeNetwork ?: return false
            val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
            return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
        } else {
            // 旧版本 API
            val networkInfo = connectivityManager.activeNetworkInfo ?: return false
            return networkInfo.type == ConnectivityManager.TYPE_MOBILE && networkInfo.isConnected
        }
    }
    
    /**
     * 获取网络类型
     * 
     * @param context 上下文
     * @return 网络类型
     */
    fun getNetworkType(context: Context): NetworkType {
        if (!isNetworkConnected(context)) {
            return NetworkType.NONE
        }
        
        if (isWifiConnected(context)) {
            return NetworkType.WIFI
        }
        
        if (isMobileConnected(context)) {
            return NetworkType.MOBILE
        }
        
        return NetworkType.OTHER
    }
    
    /**
     * 网络类型
     */
    enum class NetworkType {
        /** 无网络 */
        NONE,
        /** WiFi */
        WIFI,
        /** 移动网络 */
        MOBILE,
        /** 其他网络 */
        OTHER
    }
}