package com.android.launcher3.util;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.android.launcher3.LockScreenActivity;
import com.android.launcher3.manager.ColumnDef;
import com.android.launcher3.manager.LauncherManager;
import com.android.launcher3.model.ConfigManager;

import java.util.ArrayList;
import java.util.List;

public class ActivityUtil {
    private static ActivityUtil mInstance;


    private List<Activity> activityList;

    public static ActivityUtil getInstance() {
        if (mInstance == null) {
            mInstance = new ActivityUtil();
        }
        return mInstance;
    }


    public void init(Application application) {
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
//                boolean isLock = activity instanceof LockScreenActivity;
//                if (!isLock && !ConfigManager.getInstance().isLockScreen()) {
//                    if (LauncherManager.getInstance().getLockScreenMode() != ColumnDef.LockScreen.MODE_UNLOCK) {
//
//                        if (LauncherManager.getInstance().isForceLock()) {
//                            LauncherManager.getInstance().relockScreen();
//                        }
//                    }
//                }

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

//                if (LauncherManager.getInstance().isForceLock()&&!LauncherManager.getInstance().isSafePackageName(activity.getPackageName())) {
//                    LauncherManager.getInstance().lockScreen(application, LauncherManager.getInstance().getModeDesc1(),LauncherManager.getInstance().getModeDesc2(),LauncherManager.getInstance().getLockScreenMode());
//                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
//                if (LauncherManager.getInstance().isForceLock()&&!LauncherManager.getInstance().isSafePackageName(activity.getPackageName())) {
//                    LauncherManager.getInstance().lockScreen(application, LauncherManager.getInstance().getModeDesc1(),LauncherManager.getInstance().getModeDesc2(),LauncherManager.getInstance().getLockScreenMode());
//                }
                remove(activity);
            }
        });
    }

    private ActivityUtil() {
        activityList = new ArrayList<>();
    }


    public void addActivity(Activity activity) {

        if (activityList.contains(activity)) {
            activityList.set(0, activity);
        } else {
            activityList.add(0, activity);
        }
    }

    public void remove(Activity activity) {
        if (activityList.contains(activity)) {
            activityList.remove(activity);
        }
    }

    public Activity getTopActivity() {
        return activityList.size() > 0 ? activityList.get(0) : null;
    }

}
