package com.seer.teach.teacher.common.utils;

import com.seer.teach.teacher.common.dto.LessonContextDTO;
import com.seer.teach.teacher.common.dto.ReadingStatusDTO;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户状态缓存工具类
 * 管理用户的课程上下文(LessonContextDTO)和阅读状态(ReadingStatusDTO)
 */
public class UserStatusCacheUtils {

    /**
     * 状态类型枚举
     */
    private enum StatusType {
        LEARNING,   // 使用 LessonContextDTO
        READING
    }

    /**
     * 用户状态包装类
     */
    private static class UserStateWrapper {
        private final StatusType type;
        private final Object data;

        public UserStateWrapper(StatusType type, Object data) {
            this.type = type;
            this.data = data;
        }

        public StatusType getType() {
            return type;
        }

        public Object getData() {
            return data;
        }
    }

    // 用户状态缓存
    private static final ConcurrentHashMap<Integer, UserStateWrapper> CACHE = new ConcurrentHashMap<>();

    /**
     * 设置用户学习状态（课程上下文）
     *
     * @param userId 用户ID
     * @param context LessonContextDTO
     */
    public static void setLearning(Integer userId, LessonContextDTO context) {
        CACHE.put(userId, new UserStateWrapper(StatusType.LEARNING, context));
    }

    /**
     * 获取用户学习状态（课程上下文）
     *
     * @param userId 用户ID
     * @return LessonContextDTO 或 null
     */
    public static LessonContextDTO getLearning(Integer userId) {
        UserStateWrapper wrapper = CACHE.get(userId);
        if (wrapper != null && wrapper.getType() == StatusType.LEARNING) {
            return (LessonContextDTO) wrapper.getData();
        }
        return null;
    }

    /**
     * 删除并返回用户学习状态
     */
    public static LessonContextDTO deleteLearning(Integer userId) {
        UserStateWrapper wrapper = CACHE.get(userId);
        if (wrapper != null && wrapper.getType() == StatusType.LEARNING) {
            CACHE.remove(userId);
            return (LessonContextDTO) wrapper.getData();
        }
        return null;
    }


    public static void setReading(Integer userId, ReadingStatusDTO dto) {
        CACHE.put(userId, new UserStateWrapper(StatusType.READING, dto));
    }

    public static ReadingStatusDTO getReading(Integer userId) {
        UserStateWrapper wrapper = CACHE.get(userId);
        if (wrapper != null && wrapper.getType() == StatusType.READING) {
            return (ReadingStatusDTO) wrapper.getData();
        }
        return null;
    }

    public static ReadingStatusDTO deleteReading(Integer userId) {
        UserStateWrapper wrapper = CACHE.get(userId);
        if (wrapper != null && wrapper.getType() == StatusType.READING) {
            CACHE.remove(userId);
            return (ReadingStatusDTO) wrapper.getData();
        }
        return null;
    }


    public static void delete(Integer userId) {
        CACHE.remove(userId);
    }

    public static boolean isLearning(Integer userId) {
        UserStateWrapper wrapper = CACHE.get(userId);
        return wrapper != null && wrapper.getType() == StatusType.LEARNING;
    }

    public static boolean isReading(Integer userId) {
        UserStateWrapper wrapper = CACHE.get(userId);
        return wrapper != null && wrapper.getType() == StatusType.READING;
    }

    public static void clearAll() {
        CACHE.clear();
    }

    public static void printAll() {
        if (CACHE.isEmpty()) {
            System.out.println("[UserStatusCache] 当前缓存为空");
            return;
        }

        System.out.println("=========== UserStatusCache 当前内容 ===========");

        CACHE.forEach((userId, wrapper) -> {
            System.out.println(
                    "userId: " + userId + "\n" +
                            "状态  : " + wrapper.getType() + "\n" +
                            "数据  : " + wrapper.getData() + "\n" +
                            "-----------------------------------------------"
            );
        });

        System.out.println("=============== 打印结束 ===============");
    }
}
