package com.zxdemo.utils
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.webkit.WebSettings
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.zxdemo.ZxApplication
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.util.UUID

object DeviceUtils {
    fun getScreenHeight(): Int {
        return ZxApplication.context.resources.displayMetrics.heightPixels
    }

    fun getScreenWidth(): Int {
        return ZxApplication.context.resources.displayMetrics.widthPixels
    }

    fun getDeviceModel(): String {
        return Build.MODEL
    }

    fun getManufacturer(): String {
        return Build.MANUFACTURER
    }

    fun getAndroidVersion(): String {
        return Build.VERSION.RELEASE
    }

    fun getAppVersion(): String {
        return try {
            val packageInfo = ZxApplication.context.packageManager.getPackageInfo(
                ZxApplication.context.packageName,
                0
            )
            packageInfo.versionName
        } catch (e: Exception) {
            "Unknown"
        }
    }

    fun getAndroidID(): String {
        return Settings.Secure.getString(
            ZxApplication.context.contentResolver,
            Settings.Secure.ANDROID_ID
        )
    }

    fun getGoogleAdvertiserId() {
        GlobalScope.launch(Dispatchers.IO) {
            try {
                ZxApplication.GID =
                    AdvertisingIdClient.getAdvertisingIdInfo(ZxApplication.context).id ?: ""
            } catch (e: Exception) {
                Log.d("TAG", "getGoogleAdvertiserId: ${e.printStackTrace()}")
            }
        }
    }

    fun getUID(): String {
        return UUID.randomUUID().toString() + System.currentTimeMillis()
    }


    fun getUserAgent(): String {
        return WebSettings.getDefaultUserAgent(ZxApplication.context)
    }

    fun getAndroidVersionCode(): Int {
        return Build.VERSION.SDK_INT
    }

    fun getConfigParms(): JSONObject {
        var packname = "com.a.xxxxxx.m.storage.zxcv"
        var jsonObject = JSONObject()
        jsonObject.put("${packname}_1", getScreenHeight())//高度
        jsonObject.put("${packname}_2", getScreenWidth())//宽度
        jsonObject.put("${packname}_3", getDeviceModel())//手机型号
        jsonObject.put("${packname}_4", getManufacturer())//手机厂商
        jsonObject.put("${packname}_5", getAndroidVersion())//android系统版本号
        jsonObject.put("${packname}_8", getAppVersion())//APP版本号，如：1.1.2
        jsonObject.put("${packname}_9", getAndroidID())//Android id
        jsonObject.put("${packname}_10", ZxApplication.GID)//Google advertiser id
        jsonObject.put("${packname}_11", getUserAgent())//User agent
        jsonObject.put("${packname}_12", getUID())//用户id
        jsonObject.put("${packname}_13", "android")//platform  默认android
        jsonObject.put("${packname}_14", getAndroidVersionCode())//android版本，如：13
//        jsonObject.put("${packname}_23","en")//语言
//        jsonObject.put("${packname}_24", BuildConfig.BUILD_TYPE)//环境
        return jsonObject
    }
}