package com.engine.salary.util.page;

import org.apache.commons.collections4.CollectionUtils;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class SalaryPageUtil {

    public static <T> PageInfo<T> buildPage(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(pageNo);
        pageInfo.setPageSize(pageSize);
        pageInfo.setList(new ArrayList<>());
        return pageInfo;
    }

    public static <T> PageInfo<T> buildPage(Integer pageNo, Integer pageSize, Class<T> clazz) {
        pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
        PageInfo<T> pageInfo = new PageInfo<>(clazz);
        pageInfo.setPageNum(pageNo);
        pageInfo.setPageSize(pageSize);
        pageInfo.setList(new ArrayList<>());
        return pageInfo;
    }

    public static <T> PageInfo<T> buildPage(Integer pageNo, Integer pageSize, List<T> totalCollection) {
        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setTotal(totalCollection.size());
        totalCollection = subList(pageNo, pageSize, totalCollection);
        pageInfo.setPageNum(pageNo);
        pageInfo.setPageSize(pageSize);
        pageInfo.setList(totalCollection);
        return pageInfo;
    }

    /**
     * 内存分页
     *
     * @param pageNo
     * @param pageSize
     * @param source
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> PageInfo<T> buildPage(Integer pageNo, Integer pageSize, List<T> source, Class<T> clazz) {
        pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
        PageInfo<T> pageInfo = new PageInfo<>(clazz);
        pageInfo.setPageNum(pageNo);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(source == null ? 0 : source.size());
        pageInfo.setList(subList(pageNo, pageSize, source));
        return pageInfo;
    }


    /**
     * 分页
     *
     * @param pageNo   页码（从1开始）
     * @param pageSize 每页条数
     * @param source   待分页的数据
     * @param <T>      范型制定类
     * @return
     */
    public static <T> List<T> subList(Integer pageNo, Integer pageSize, List<T> source) {
        if (CollectionUtils.isEmpty(source)) {
            return Collections.emptyList();
        }
        pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
        int endIndex = pageNo * pageSize;
        int startIndex = (pageNo - 1) * pageSize;
        startIndex = startIndex < 0 ? 0 : startIndex;
        return source.subList(startIndex > source.size() ? source.size() : startIndex,
                endIndex > source.size() ? source.size() : endIndex);
    }


    static Font font = new Font("Arial", Font.PLAIN, 12); // 设置字体样式、大小等属性
    static FontRenderContext frc = new FontRenderContext(null, true, false);
//        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    /**
     * 自适应文字长度
     *
     * @param chars
     * @param width
     * @return
     */
    public static String selfAdaption(String chars, Integer width) {
        if (width != null && width != 0) {
            return width + "";
        }
        Rectangle2D bounds = font.getStringBounds(chars, frc);
        int pxLength = (int) Math.ceil(bounds.getWidth()) + 55;
        return pxLength < 90 ? 90+"" : pxLength+"";
    }
}
