package com.unit.websdk;


import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class WebViewUtil {
    private static final String TAG = "WebViewUtil";
    /**
     * 外跳浏览器方法
     * @param context
     * @param url
     */
    public static void openBrowser(Context context, String url) {
        if (isAppInstalled(context, "com.android.chrome")) {
            // 创建一个 Intent，指定 ACTION_VIEW 动作和 URL
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 指定要使用的浏览器的包名
            intent.setPackage("com.android.chrome"); // Chrome 浏览器的包名
            // 启动 Chrome 浏览器来处理链接
            context.startActivity(intent);
        } else {
            // 如果没有安装 Chrome 浏览器，使用系统默认浏览器打开链接
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 启动默认浏览器来处理链接
            context.startActivity(intent);
        }
    }

    private static final String kGpHttpScheme = "http://play.google.com/store/";
    private static final String kGpHttpsScheme = "https://play.google.com/store/";
    private static final String kMarketScheme = "market:";
    private static final String kGpMarketIdUrl = "market://details?id=";
    private static final String kGpMarketUrl = "market://details";
    private static final String kGooglePlayUrl = "https://play.google.com/store/apps/details";
    /**
     * WebView对url协议头和外跳浏览器标识参数的支持
     * @param context
     * @param url
     * @return
     */
    public static boolean openIntent(Context context, String url) {
        if (url.startsWith(kMarketScheme)
                || url.startsWith(kGpHttpsScheme)
                || url.startsWith(kGpHttpScheme)) {
            // market链接
            Intent intent = new Intent(Intent.ACTION_VIEW);
            if (url.startsWith(kGpMarketIdUrl)) {
                final String replace = url.replace(kGpMarketUrl, kGooglePlayUrl);
                Log.d(TAG, "marketUrl replace: \n" + url + "\n" + replace);
                intent.setData(Uri.parse(replace));
            } else {
                intent.setData(Uri.parse(url));
            }
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            return true;
        } else if (!url.startsWith("http://") && !url.startsWith("https://")) {
            //其它协议链接打开
            openUrl(context, url);
            return true;
        } else if (url.contains("lz_open_browser=1")) {
            //lz_open_browser=1参数是外跳浏览器的标识
            openBrowser(context, url);
            return true;
        }
        return false;
    }


    /**
     * 检查包是否安装
     * @param context
     * @param packageName
     * @return
     */
    private static boolean isAppInstalled(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        try {
            context.getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    /**
     * 其它协议链接打开
     * @param context
     * @param url
     */
    private static void openUrl(Context context, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        ActivityInfo activityInfo = intent.resolveActivityInfo(context.getPackageManager(), 0);
        if (activityInfo.exported) {
            intent.setData(Uri.parse(url));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }
}
