package com.quyunshuo.module.home.utils

import android.Manifest
import android.app.Activity
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import com.igexin.sdk.PushManager
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
import com.quyunshuo.module.home.getui.MyIntentService
import com.quyunshuo.module.home.getui.MyPushService
import java.io.File

class GeTuiUtil {
    private var activity: Activity? = null
    val REQUEST_PERMISSION: Int = 0
    private var appkey: String? = ""
    private var appsecret: String? = ""
    private var appid: String? = ""

    constructor(activity: Activity?) {
        this.activity = activity
    }


    /**
     * 个推
     */
    fun geTui() {
        parseManifests()
        val pkgManager: PackageManager = BaseApplication.context.getPackageManager()
        // 读写 sd card 权限非常重要, android6.0默认禁止的, 建议初始化之前就弹窗让用户赋予该权限
        val sdCardWritePermission =
            pkgManager.checkPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                BaseApplication.context.getPackageName()
            ) == PackageManager.PERMISSION_GRANTED

        // read phone state用于获取 imei 设备信息
        val phoneSatePermission =
            pkgManager.checkPermission(
                Manifest.permission.READ_PHONE_STATE,
                BaseApplication.context.getPackageName()
            ) == PackageManager.PERMISSION_GRANTED

        // read phone state用于获取 imei 设备信息
        val fineLocationPermission =
            pkgManager.checkPermission(
                Manifest.permission.ACCESS_FINE_LOCATION,
                BaseApplication.context.getPackageName()
            ) == PackageManager.PERMISSION_GRANTED

        val coarseLocationPermission =
            pkgManager.checkPermission(
                Manifest.permission.ACCESS_COARSE_LOCATION,
                BaseApplication.context.getPackageName()
            ) == PackageManager.PERMISSION_GRANTED

        val smsPermission =
            pkgManager.checkPermission(
                Manifest.permission.READ_SMS,
                BaseApplication.context.getPackageName()
            ) == PackageManager.PERMISSION_GRANTED
        val phoneNumbersPermission =
            pkgManager.checkPermission(
                Manifest.permission.READ_SMS,
                BaseApplication.context.getPackageName()
            ) == PackageManager.PERMISSION_GRANTED

        if (Build.VERSION.SDK_INT >= 23 && !sdCardWritePermission || !phoneSatePermission ||!fineLocationPermission || !coarseLocationPermission||!smsPermission||!phoneNumbersPermission) {
            requestPermission()
        } else {
            PushManager.getInstance().initialize(
                BaseApplication.context.getApplicationContext(),
                MyPushService::class.java
            )
        }

        // 注册 intentService 后 PushDemoReceiver 无效, sdk 会使用 DemoIntentService 传递数据,
        // AndroidManifest 对应保留一个即可(如果注册 DemoIntentService, 可以去掉 PushDemoReceiver, 如果注册了
        // IntentService, 必须在 AndroidManifest 中声明)
        PushManager.getInstance().registerPushIntentService(
            BaseApplication.context.getApplicationContext(),
            MyIntentService::class.java
        )

        // cpu 架构
//        com.hboxs.base_library.util.LogUtil.d(TAG, "cpu arch = " + (Build.VERSION.SDK_INT < 21 ? Build.CPU_ABI : Build.SUPPORTED_ABIS[0]));

        // 检查 so 是否存在
        val file: File = File(
            BaseApplication.context
                .getApplicationInfo().nativeLibraryDir + File.separator + "libgetuiext2.so"
        )
//        LogUtil.e(TAG, "libgetuiext2.so exist = " + file.exists());
    }

    /**
     * 个推
     * 获取key等数据
     */
    private fun parseManifests() {
        val packageName: String = BaseApplication.context.getPackageName()
        try {
            val appInfo: ApplicationInfo = BaseApplication.context.getPackageManager()
                .getApplicationInfo(packageName, PackageManager.GET_META_DATA)
            if (appInfo.metaData != null) {
                appid = appInfo.metaData.getString("PUSH_APPID")
                appsecret = appInfo.metaData.getString("PUSH_APPSECRET")
                appkey = appInfo.metaData.getString("PUSH_APPKEY")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 个推
     */
    private fun requestPermission() {
        ActivityCompat.requestPermissions(
            activity!!,
            arrayOf<String>(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_SMS,
                Manifest.permission.READ_PHONE_NUMBERS
            ),
            REQUEST_PERMISSION
        )
    }

}