package com.atmob.voiceai.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;

import com.atmob.common.runtime.ContextUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class AppUtil {

    public static final String GooglePlay = "com.android.vending";

    private AppUtil() {

    }

    public static void launchAppMarketDetail(String appPackageName) {
        if (TextUtils.isEmpty(appPackageName)) {
            return;
        }
        Uri uri = Uri.parse("market://details?id=" + appPackageName);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        String existMarketPackage = getExistMarketPackage();
        if (!TextUtils.isEmpty(existMarketPackage)) {
            intent.setPackage(existMarketPackage);
        }
        try {
            ContextUtil.getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void launchGooglePlayDetail(String appPackageName) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
        intent.setPackage(GooglePlay);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            ContextUtil.getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getExistMarketPackage() {
        List<String> marketList = Arrays.asList(
                "com.tencent.android.qqdownloader", //应用宝
                "com.huawei.appmarket", //华为
                "com.xiaomi.market", //小米
                "com.oppo.market", //OPPO
                "com.bbk.appstore", //VIVO
                "com.oneplus.market", //一加
                "com.qihoo.appstore", //360
                "com.baidu.appsearch", //百度
                GooglePlay
        );
        for (String marketPackageName : marketList) {
            if (isAppExist(marketPackageName)) {
                return marketPackageName;
            }
        }
        return null;
    }

    public static boolean isAppExist(String appPackageName) {
        Context context = ContextUtil.getContext();
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
        List<String> pName = new ArrayList<>();
        if (packageInfoList != null) {
            for (int i = 0; i < packageInfoList.size(); i++) {
                String pf = packageInfoList.get(i).packageName;
                pName.add(pf);
            }
        }
        return pName.contains(appPackageName);
    }

    public static List<ApplicationInfo> get3rdPartyApp() {
        Context context = ContextUtil.getContext();
        PackageManager packageManager = context.getPackageManager();
        List<ApplicationInfo> installedApplications = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
        Iterator<ApplicationInfo> iterator = installedApplications.iterator();
        while (iterator.hasNext()) {
            ApplicationInfo applicationInfo = iterator.next();
            if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                iterator.remove();
            } else if (Objects.equals(applicationInfo.packageName, context.getPackageName())) {
                iterator.remove();
            }
        }
        return installedApplications;
    }
}
