package com.abbidot.baselibrary.util

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import com.abbidot.baselibrary.constant.MMKVKey
import java.util.Locale


/**
 *Created  by .yzq on 2021/11/6/006.
 * @link
 * @description: app属性相关工具类
 */
class AppUtils {

    companion object {

        private var isDebug: Boolean? = null

        /**
         * 屏幕密度,系统源码注释不推荐使用
         */
        private val DENSITY = Resources.getSystem().displayMetrics.density
        private val SCALED_DENSITY = Resources.getSystem().displayMetrics.scaledDensity

        fun isDebug(): Boolean {
            return if (isDebug == null) false else isDebug!!
        }

        fun syncIsDebug(context: Context) {
            if (isDebug == null) {
                isDebug =
                    context.applicationInfo != null && context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
            }
        }


        /**
         * 根据包名判断app是否安装 高版本不兼容
         */
        fun isAppInstalled(context: Context, packageName: String): Boolean {
            val pm = context.packageManager
            return try {
                pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
                true
            } catch (e: PackageManager.NameNotFoundException) {
                false
            }
        }

        /**
         * 把以 dp 为单位的值，转化为以 px 为单位的值
         *
         * @param dpValue 以 dp 为单位的值
         * @return px value
         */
        fun dpToPx(dpValue: Float) = dpValue * DENSITY + 0.5f

        /**
         * 把以 dp 为单位的值，转化为以 px 为单位的值
         *
         * @param dpValue 以 dp 为单位的值
         * @return px value
         */
        fun dpToPx(dpValue: Int) = (dpValue * DENSITY + 0.5f).toInt()

        /**
         * 单位转换: sp -> px
         *
         * @param sp
         * @return
         */
        fun spToPx(sp: Int) = (SCALED_DENSITY * sp + 0.5).toInt()

        /**
         * 单位转换: sp -> px
         *
         * @param sp
         * @return
         */
        fun spToPx(sp: Float) = SCALED_DENSITY * sp + 0.5f


        const val SWITCH_MAP_TYPE = "Map"

        //        const val SWITCH_BASE_URL_TYPE = "BaseUrl"
        private const val SWITCH_PAGE_TYPE = "PageUrl"

        /**
         * 是否是国内
         */
        fun isChina(type: String = SWITCH_PAGE_TYPE): Boolean {
            return false
            if (isDebug()) {
                if (type == SWITCH_MAP_TYPE && MMKVUtil.getBoolean(MMKVKey.OnlyGoogleMap, false)) {
                    //只启用谷歌地图
                    return false
                }
                return MMKVUtil.getBoolean(MMKVKey.DebugIp, true)
            }
            val locale = Locale.getDefault()
            val lang = locale.language + "-" + locale.country
            if (lang.contains("CN") || lang.contains("zh")) {
                return true
            }
            return false
        }

        /**
         * 判断是否是深色模式
         */
        fun isDarkThemeEnabled(context: Context): Boolean {
            val nightModeFlags =
                context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
            return nightModeFlags == Configuration.UI_MODE_NIGHT_YES
        }
    }
}