package com.hfkj.common.utils;

import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * 分页工具
 */
public class PageUtil {

    /**
     * 初始化
     * @param currentPage
     * @param total
     * @param pageSize
     * @param pageInfo
     * @param <T>
     * @return
     */
    public static <T> PageInfo<T> initPageInfoObj(int currentPage, int total, int pageSize, PageInfo<T> pageInfo) {
        List<T> list = pageInfo.getList();
        int fromIndex = 0;
        int toIndex = 0;
        if (total / pageSize == 0 && total % pageSize > 0) {
            fromIndex = 0;
            toIndex = total;
        } else {
            if (total / pageSize >= 1 && total % pageSize >= 0) {
                fromIndex = pageSize * (currentPage - 1);
                if (pageSize * currentPage >= total) {
                    toIndex = total;
                } else {
                    toIndex = pageSize * currentPage;
                }
            }
        }

        try {
            list = list.subList(fromIndex, toIndex);
        } catch (IndexOutOfBoundsException e) {
            fromIndex = 0;
            toIndex= pageSize;
            list = list.subList(fromIndex, toIndex);
        }catch(IllegalArgumentException e) {
            fromIndex = total-pageSize;
            toIndex =total;
            list = list.subList(fromIndex, toIndex);
        }
        pageInfo.setList(list);
        pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(currentPage);
        pageInfo.setPages((total + pageSize - 1) / pageSize);
        pageInfo.setNavigateLastPage((total + pageSize - 1) / pageSize);
        pageInfo.setPrePage(currentPage > 1 ? currentPage - 1 : currentPage);
        pageInfo.setIsFirstPage(currentPage == 1 ? true : false);
        pageInfo.setIsLastPage(currentPage == (total + pageSize - 1) / pageSize ? true : false);
        pageInfo.setHasPreviousPage(currentPage == 1 ? false : true);
        pageInfo.setHasNextPage(currentPage == (total + pageSize - 1) / pageSize ? false : true);
        return calcNavigatepageNums(pageInfo);
    }

    private static <T> PageInfo<T> calcNavigatepageNums(PageInfo<T> pageInfo) {
        // 当总页数小于或等于导航页码数时
        if (pageInfo.getPages() <= pageInfo.getNavigatePages()) {
            pageInfo.setNavigatepageNums(new int[pageInfo.getPages()]);
            for (int i = 0; i < pageInfo.getPages(); i++) {
                pageInfo.getNavigatepageNums()[i] = i + 1;
            }
        } else { // 当总页数大于导航页码数时
            pageInfo.setNavigatepageNums(new int[pageInfo.getNavigatePages()]);
            int startNum = pageInfo.getPageNum() - pageInfo.getNavigatePages() / 2;
            int endNum = pageInfo.getPageNum() + pageInfo.getNavigatePages() / 2;

            if (startNum < 1) {
                startNum = 1;
                // (最前navigatePages页
                for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
                    pageInfo.getNavigatepageNums()[i] = startNum++;
                }
            } else if (endNum > pageInfo.getPages()) {
                endNum = pageInfo.getPages();
                // 最后navigatePages页
                for (int i = pageInfo.getNavigatePages() - 1; i >= 0; i--) {
                    pageInfo.getNavigatepageNums()[i] = endNum--;
                }
            } else {
                // 所有中间页
                for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
                    pageInfo.getNavigatepageNums()[i] = startNum++;
                }
            }
        }
        return pageInfo;
    }

}
