package jnpf.model.cultivate;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 培训分页入参
 *
 * @author fantaibao
 * @date 2023/12/20
 */
@Data
public class CultivatePage {


    /**
     * 每页显示条数，默认 20
     */
    private long pageSize = 20;
    /**
     * 当前页
     */
    private long currentPage = 1;

    /**
     * 获取分页对象
     * @param <T>
     * @return 分页对象
     */
    public <T> Page<T> coverCultivatePage(String... orders) {
        Page<T> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        if (orders != null && orders.length > 0) {
            page.setOrders(OrderItem.descs(orders));
        }
        return page;
    }

    /**
     * 转换分页list
     * @param page 要转换的Page对象
     * @return 转换后的 PageListVO 对象
     * @throws IllegalArgumentException 如果page参数为null
     */
    public static <T> PageListVO<T> coverPageList(Page<T> page) {
        // 检查参数是否为 null
        if (page == null) {
            throw new IllegalArgumentException("Page object cannot be null");
        }

        PageListVO<T> listVO = new PageListVO<>();
        PaginationVO pagination = new PaginationVO();
        pagination.setCurrentPage(page.getCurrent());
        pagination.setPageSize(page.getSize());
        // 调用者应当处理返回值超出int范围时的异常情况
        long total = page.getTotal();
        if (CollUtil.isEmpty(page.getRecords()) &&
                (page.getSize() != -1 && total == 0)) {
            pagination.setTotal(0);
            listVO.setList(null);
            listVO.setPagination(pagination);
            return listVO;
        }
        if(page.getSize() == -1){
            page.setTotal(page.getRecords().size());
            listVO.setList(page.getRecords());
            return listVO;
        }
        pagination.setTotal(Math.toIntExact(total));
        listVO.setPagination(pagination);
        listVO.setList(page.getRecords());
        return listVO;
    }
    /**
     * 转换分页list
     * @param page 要转换的Page对象
     * @return 转换后的 PageListVO 对象
     * @throws IllegalArgumentException 如果page参数为null
     */
    public static <T> PageInfo<T> coverPageInfo(Page<T> page) {
        // 检查参数是否为 null
        if (page == null) {
            throw new IllegalArgumentException("Page object cannot be null");
        }
        PageInfo<T> listVO = new PageInfo<>();
        // 调用者应当处理返回值超出int范围时的异常情况
        long total = page.getTotal();
        listVO.setList(page.getRecords());
        listVO.setTotal(total);
        listVO.setPageSize((int) page.getSize());
        listVO.setPageNum((int) page.getCurrent());
        return listVO;
    }
    /**
     * 转换分页list
     * @param page 要转换的Page对象
     * @return 转换后的 PageListVO 对象
     * @throws IllegalArgumentException 如果page参数为null
     */
    public static <T> PageListVO<T> coverPageInfoList(PageInfo<T> page) {
        // 检查参数是否为 null
        if (page == null) {
            throw new IllegalArgumentException("Page object cannot be null");
        }
        PageListVO<T> listVO = new PageListVO<>();
        PaginationVO pagination = new PaginationVO();
        pagination.setCurrentPage((long) page.getPageNum());
        pagination.setPageSize((long) page.getPageSize());
        // 调用者应当处理返回值超出int范围时的异常情况
        long total = page.getTotal();
        pagination.setTotal(Math.toIntExact(total));

        listVO.setPagination(pagination);
        listVO.setList(page.getList());
        return listVO;
    }
    /**
     * 在内存中进行分页操作的方法
     *
     * @param dataList 所有数据的List
     * @param page 分页对象
     * @return 返回指定页码的分页数据
     */
    public static  <T> PageListVO<T> paginate(List<T> dataList, Page<T> page) {
        long pageIndex =page.getCurrent();
        long pageSize = page.getSize();
        PageListVO<T> listVO = new PageListVO<>();
        PaginationVO pagination = new PaginationVO();
        pagination.setCurrentPage(page.getCurrent());
        pagination.setPageSize(page.getSize());
        listVO.setPagination(pagination);
        if (pageSize == -1){
            // 返回全部数据
            listVO.setList(dataList);
            return listVO;
        }
        if (pageIndex < 1 || pageSize <= 0 || dataList == null || dataList.isEmpty()) {
            pagination.setTotal(0);
            return listVO;
        }
        long start = (pageIndex - 1) * pageSize;
        long end = Math.min(start + pageSize, dataList.size());
        // 使用subList方法截取指定范围的数据
        String s = subListRangeCheck(start, end, dataList.size());
        List<T> tList = new ArrayList<>();
        if (StringUtils.isEmpty(s)){
            tList  = dataList.subList(Math.toIntExact(start), Math.toIntExact(end));
        }
        // 调用者应当处理返回值超出int范围时的异常情况
        long total = dataList.size();
        pagination.setTotal(Math.toIntExact(total));
        listVO.setList(tList);
        return listVO;
    }
    /**
     * 使用外部分页数据进行转换
     */
    public static  <T> PageListVO<T> paginate(List<T> dataList, PaginationVO pagination) {
        PageListVO<T> listVO = new PageListVO<>();
        listVO.setPagination(pagination);
        if (pagination.getCurrentPage() == null || pagination.getPageSize() == null) {
            pagination.setTotal(0);
            return listVO;
        }
        long pageSize = pagination.getPageSize();
        if (pageSize == -1){
            // 返回全部数据
            listVO.setList(dataList);
            return listVO;
        }
        long pageIndex =pagination.getCurrentPage();
        if (pageIndex < 1 || pageSize <= 0 || dataList == null || dataList.isEmpty()) {
            pagination.setTotal(0);
            return listVO;
        }
        long start = (pageIndex - 1) * pageSize;
        long end = Math.min(start + pageSize, dataList.size());
        // 使用subList方法截取指定范围的数据
        String s = subListRangeCheck(start, end, dataList.size());
        List<T> tList = new ArrayList<>();
        if (StringUtils.isEmpty(s)){
            tList  = dataList.subList(Math.toIntExact(start), Math.toIntExact(end));
        }
        // 调用者应当处理返回值超出int范围时的异常情况
        long total = dataList.size();
        pagination.setTotal(Math.toIntExact(total));
        listVO.setList(tList);
        return listVO;
    }
    /**
     * 在内存中进行分页操作的方法
     *
     * @param dataList 所有数据的List
     * @param page 分页对象
     * @return 返回指定页码的分页数据
     */
    public static  <T> PageInfo<T> paginateInfo(List<T> dataList, Page<T> page) {
        long pageIndex =page.getCurrent();
        long pageSize = page.getSize();
        PageInfo<T> listVO = new PageInfo<>();
        if (pageIndex < 1 || pageSize <= 0 || dataList == null || dataList.isEmpty()) {
            return listVO;
        }

        long start = (pageIndex - 1) * pageSize;
        long end = Math.min(start + pageSize, dataList.size());
        // 使用subList方法截取指定范围的数据
        String s = subListRangeCheck(start, end, dataList.size());
        List<T> tList = new ArrayList<>();
        if (StringUtils.isEmpty(s)){
            tList  = dataList.subList(Math.toIntExact(start), Math.toIntExact(end));
        }
        // 调用者应当处理返回值超出int范围时的异常情况
        long total = dataList.size();
        listVO.setList(tList);
        listVO.setTotal(total);
        listVO.setPageSize((int) page.getSize());
        listVO.setPageNum((int) page.getCurrent());
        return listVO;
    }

    private static String subListRangeCheck(long fromIndex, long toIndex, int size) {
        if (fromIndex < 0){
            return "1";
        }
        if (toIndex > size){
            return "2";
        }
        if (fromIndex > toIndex){
            return "3";
        }
        return null;
    }
}
