package com.remair.util;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.Window;
import android.view.WindowManager;
import com.remair.log.LogUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/9/23
 *     desc  : Activity相关工具类
 * </pre>
 */
public class ActivityUtils {

    private ActivityUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }


    /**
     * 判断是否存在Activity
     *
     * @param packageName 包名
     * @param className activity全路径类名
     * @return {@code true}: 是<br>{@code false}: 否
     */
    public static boolean isActivityExists(String packageName, String className) {
        Intent intent = new Intent();
        intent.setClassName(packageName, className);
        PackageManager packageManager = ContextUtils.getContext()
                                                    .getPackageManager();
        return !(packageManager.resolveActivity(intent, 0) == null ||
                         intent.resolveActivity(packageManager) == null ||
                         packageManager.queryIntentActivities(intent, 0)
                                       .isEmpty());
    }


    /**
     * 打开Activity
     *
     * @param packageName 包名
     * @param className 全类名
     */
    public static void launchActivity(String packageName, String className) {
        launchActivity(packageName, className, null);
    }


    /**
     * 打开Activity
     *
     * @param packageName 包名
     * @param className 全类名
     * @param bundle bundle
     */
    public static void launchActivity(String packageName, String className, Bundle bundle) {
        ContextUtils.getContext().startActivity(IntentUtils
                .getComponentIntent(packageName, className, bundle));
    }


    /**
     * 获取launcher activity
     *
     * @param packageName 包名
     * @return launcher activity
     */
    public static String getLauncherActivity(String packageName) {
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PackageManager pm = ContextUtils.getContext().getPackageManager();
        List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo info : infos) {
            if (info.activityInfo.packageName.equals(packageName)) {
                return info.activityInfo.name;
            }
        }
        return "no " + packageName;
    }


    /**
     * 获取栈顶Activity
     *
     * @return 栈顶Activity
     */
    public static Activity getTopActivity() {
        try {
            Class activityThreadClass = Class
                    .forName("android.app.ActivityThread");
            Object activityThread = activityThreadClass
                    .getMethod("currentActivityThread").invoke(null);
            Field activitiesField = activityThreadClass
                    .getDeclaredField("mActivities");
            activitiesField.setAccessible(true);
            Map activities = null;
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                activities = (HashMap) activitiesField.get(activityThread);
            } else {
                activities = (ArrayMap) activitiesField.get(activityThread);
            }
            for (Object activityRecord : activities.values()) {
                Class activityRecordClass = activityRecord.getClass();
                Field pausedField = activityRecordClass
                        .getDeclaredField("paused");
                pausedField.setAccessible(true);
                if (!pausedField.getBoolean(activityRecord)) {
                    Field activityField = activityRecordClass
                            .getDeclaredField("activity");
                    activityField.setAccessible(true);
                    return (Activity) activityField.get(activityRecord);
                }
            }
        } catch (Exception e) {
            LogUtils.e(e);
        }
        return null;
    }


    /**
     * @return 判断Activity是否全屏
     */
    public static boolean isFullScreen(Activity activity) {
        return isFullScreen(activity.getWindow());
    }


    public static boolean isFullScreen(Window window) {
        int flag = window.getAttributes().flags;
        return (flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) ==
                WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }
}