package com.keyborad.theme.trendyborad.environment.hy;

import android.annotation.SuppressLint
import android.content.Context
import android.net.wifi.WifiManager
import android.os.Build
import android.telephony.TelephonyManager
import android.webkit.WebSettings
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.google.gson.JsonObject

object PhoneInfoUtil {
    //日志服务器相关
    const val LOG_STORE = "vault_data"

    //参数名
    const val GOOGLE_ADID = "google_adid"
    const val BUILD_ID = "build_id"
    const val BUILD_DISPLAY = "build_display"
    const val BUILD_INCREMENTAL = "build_incremental"
    const val BUILD_SDK_INT = "build_sdk_int"
    const val BUILD_CODENAME = "build_codename"
    const val BUILD_RELEASE = "build_release"
    const val BUILD_TYPE = "build_type"
    const val BUILD_USER = "build_user"
    const val BUILD_HOST = "build_host"
    const val BUILD_TAGS = "build_tags"
    const val BUILD_MODEL = "build_model"
    const val BUILD_BRAND = "build_brand"
    const val BUILD_PRODUCT = "build_product"
    const val BUILD_DEVICE = "build_device"
    const val BUILD_BOARD = "build_board"
    const val BUILD_CPU_ABI = "build_cpu_abi"
    const val BUILD_CPU_ABI2 = "build_cpu_abi2"
    const val BUILD_MANUFACTURER = "build_manufacturer"
    const val BUILD_FINGERPRINT = "build_fingerprint"
    const val BUILD_SERIAL = "build_serial"
    const val BUILD_HARDWARE = "build_hardware"
    const val BUILD_BOOTLOADER = "build_bootloader"
    const val SETTINGS_SECURE_ANDROID_ID = "settings_secure_android_id"
    const val TELE_GETDEVICEID = "tele_getdeviceid"
    const val TELE_GETDEVICESOFTWAREVERSION = "tele_getdevicesoftwareversion"
    const val TELE_GETLINE1NUMBER = "tele_getline1number"
    const val TELE_GETNETWORKCOUNTRYISO = "tele_getnetworkcountryiso"
    const val TELE_GETNETWORKOPERATOR = "tele_getnetworkoperator"
    const val TELE_GETNETWORKOPERATORNAME = "tele_getnetworkoperatorname"
    const val TELE_GETNETWORKTYPE = "tele_getnetworktype"
    const val TELE_GETPHONETYPE = "tele_getphonetype"
    const val TELE_GETSIMCOUNTRYISO = "tele_getsimcountryiso"
    const val TELE_GETSIMOPERATOR = "tele_getsimoperator"
    const val TELE_GETSIMOPERATORNAME = "tele_getsimoperatorname"
    const val TELE_GETSIMSERIALNUMBER = "tele_getsimserialnumber"
    const val TELE_GETSIMSTATE = "tele_getsimstate"
    const val TELE_GETSUBSCRIBERID = "tele_getsubscriberid"
    const val WEB_USERAGENT = "web_useragent"
    const val WIFIINFO_GETBSSID = "wifiinfo_getbssid"
    const val WIFIINFO_GETMACADDRESS = "wifiinfo_getmacaddress"
    const val WIFIINFO_GETNETWORKID = "wifiinfo_getnetworkid"
    const val WIFIINFO_GETSSID = "wifiinfo_getssid"

