package com.hh.lib_base.manager;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/**
 * Description: $
 * @author xwh
 * time 2024/8/19
 **/
public class ActivityManager {
    public static Stack<WeakReference<Activity>> activityStack = new Stack<>();

    public static int size() {
        return activityStack.size();
    }
    public static void add(WeakReference<Activity> weakRefActivity) {
        synchronized (ActivityManager.class) {
            activityStack.add(weakRefActivity);
        }
    }

    public static void remove(WeakReference<Activity> weakRefActivity) {
        boolean b = activityStack.remove(weakRefActivity);
    }

    public static Activity getTopActivity() {
        if(activityStack.empty()) return null;
        return activityStack.peek().get();
    }

    public  synchronized static void finishAll() {
        synchronized (ActivityManager.class) {
            if (activityStack != null && !activityStack.isEmpty()) {
                for (WeakReference<Activity> activityWeakReference : activityStack) {
                    Activity activity = activityWeakReference.get();
                    if (activity != null && !activity.isFinishing()) {
                        activity.finish();
                    }
                }
                activityStack.clear();
            }
        }
    }

    public static void keepMainActivity(Class main) {
        synchronized (ActivityManager.class) {
            if (activityStack!=null&&!activityStack.isEmpty()) {
                Iterator<WeakReference<Activity>> iterator = activityStack.iterator();
                while (iterator.hasNext()) {
                    WeakReference<Activity> next = iterator.next();
                    Activity activity = next.get();
                    if (activity != null && activity.getClass() == main) {
                        activity.finish();
                        iterator.remove();
                    }
                }
            }
        }
    }



    /**
     * 是否包含指定activity
     */
    public static boolean containsActivity(@NonNull final Class<?> cls) {
        for (int i = activityStack.size() - 1; i >= 0; --i) {
            WeakReference<Activity> weakReference = activityStack.get(i);
            if (weakReference.get().getClass().getName().equals(cls.getName())) {
                return true;
            }
        }
        return false;
    }

    public static void removeExitsActivity(Activity activity) {
        for (int i = activityStack.size() - 1; i >= 0; --i) {
            WeakReference<Activity> weakReference = activityStack.get(i);
            if (weakReference == null) {
                continue;
            }
            if(weakReference.get()== null){
                return;
            }
            if (weakReference.get().getClass().equals(activity.getClass())) {
                if(activity != weakReference.get()) {
                    weakReference.get().finish();
                    activityStack.remove(weakReference);
                }
            }
        }
    }

    public static void removeActivity(@NonNull final Class<?> cls) {
        for (int i = activityStack.size() - 1; i >= 0; --i) {
            WeakReference<Activity> weakReference = activityStack.get(i);
            if (weakReference.get().getClass().equals(cls)) {
                weakReference.get().finish();
            }
        }
    }

    public static void removeActivity2(Activity activity) {
        for (int i = activityStack.size() - 1; i >= 0; --i) {
            WeakReference<Activity> weakReference = activityStack.get(i);
            if (weakReference.get().getClass().equals(activity.getClass())) {
                activityStack.remove(weakReference);
            }
        }
    }

}
