package com.corewing.app.common.page;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.HashMap;
import java.util.Map;

public class PageContext {
    private static final ThreadLocal<PagePool> PAGE_POOL_HOLDER = new ThreadLocal<>();

    private static class PagePool {
        private final Map<String, Page<?>> pool = new HashMap<>();
        private int counter = 0;

        public synchronized String generateKey() {
            return "page_" + (counter++);
        }

        public Page<?> get(String key) {
            return pool.get(key);
        }

        public void put(String key, Page<?> page) {
            pool.put(key, page);
        }

        public void remove(String key) {
            pool.remove(key);
        }

        public void clear() {
            pool.clear();
            counter = 0;
        }

        public boolean isEmpty() {
            return pool.isEmpty();
        }
    }

    /**
     * 获取一个新的 Page 对象（带唯一key）
     * 适合单个查询场景
     */
    public static <T> Page<T> getPage(Class<T> clazz) {
        PagePool pool = getPagePool();
        String key = pool.generateKey();
        Page<T> page = new Page<>();
        pool.put(key, page);
        return page;
    }

    /**
     * 获取带有默认分页参数的 Page 对象
     */
    public static <T> Page<T> getPage(Class<T> clazz, Integer current, Integer size) {
        int pageNum = current != null && current > 0 ? current : 1;
        int pageSize = size != null && size > 0 ? size : 10;

        PagePool pool = getPagePool();
        String key = pool.generateKey();
        Page<T> page = new Page<>(pageNum, pageSize);
        pool.put(key, page);
        return page;
    }

    /**
     * 注册一个 Page 对象到池中（供 PageInterceptor 使用）
     */
    public static <T> void registerPage(Page<T> page) {
        PagePool pool = getPagePool();
        String key = pool.generateKey();
        pool.put(key, page);
    }

    /**
     * 获取默认的 Page 对象（第一个注册的）
     * 保持向后兼容性
     */
    @SuppressWarnings("unchecked")
    public static <T> Page<T> getDefaultPage(Class<T> clazz) {
        PagePool pool = PAGE_POOL_HOLDER.get();
        if (pool == null || pool.isEmpty()) {
            return null;
        }

        // 返回第一个 Page 对象
        return (Page<T>) pool.pool.values().stream().findFirst().orElse(null);
    }

    /**
     * 通过 key 获取 Page 对象
     */
    @SuppressWarnings("unchecked")
    public static <T> Page<T> getPageByKey(String key) {
        PagePool pool = PAGE_POOL_HOLDER.get();
        if (pool == null) {
            return null;
        }
        return (Page<T>) pool.get(key);
    }

    /**
     * 获取 PagePool
     */
    private static PagePool getPagePool() {
        PagePool pool = PAGE_POOL_HOLDER.get();
        if (pool == null) {
            pool = new PagePool();
            PAGE_POOL_HOLDER.set(pool);
        }
        return pool;
    }

    /**
     * 清除指定 key 的 Page 对象
     */
    public static void clearPage(String key) {
        PagePool pool = PAGE_POOL_HOLDER.get();
        if (pool != null) {
            pool.remove(key);
        }
    }

    /**
     * 清除所有
     */
    public static void clear() {
        PagePool pool = PAGE_POOL_HOLDER.get();
        if (pool != null) {
            pool.clear();
        }
        PAGE_POOL_HOLDER.remove();
    }
}