    /**
     * 需要异步，有异步获取的内容
     */
    @SuppressLint("MissingPermission")
    fun getPhoneInfo(ctx: Context): PhoneInfo {
        val phoneInfo = PhoneInfo()
        try {
            phoneInfo.gaid = AdvertisingIdClient.getAdvertisingIdInfo(ctx.applicationContext).id
        } catch (e: Exception) {
            e.printStackTrace()
        }
        phoneInfo.build_id = Build.ID
        phoneInfo.build_display = Build.DISPLAY
        phoneInfo.build_incremental = Build.VERSION.INCREMENTAL
        phoneInfo.build_sdk_int = Build.VERSION.SDK_INT
        phoneInfo.build_codename = Build.VERSION.CODENAME
        phoneInfo.build_release = Build.VERSION.RELEASE
        phoneInfo.build_type = Build.TYPE
        phoneInfo.build_user = Build.USER
        phoneInfo.build_host = Build.HOST
        phoneInfo.build_tags = Build.TAGS
        phoneInfo.build_model = Build.MODEL
        phoneInfo.build_brand = Build.BRAND
        phoneInfo.build_product = Build.PRODUCT
        phoneInfo.build_device = Build.DEVICE
        phoneInfo.build_board = Build.BOARD
        phoneInfo.build_cpu_abi = Build.CPU_ABI
        phoneInfo.build_cpu_abi2 = Build.CPU_ABI2
        phoneInfo.build_manufacturer = Build.MANUFACTURER
        phoneInfo.build_fingerprint = Build.FINGERPRINT
//        phoneInfo.build_serial = Build.SERIAL
        phoneInfo.build_serial = ""
        phoneInfo.build_hardware = Build.HARDWARE
        phoneInfo.build_bootloader = Build.BOOTLOADER
        try {
//            phoneInfo.settings_secure_android_id = Settings.Secure.getString(
//                ctx.applicationContext.contentResolver,
//                Settings.Secure.ANDROID_ID
//            )
            phoneInfo.settings_secure_android_id = ""
        } catch (ignore: Exception) {
        }
        val manager =
            ctx.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        if (manager != null) {
            try {
//                if (Build.VERSION.SDK_INT >= 23) {
//                    phoneInfo.tel_get_device_id = manager.getDeviceId(0)
//                } else {
//                    phoneInfo.tel_get_device_id = manager.deviceId
//                }

                phoneInfo.tel_get_device_id = ""
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_device_software_version = manager.deviceSoftwareVersion
            } catch (ignore: Exception) {
            }
            try {
//                phoneInfo.tel_get_line1_number = manager.line1Number
                phoneInfo.tel_get_line1_number = ""
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_network_country_iso = manager.networkCountryIso
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_network_operator = manager.networkOperator
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_network_operator_name = manager.networkOperatorName
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_phone_type = manager.phoneType
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_sim_country_iso = manager.simCountryIso
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_sim_operator = manager.simOperator
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_sim_operator_name = manager.simOperatorName
            } catch (ignore: Exception) {
            }
            try {
//                phoneInfo.tel_get_sim_serial_number = manager.simSerialNumber
                phoneInfo.tel_get_sim_serial_number = ""
            } catch (ignore: Exception) {
            }
            try {
                phoneInfo.tel_get_sim_state = manager.simState
            } catch (ignore: Exception) {
            }
            try {
//                phoneInfo.tel_get_subscriber_id = manager.subscriberId
                phoneInfo.tel_get_subscriber_id = ""
            } catch (ignore: Exception) {
            }
        }
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                phoneInfo.web_user_agent = WebSettings.getDefaultUserAgent(ctx)
            }
        } catch (ignore: Exception) {
        }
        val wifiManager =
            ctx.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        if (wifiManager != null) {
            try {
                if (wifiManager.connectionInfo != null) {
                    phoneInfo.wifi_info_get_bssid = wifiManager.connectionInfo.bssid
                }
            } catch (ignore: Exception) {
            }
            try {
//                if (wifiManager.connectionInfo != null) {
//                    phoneInfo.wifi_info_get_mac_address = wifiManager.connectionInfo.macAddress
//                }
                phoneInfo.wifi_info_get_mac_address = ""
            } catch (ignore: Exception) {
            }
            try {
                if (wifiManager.connectionInfo != null) {
                    phoneInfo.wifi_info_get_network_id = wifiManager.connectionInfo.networkId
                }
            } catch (ignore: Exception) {
            }
            try {
//                if (wifiManager.connectionInfo != null) {
//                    phoneInfo.wifi_info_get_ssid = wifiManager.connectionInfo.ssid
//                }
                phoneInfo.wifi_info_get_ssid = ""
            } catch (ignore: Exception) {
            }
        }
        return phoneInfo
    }

    fun getInfoJson(phoneInfo: PhoneInfo): JsonObject {
        val json = JsonObject()
        json.addProperty(GOOGLE_ADID, phoneInfo.gaid)
        json.addProperty(BUILD_ID, phoneInfo.build_id)
        json.addProperty(BUILD_DISPLAY, phoneInfo.build_display)
        json.addProperty(BUILD_INCREMENTAL, phoneInfo.build_incremental)
        json.addProperty(BUILD_SDK_INT, phoneInfo.build_sdk_int.toString() + "")
        json.addProperty(BUILD_CODENAME, phoneInfo.build_codename)
        json.addProperty(BUILD_RELEASE, phoneInfo.build_release)
        json.addProperty(BUILD_TYPE, phoneInfo.build_type)
        json.addProperty(BUILD_USER, phoneInfo.build_user)
        json.addProperty(BUILD_HOST, phoneInfo.build_host)
        json.addProperty(BUILD_TAGS, phoneInfo.build_tags)
        json.addProperty(BUILD_MODEL, phoneInfo.build_model)
        json.addProperty(BUILD_BRAND, phoneInfo.build_brand)
        json.addProperty(BUILD_PRODUCT, phoneInfo.build_product)
        json.addProperty(BUILD_DEVICE, phoneInfo.build_device)
        json.addProperty(BUILD_BOARD, phoneInfo.build_board)
        json.addProperty(BUILD_CPU_ABI, phoneInfo.build_cpu_abi)
        json.addProperty(BUILD_CPU_ABI2, phoneInfo.build_cpu_abi2)
        json.addProperty(BUILD_MANUFACTURER, phoneInfo.build_manufacturer)
        json.addProperty(BUILD_FINGERPRINT, phoneInfo.build_fingerprint)
        json.addProperty(BUILD_SERIAL, phoneInfo.build_serial)
        json.addProperty(BUILD_HARDWARE, phoneInfo.build_hardware)
        json.addProperty(BUILD_BOOTLOADER, phoneInfo.build_bootloader)
        json.addProperty(SETTINGS_SECURE_ANDROID_ID, phoneInfo.settings_secure_android_id)
        json.addProperty(TELE_GETDEVICEID, phoneInfo.tel_get_device_id)
        json.addProperty(TELE_GETDEVICESOFTWAREVERSION, phoneInfo.tel_get_device_software_version)
        json.addProperty(TELE_GETLINE1NUMBER, phoneInfo.tel_get_line1_number)
        json.addProperty(TELE_GETNETWORKCOUNTRYISO, phoneInfo.tel_get_network_country_iso)
        json.addProperty(TELE_GETNETWORKOPERATOR, phoneInfo.tel_get_network_operator)
        json.addProperty(TELE_GETNETWORKOPERATORNAME, phoneInfo.tel_get_network_operator_name)
        json.addProperty(TELE_GETNETWORKTYPE, phoneInfo.tel_get_network_type.toString() + "")
        json.addProperty(TELE_GETPHONETYPE, phoneInfo.tel_get_phone_type.toString() + "")
        json.addProperty(TELE_GETSIMCOUNTRYISO, phoneInfo.tel_get_sim_country_iso)
        json.addProperty(TELE_GETSIMOPERATOR, phoneInfo.tel_get_sim_operator)
        json.addProperty(TELE_GETSIMOPERATORNAME, phoneInfo.tel_get_sim_operator_name)
        json.addProperty(TELE_GETSIMSERIALNUMBER, phoneInfo.tel_get_sim_serial_number)
        json.addProperty(TELE_GETSIMSTATE, phoneInfo.tel_get_sim_state.toString() + "")
        json.addProperty(TELE_GETSUBSCRIBERID, phoneInfo.tel_get_subscriber_id + "")
        json.addProperty(WEB_USERAGENT, phoneInfo.web_user_agent + "")
        json.addProperty(WIFIINFO_GETBSSID, phoneInfo.wifi_info_get_bssid + "")
        json.addProperty(WIFIINFO_GETMACADDRESS, phoneInfo.wifi_info_get_mac_address + "")
        json.addProperty(WIFIINFO_GETNETWORKID, phoneInfo.wifi_info_get_network_id.toString() + "")
        json.addProperty(WIFIINFO_GETSSID, phoneInfo.wifi_info_get_ssid + "")
        return json
    }

    class PhoneInfo {
        //google ad id
        var gaid: String? = null

        //build id
        var build_id: String? = null

        //build_display
        var build_display: String? = null
        var build_incremental: String? = null
        var build_sdk_int = 0
        var build_codename: String? = null
        var build_release: String? = null
        var build_type: String? = null
        var build_user: String? = null
        var build_host: String? = null
        var build_tags: String? = null
        var build_model: String? = null
        var build_brand: String? = null
        var build_product: String? = null
        var build_device: String? = null
        var build_board: String? = null
        var build_cpu_abi: String? = null
        var build_cpu_abi2: String? = null
        var build_manufacturer: String? = null
        var build_fingerprint: String? = null
        var build_serial: String? = null
        var build_hardware: String? = null
        var build_bootloader: String? = null
        var settings_secure_android_id: String? = null

        //高版本获取不到，低版本不确定
        var tel_get_device_id: String? = null

        //需要权限，读取手机状态
        //android.Manifest.permission.READ_PHONE_STATE
        var tel_get_device_software_version: String? = null

        //需要权限，读取手机状态
        //android.Manifest.permission.READ_PHONE_STATE
        var tel_get_line1_number: String? = null
        var tel_get_network_country_iso: String? = null
        var tel_get_network_operator: String? = null
        var tel_get_network_operator_name: String? = null

        //需要权限，读取手机状态
        //android.Manifest.permission.READ_PHONE_STATE
        var tel_get_network_type = 0
        var tel_get_phone_type = 0
        var tel_get_sim_country_iso: String? = null
        var tel_get_sim_operator: String? = null
        var tel_get_sim_operator_name: String? = null

        //高版本获取不到，低版本不确定
        var tel_get_sim_serial_number: String? = null
        var tel_get_sim_state = 0

        //高版本获取不到，低版本不确定
        var tel_get_subscriber_id: String? = null
        var web_user_agent: String? = null

        //部分手机需要定位权限, wifi mac
        var wifi_info_get_bssid: String? = null

        //需要权限，定位
        var wifi_info_get_mac_address: String? = null
        var wifi_info_get_network_id = 0

        //需要权限，包括定位，wifi状态读取等权限
        var wifi_info_get_ssid: String? = null
        fun log() {
            println(
                """
                    xx - 1 = $gaid
                    xx - 2 = $build_id
                    xx - 3 = $build_display
                    xx - 4 = $build_incremental
                    xx - 5 = $build_sdk_int
                    xx - 6 = $build_codename
                    xx - 7 = $build_release
                    xx - 8 = $build_type
                    xx - 9 = $build_user
                    xx - 10 = $build_host
                    xx - 11 = $build_tags
                    xx - 12 = $build_model
                    xx - 13 = $build_brand
                    xx - 14 = $build_product
                    xx - 15 = $build_device
                    xx - 16 = $build_board
                    xx - 17 = $build_cpu_abi
                    xx - 18 = $build_cpu_abi2
                    xx - 19 = $build_manufacturer
                    xx - 20 = $build_fingerprint
                    xx - 21 = $build_serial
                    xx - 22 = $build_hardware
                    xx - 23 = $build_bootloader
                    xx - 24 = $settings_secure_android_id
                    xx - 25 = $tel_get_device_id
                    xx - 26 = $tel_get_device_software_version
                    xx - 27 = $tel_get_line1_number
                    xx - 28 = $tel_get_network_country_iso
                    xx - 29 = $tel_get_network_operator
                    xx - 30 = $tel_get_network_operator_name
                    xx - 31 = $tel_get_network_type
                    xx - 32 = $tel_get_sim_country_iso
                    xx - 33 = $tel_get_sim_operator
                    xx - 34 = $tel_get_sim_operator_name
                    xx - 35 = $tel_get_sim_serial_number
                    xx - 36 = $tel_get_sim_state
                    xx - 37 = $tel_get_subscriber_id
                    xx - 38 = $web_user_agent
                    xx - 39 = $wifi_info_get_bssid
                    xx - 40 = $wifi_info_get_mac_address
                    xx - 41 = $wifi_info_get_network_id
                    xx - 42 = $wifi_info_get_ssid
                    
                    """.trimIndent()
            )
        }
    }
}