package com.ccwangluo.accelerator.utils

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.text.TextUtils
import android.util.DisplayMetrics
import com.ccwangluo.accelerator.model.NetState
import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xutil.net.NetworkUtils
import java.io.BufferedReader
import java.io.InputStreamReader
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import android.app.DownloadManager
import android.content.Intent
import android.net.*
import com.hjq.toast.ToastUtils
import java.lang.Exception


object SysUtils {

    fun getConnectivityStatus(): Boolean {
        return NetworkUtils.isHaveInternet()
    }

    fun getActiveNet(): String {
        when (NetworkUtils.getNetStateType()) {
            NetworkUtils.NetState.NET_WIFI -> return "WIFI"
            NetworkUtils.NetState.NET_5G -> return "5G"
            NetworkUtils.NetState.NET_4G -> return "4G"
            NetworkUtils.NetState.NET_3G -> return "3G"
            NetworkUtils.NetState.NET_2G -> return "2G"
            NetworkUtils.NetState.NET_UNKNOWN -> return "UNKNOWN"
            NetworkUtils.NetState.NET_NO -> return "__"
            else -> return "__"
        }
    }



    /*
    * 判断设备 是否使用代理上网
    * */
    fun isVpn(context: Context): Boolean {
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetwork = connectivityManager?.getActiveNetwork()
        val caps = connectivityManager.getNetworkCapabilities(activeNetwork)
        caps?.hasTransport(NetworkCapabilities.TRANSPORT_VPN)?.let {
            return it
        }
        return false
    }

    /**
     * 判断包是否安装
     */
    fun isPackageInstall(context: Context, packageName: String): Boolean {
        val packageManager: PackageManager = context.getPackageManager()
        val packageInfos = packageManager.getInstalledPackages(0)
        val packageNames: MutableList<String> = ArrayList()
        if (packageInfos != null) {
            for (i in packageInfos.indices) {
                val packName = packageInfos[i].packageName
                packageNames.add(packName)
            }
        }
        return packageNames.contains(packageName)
    }

    fun getnetQuality(delay: Int): String {
        when (delay) {
            in (1..30) -> return "极快"
            in (31..50) -> return "良好"
            in (51..100) -> return "一般"
            else -> return "差"
        }
    }

    fun getAccTimeString(time: Long): String {
        return "加速中 ${tranlateTime(time)} "
    }

    fun tranlateTime(long: Long): String {
        val formatter = SimpleDateFormat("HH:mm:ss");//这里想要只保留分秒可以写成"mm:ss"
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        val hms = formatter.format(long);
        return hms
    }

    /**
     * 获取dpi
     */
    fun getDpi(xPageFragment: XPageFragment): Float {
        val dm = DisplayMetrics()
        xPageFragment.activity?.windowManager?.getDefaultDisplay()?.getMetrics(dm)
        return (dm.density * 160)
    }

    fun downloadFile(context: Context, url: String) {
        val request = DownloadManager.Request(Uri.parse(url))
        request.setDestinationInExternalPublicDir("", url.substring(url.lastIndexOf("/") + 1))
        val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        downloadManager.enqueue(request)
    }

    fun joinQQGroup(context: Context) {
        val key = "388Tnjzn_jkY7SNKO1ACI6PL5gbhpeYR"
        val intent = Intent()
        intent.data =
            Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26jump_from%3Dwebapi%26k%3D$key")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            context.startActivity(intent)
        } catch (e: Exception) {
            ToastUtils.show("没有安装qq")
        }
    }
}