package com.sunzee.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

import com.igexin.sdk.PushManager;
import com.sunzee.base.BaseApplication;
import com.sunzee.service.MyIntentService;
import com.sunzee.service.MyPushService;

import java.io.File;

/**
 * 个推工具类
 */
public class GeTuiUtil {

    private Activity activity;
    public static final int REQUEST_PERMISSION = 0;
    private String appkey = "";
    private String appsecret = "";
    private String appid = "";

    public GeTuiUtil(Activity activity) {
        this.activity = activity;

    }

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

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

        if (Build.VERSION.SDK_INT >= 23 && !sdCardWritePermission || !phoneSatePermission) {
            requestPermission();
        } else {
            PushManager.getInstance().initialize(BaseApplication.getContext().getApplicationContext(), MyPushService.class);
        }

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

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

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

    /**
     * 个推
     * 获取key等数据
     */
    private void parseManifests() {
        String packageName = BaseApplication.getContext().getPackageName();
        try {
            ApplicationInfo appInfo = BaseApplication.getContext().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 (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 个推
     */
    private void requestPermission() {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE},
                REQUEST_PERMISSION);
    }


}
