package com.base.scanqrclear.luma

import android.os.Build
import android.util.Log
import com.base.scanqrclear.BuildConfig
import com.base.scanqrclear.GlobalConfig.PACKAGE_NAME
import com.base.scanqrclear.MyApplication
import com.base.scanqrclear.luma.AdmobHelps.KEY_DEVICE_NAME
import com.base.scanqrclear.luma.AdmobHelps.KEY_GOOGLE_ADVERTISER_ID
import com.base.scanqrclear.luma.AdmobHelps.KEY_UUID
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.util.Locale
import java.util.TimeZone

object DeviceHelps {

    fun init() {
        MainScope().launch(Dispatchers.IO) {
            getGoogleAdvertiserId()
            getDeviceName()
        }
    }

    private fun getGoogleAdvertiserId() {
        try {
            val googleAdvertiserId = AdvertisingIdClient.getAdvertisingIdInfo(MyApplication.appContext).id ?: ""
            if (googleAdvertiserId.isNotEmpty())
                SpUtils.getInstance().putString(KEY_GOOGLE_ADVERTISER_ID, googleAdvertiserId)
        } catch (e: Exception) {
            Log.d("TAG", "getGoogleAdvertiserId error: ${e.localizedMessage}")
        }
    }

    private fun getDeviceName() {
        val manufacturer = Build.MANUFACTURER
        val model = Build.MODEL
        val flag = model.lowercase(Locale.getDefault()).startsWith(manufacturer.lowercase(Locale.getDefault()))

        val deviceName = if (flag) {
            if (model.isNullOrEmpty()) {
                model
            } else {
                model.substring(0, 1)
                    .uppercase(Locale.getDefault()) + model.substring(1)
                    .lowercase(Locale.getDefault())
            }

        } else {
            val ss = if (manufacturer.isNullOrEmpty()) {
                manufacturer
            } else {
                manufacturer.substring(0, 1)
                    .uppercase(Locale.getDefault()) + manufacturer.substring(1)
                    .lowercase(Locale.getDefault())
            }
            "$ss $model"
        }
        SpUtils.getInstance().putString(KEY_DEVICE_NAME, deviceName)
    }

    // 获取配置参数
    fun getConfigParams(): JSONObject {
        val packageName = PACKAGE_NAME
        val jsonObject = JSONObject()
        jsonObject.put("${packageName}_3", SpUtils.getInstance().getString(KEY_DEVICE_NAME))//手机型号
        jsonObject.put("${packageName}_4", Build.MANUFACTURER)//手机厂商
        jsonObject.put("${packageName}_5", Build.VERSION.SDK_INT)//android系统版本号
        jsonObject.put("${packageName}_8", BuildConfig.VERSION_NAME)//APP版本号，如：1.1.2
        jsonObject.put("${packageName}_9", SpUtils.getInstance().getString(KEY_UUID))//Android id
        jsonObject.put("${packageName}_10", SpUtils.getInstance().getString(KEY_GOOGLE_ADVERTISER_ID))//Google advertiser id
        jsonObject.put("${packageName}_13", "android")//platform  默认android
        jsonObject.put("${packageName}_14", BuildConfig.VERSION_CODE)//android版本，如：13
        jsonObject.put("${packageName}_15", "google")
        jsonObject.put("${packageName}_24", BuildConfig.BUILD_TYPE)//环境
        val timeZone: TimeZone = TimeZone.getDefault()
        jsonObject.put("${packageName}_34", timeZone.id) // 手机本地时区，值如下格式：America/New_York
        return jsonObject
    }
}