package com.duben.library.utils;

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

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：ConstantUtil
 * 作者：孟崔广
 * 时间：2018/1/10 17:51
 * 邮箱：mengcuiguang@cashbang.cn
 */
public class ConstantUtil {

    /**
     * 获取App安装包信息
     *
     * @return
     */
    public static PackageInfo getPackageInfo(Context context) {
        PackageInfo info = null;
        try {
            info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace(System.err);
        }
        if (info == null)
            info = new PackageInfo();
        return info;
    }

    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 判断是否安装了packageNameString
     *
     * @param
     * @return
     */
    public static boolean isInstall(Context context, String packageNameString) {
        boolean flag = false;
        if (context == null || TextUtils.isEmpty(packageNameString)) {
            return flag;
        }
        PackageManager manager = context.getPackageManager();
        List<PackageInfo> allApps = manager.getInstalledPackages(0);
        for (int i = 0; i < allApps.size(); i++) {
            String packageName = allApps.get(i).packageName;
            if (packageName.equals(packageNameString)) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * Andorid检测支付宝客户端是否安装
     *
     * @param context ture 为已安装
     * @return
     */
    public static boolean checkAliPayInstalled(Context context) {
        boolean isAlipayInstall = false;
        if (isInstall(context, "com.eg.android.AlipayGphone")) {
            Uri uri = Uri.parse("alipays://platformapi/startApp");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            ComponentName componentName = intent.resolveActivity(context.getPackageManager());
            isAlipayInstall = componentName != null;
        }
        return isAlipayInstall;
    }

    /**
     * 获取geturl的参数
     *
     * @param url
     * @return
     */
    public static Map<String, String> getUrlParameter(String url) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            url = URLDecoder.decode(url, "utf-8");
            if (url.contains("?")) {
                String substring = url.substring(url.indexOf("?") + 1, url.length());
                substring = URLDecoder.decode(substring, "utf-8");
                if (substring.contains("&")) {
                    String[] split = substring.split("&");
                    for (int i = 0; i < split.length; i++) {
                        String[] split2 = split[i].split("=");
                        if (split2 != null && split2.length == 2)
                            map.put(split2[0], split2[1]);
                    }
                } else {
                    String[] split2 = substring.split("=");
                    if (split2 != null && split2.length == 2)
                        map.put(split2[0], split2[1]);
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 根据key 获取URL参数值
     *
     * @param url
     * @param key
     * @return
     */
    public static String getUrlParameter(String url, String key) {
        String value = "";
        try {
            url = URLDecoder.decode(url, "utf-8");
            if (url.contains("?")) {
                String substring = url.substring(url.indexOf("?") + 1, url.length());
                substring = URLDecoder.decode(substring, "utf-8");
                if (substring.contains("&")) {
                    String[] split = substring.split("&");
                    for (int i = 0; i < split.length; i++) {
                        String[] split2 = split[i].split(key);
                        if (split2.length == 2)
                            value = split2[1];
                    }
                } else {
                    String[] split2 = substring.split(key);
                    if (split2.length == 2)
                        value = split2[1];
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return value;
    }

}
