package com.tcl.ah.tvcontrol.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.ResolveInfo;
import android.util.Log;

import com.tcl.ah.tvcontrol.MyApplication;

import java.util.List;

public class PackageHelper {

    public static String TAG = "DEBUG_TS";

    public static void startActivityByPackage(String packageName) {
        try {

            String activityName = getActivityNameFromPackage(MyApplication.getAppContext(), packageName);
            Log.d(TAG, "this is activityName:" + activityName);
            if (activityName == null) {
                if (packageName.contains("/")) {
                    String[] ret = packageName.split("/");
                    //Log.d(TAG, "this is activityName:" + ret[0] + "=" + ret[1]);
                    startActivityByComponent(ret[0], ret[1]);
                }
                return;
            }
            startActivityByComponent(packageName, activityName);
        } catch (Exception e) {
            Log.e("DEBUG_TS", "this is startActivityByPackage:" + e.toString());
        }


    }

    private static void startActivityByComponent(String packageName, String activityName) {
        ComponentName cn = new ComponentName(packageName, activityName);
        Intent mIntent = new Intent();
        mIntent.setComponent(cn);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        MyApplication.getAppContext().startActivity(mIntent);
    }

    private static String getActivityNameFromPackage(Context context, String packageName) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi;
            pi = pm.getPackageInfo(packageName, 0);
            Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
            resolveIntent.setPackage(pi.packageName);
            //resolveIntent.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
            List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
            if (apps != null) {
                Log.d(TAG, "getActivityNameFromPackage: LEANBACK_LAUNCHER: launcherIntent size == " + apps.size());
            }
            if (apps == null || apps.isEmpty()) {
                //resolveIntent.removeCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
                resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                apps = pm.queryIntentActivities(resolveIntent, 0);
                Log.d(TAG, "getActivityNameFromPackage: LAUNCHER: launcherIntent size == " + apps.size());
            }
            if (apps != null && apps.size() > 0) {
                ResolveInfo info = apps.iterator().next();
                if (info != null) {
                    return info.activityInfo.name;
                }
            } else {
                Log.d(TAG, "getActivityNameFromPackage: apps is null");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
