package com.miya.fastcashier.utils

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.text.TextUtils
import java.net.Inet4Address
import java.net.NetworkInterface
import java.net.SocketException

fun isEmpty(s: String?): Boolean {
    return TextUtils.isEmpty(s)
}

fun isEmpty(list: List<Any>?): Boolean {
    return list == null || list.isEmpty()
}

fun sp2px(context: Context, spValue: Float): Int {
    val fontScale: Float = context.resources.displayMetrics.scaledDensity
    return (spValue * fontScale + 0.5f).toInt()
}

/**
 * 根据分辨率从 dp 的单位 转成为 px(像素)
 */
fun dp2px(context: Context, dpValue: Float): Int {
    val scale = context.resources.displayMetrics.density
    return (dpValue * scale + 0.5f).toInt()
}

fun getNetIp(c: Context): String? {
    val info =
        (c.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo
    if (info != null && info.isConnected) {
        if (info.type == ConnectivityManager.TYPE_MOBILE) { //当前使用2G/3G/4G网络
            try {
                //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
                val en = NetworkInterface.getNetworkInterfaces()
                while (en.hasMoreElements()) {
                    val intf = en.nextElement()
                    val enumIpAddr = intf.inetAddresses
                    while (enumIpAddr.hasMoreElements()) {
                        val inetAddress = enumIpAddr.nextElement()
                        if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                            return inetAddress.getHostAddress()
                        }
                    }
                }
            } catch (e: SocketException) {
                e.printStackTrace()
            }
        } else if (info.type == ConnectivityManager.TYPE_WIFI) { //当前使用无线网络
            val wifiManager = c.getSystemService(Context.WIFI_SERVICE) as WifiManager
            val wifiInfo = wifiManager.connectionInfo
            return intIP2StringIP(wifiInfo.ipAddress) //得到IPV4地址
        }
    } else {
        //当前无网络连接,请在设置中打开网络
    }
    return null
}

/**
 * 将得到的int类型的IP转换为String类型
 *
 * @param ip
 * @return
 */
fun intIP2StringIP(ip: Int): String? {
    return (ip and 0xFF).toString() + "." +
            (ip shr 8 and 0xFF) + "." +
            (ip shr 16 and 0xFF) + "." +
            (ip shr 24 and 0xFF)
}


/**
 * 获取wifi名称（即ssid）
 *
 * @param c
 * @return
 */
fun getWifyName(c: Context): String? {
    val mWifiManager = c.getSystemService(Context.WIFI_SERVICE) as WifiManager
    return mWifiManager.connectionInfo.ssid
}

fun getVersion(context: Context): String? {
    val packageManager = context.packageManager
    val packageInfo: PackageInfo
    var version = ""
    try {
        packageInfo = packageManager.getPackageInfo(context.packageName, 0)
        version = "${packageInfo.versionCode}_v${packageInfo.versionName}"
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
    return version
}


/**
 *  判断是否有网络连接
 *  @param context
 */
fun isNetworkConnected(context: Context?): Boolean {
    if (context != null) {
        val mConnectivityManager = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val mNetworkInfo = mConnectivityManager.activeNetworkInfo
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable
        }
    }
    return false
}
