package com.composition.android.lib.ad.util

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.view.View
import android.view.ViewGroup
import kotlinx.coroutines.CancellableContinuation
import kotlin.coroutines.resume

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/11/1
 * description:
 */

fun View.parentRemove() {
    try {
        if (this.parent != null) {
            (this.parent as ViewGroup).removeView(this)
        }
    }catch (e : Exception){

    }
}

fun View.getActivityFromView() : Activity?{
    var cont = context
    while (cont is ContextWrapper){
        if (cont is Activity){
            return cont as Activity
        }
        cont = cont.baseContext
    }
    return null
}

/**
 * @方法说明:网络是否可用
 * @方法名称:isNetworkAvailable
 * @param context
 * @return
 * @返回值:boolean
 */
@SuppressLint("MissingPermission")
fun isNetworkAvailable(context: Context?): Boolean {
    if (context == null) return false
    try {
        val connectivityManager = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = connectivityManager.activeNetwork ?: return false
            val actNw = connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
            when {
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
                        actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
                        actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
                else -> false
            }
        } else {
            connectivityManager.run {
                activeNetworkInfo?.run {
                    when (type) {
                        ConnectivityManager.TYPE_WIFI,
                        ConnectivityManager.TYPE_MOBILE,
                        ConnectivityManager.TYPE_ETHERNET -> true
                        else -> false
                    }
                } ?: false
            }
        }
    }catch (e : Exception){
        return false
    }

}




/**
 * 屏幕宽度
 */
fun getScreenWidth(context: Context): Int = context.resources?.displayMetrics?.widthPixels ?: 0

/**
 * 获取屏幕密度
 */
fun getDensity(context: Context): Float = context.resources?.displayMetrics?.density ?: 0F

/**
 * 屏幕高度
 */
fun getScreenHeight(context: Context): Int =context.resources?.displayMetrics?.heightPixels ?: 0

fun getScreenWidthDp(context: Context) : Float = getScreenWidth(context) / (if (getDensity(context) <= 0) 1F else getDensity(context)) + 0.5f

fun getScreenHeightDp(context: Context) : Float = px2dip(context, getScreenHeight(context).toFloat()).toFloat()

/**
 * @param ：[context, dpValue]
 * @return : int
 * @methodName ：dip2px created by luozhipeng on 5/12/17 13:57.
 * @description ：根据手机的分辨率从 dp 的单位 转成为 px(像素)
 */
fun dp2px(context: Context, dpValue: Float): Int {
    val scale = getDensity(context = context)
    return (dpValue * scale + 0.5f).toInt()
}

fun px2dip(context: Context,pxValue: Float): Int {
    val scale = getDensity(context)
    return (pxValue / scale + 0.5f).toInt()
}


fun <T> CancellableContinuation<T>.isActiveResult(value : T){
    if (isActive){
        resume(value)
    }
}