package com.ycgis.macall.personalcenter.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import java.io.File;

/**
 * created by:   Macall
 * create time:  2023/2/2 15:33
 * copyright:    @ruansee.com
 * Describe:    安装应用
 */
public class InsertAppUtils {

    /**
     * 检测是否安装应用
     * @param context 上下文环境
     * @return  true 未安装     false 安装
     */
    public boolean isInspectStaticFaceSDK(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo("com.ruansee.unifiedservicecomponent", 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        return packageInfo == null;
    }

    /**
     * 检测本地安装的版本是否需要更是
     * @param context   上下文环境
     * @return  true 不需要更新    false 需要安装
     */
    public boolean isUpdateStaticFaceSDK(Context context) {
        PackageInfo packageInfo = null;
        try {
            //读取本地组件版本号，版本号
            packageInfo = context.getPackageManager().getPackageInfo("com.ruansee.unifiedservicecomponent", 0);
            if (packageInfo == null) return false;
            //这是新版本的 版本号
            int nweCode = 2;
            // 满足条件说明 本地安装的是最新版 返回true
            return packageInfo.versionCode>=nweCode;
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
            return true;
        }
    }

    //申请应用安装权限后安装应用
    public void installApp(String titleMessage, Activity context, File file, DialogInterface.OnClickListener cancelClickListener) throws Exception {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            boolean isGranted = context.getPackageManager().canRequestPackageInstalls();
            if (!isGranted) {
                applyInstallPermission(context,titleMessage, cancelClickListener);
            } else {
                instead(context, file);
            }
        } else {
            instead(context, file);
        }
    }

    //申请应用安装权限
    public void applyInstallPermission(Activity context, String titleMessage,DialogInterface.OnClickListener cancelClickListener) {
        new AlertDialog.Builder(context)
                .setTitle("重要提示")
                .setMessage(titleMessage)
                .setCancelable(false)
                .setNegativeButton("取消", cancelClickListener)
                .setPositiveButton("继续", (dialog, which) -> {
                    dialog.dismiss();
                    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                    context.startActivityForResult(intent, 1023);
                }).create().show();
    }


    private void instead(Activity context, File file) throws Exception {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri apkUri = UriUtils.getUri(context, file);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.startActivityForResult(intent, 10145);
        } else {
            context.startActivityForResult(intent, 10145);
        }
    }
}
