package cc.winboll.studio.contacts;

import android.app.Activity;
import cc.winboll.studio.libappbase.LogUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
 * @Date 2025/02/13 06:58:04
 * @Describe Activity 栈管理工具，用于统一管理应用内 Activity 生命周期
 */
public class ActivityStack {
    // ====================== 常量定义区 ======================
    public static final String TAG = "ActivityStack";

    // ====================== 单例与成员变量区 ======================
    private static final ActivityStack INSTANCE = new ActivityStack();
    private List<Activity> mActivityList = new ArrayList<Activity>();

    // ====================== 单例获取方法区 ======================
    public static ActivityStack getInstance() {
        return INSTANCE;
    }

    // ====================== 私有构造函数（防止外部实例化） ======================
    private ActivityStack() {
        LogUtils.d(TAG, "ActivityStack: 初始化 Activity 栈管理工具");
    }

    // ====================== Activity 栈操作方法区 ======================
    /**
     * 添加 Activity 到栈中
     */
    public void addActivity(Activity activity) {
        if (activity == null) {
            LogUtils.w(TAG, "addActivity: 待添加的 Activity 为 null，跳过添加");
            return;
        }
        mActivityList.add(activity);
        LogUtils.d(TAG, "addActivity: Activity入栈 | 类名=" + activity.getClass().getSimpleName() + " | 栈大小=" + mActivityList.size());
    }

    /**
     * 获取栈顶 Activity
     */
    public Activity getTopActivity() {
        if (mActivityList.isEmpty()) {
            LogUtils.w(TAG, "getTopActivity: Activity 栈为空，返回 null");
            return null;
        }
        Activity topActivity = mActivityList.get(mActivityList.size() - 1);
        LogUtils.d(TAG, "getTopActivity: 获取栈顶 Activity | 类名=" + topActivity.getClass().getSimpleName());
        return topActivity;
    }

    /**
     * 移除并销毁栈顶 Activity
     */
    public void finishTopActivity() {
        if (mActivityList.isEmpty()) {
            LogUtils.w(TAG, "finishTopActivity: Activity 栈为空，无需操作");
            return;
        }
        Activity topActivity = mActivityList.remove(mActivityList.size() - 1);
        topActivity.finish();
        LogUtils.d(TAG, "finishTopActivity: 销毁栈顶 Activity | 类名=" + topActivity.getClass().getSimpleName() + " | 栈大小=" + mActivityList.size());
    }

    /**
     * 移除并销毁指定 Activity
     */
    public void finishActivity(Activity activity) {
        if (activity == null) {
            LogUtils.w(TAG, "finishActivity: 待销毁的 Activity 为 null，跳过操作");
            return;
        }
        if (mActivityList.remove(activity)) {
            activity.finish();
            LogUtils.d(TAG, "finishActivity: 销毁指定 Activity | 类名=" + activity.getClass().getSimpleName() + " | 栈大小=" + mActivityList.size());
        } else {
            LogUtils.w(TAG, "finishActivity: 指定 Activity 不在栈中 | 类名=" + activity.getClass().getSimpleName());
        }
    }

    /**
     * 移除并销毁指定类的所有 Activity
     */
    public void finishActivity(Class<?> activityClass) {
        if (activityClass == null) {
            LogUtils.w(TAG, "finishActivity: 待销毁的 Activity 类为 null，跳过操作");
            return;
        }
        // Java7 兼容：使用 Iterator 遍历避免 ConcurrentModificationException
        Iterator<Activity> iterator = mActivityList.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity.getClass().equals(activityClass)) {
                iterator.remove();
                activity.finish();
                LogUtils.d(TAG, "finishActivity: 销毁指定类 Activity | 类名=" + activityClass.getSimpleName() + " | 栈大小=" + mActivityList.size());
            }
        }
    }

    /**
     * 销毁栈中所有 Activity
     */
    public void finishAllActivity() {
        if (mActivityList.isEmpty()) {
            LogUtils.w(TAG, "finishAllActivity: Activity 栈为空，无需操作");
            return;
        }
        // Java7 兼容：使用增强 for 循环遍历销毁，避免迭代器异常
        for (Activity activity : mActivityList) {
            if (!activity.isFinishing()) {
                activity.finish();
                LogUtils.d(TAG, "finishAllActivity: 销毁 Activity | 类名=" + activity.getClass().getSimpleName());
            }
        }
        mActivityList.clear();
        LogUtils.d(TAG, "finishAllActivity: 所有 Activity 已销毁，栈已清空");
    }

    /**
     * 新增：移除指定Activity但不销毁（用于Activity正常退出）
     */
    public void removeActivity(Activity activity) {
        if (activity == null) {
            LogUtils.w(TAG, "removeActivity: 待移除的 Activity 为 null，跳过操作");
            return;
        }
        if (mActivityList.remove(activity)) {
            LogUtils.d(TAG, "removeActivity: 移除 Activity | 类名=" + activity.getClass().getSimpleName() + " | 栈大小=" + mActivityList.size());
        }
    }
}

