package com.itant.shiwushu.manager;

import android.app.Activity;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Jason on 2018/8/26.
 */

public class ActivityManager {
    private static ActivityManager activityManager;
    private Set<Activity> activitySet;

    private boolean isLoginExist;

    private ActivityManager() {
        activitySet = new HashSet<>();
    }

    public static ActivityManager getInstance() {
        if (activityManager == null) {
            initManager();
        }

        return activityManager;
    }

    private static synchronized void initManager() {
        if (activityManager == null) {
            activityManager = new ActivityManager();
        }
    }

    public void addActivity(Activity activity) {
        if (!activitySet.contains(activity)) {
            activitySet.add(activity);
        }
    }

    public void removeActivity(Activity activity) {
        activitySet.remove(activity);
    }

    public void clearActivity() {
        if (activitySet == null || activitySet.size() == 0) {
            return;
        }
        for (Activity activity : activitySet) {
            activity.finish();
        }
        activitySet.clear();
    }

    public void clearActivityExcept(Activity except) {
        if (activitySet == null || activitySet.size() == 0) {
            return;
        }
        for (Activity activity : activitySet) {
            if (activity != except) {
                activity.finish();
            }
        }
    }

    public void clearActivityExcept(Class except) {
        if (activitySet == null || activitySet.size() == 0) {
            return;
        }
        for (Activity activity : activitySet) {
            if (activity.getClass() != except) {
                activity.finish();
            }
        }
    }

    public boolean isLoginExist() {
        return isLoginExist;
    }

    public void setLoginExist(boolean loginExist) {
        isLoginExist = loginExist;
    }
}
