package com.sunyard.ssp.utils;

import cn.hutool.core.util.StrUtil;
import com.sunyard.ssp.common.PageVo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author Exrickx
 */
public class PageUtil {

    /**
     * JPA��ҳ
     *
     * @param page
     * @return
     */
    public static Pageable initPage(PageVo page) {

        Pageable pageable = null;
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();
        String sort = page.getSort();
        String order = page.getOrder();

        if (pageNumber < 1) {
            pageNumber = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }

        if (StrUtil.isNotBlank(sort)) {
            Sort.Direction d;
            if (StrUtil.isBlank(order)) {
                d = Sort.Direction.DESC;
            } else {
                d = Sort.Direction.valueOf(order.toUpperCase());
            }
            Sort s = Sort.by(d, sort);
            pageable = PageRequest.of(pageNumber - 1, pageSize, s);
        } else {
            pageable = PageRequest.of(pageNumber - 1, pageSize);
        }
        return pageable;
    }

    /*
     * jpa的分页和mybatis的分页实现逻辑不一样
     * */
    public static Pageable initMybatisPage(PageVo page) {
        Pageable pageable = null;
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();
        String sort = page.getSort();
        String order = page.getOrder();

        if (pageNumber < 1) {
            pageNumber = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        if (StrUtil.isNotBlank(sort)) {
            Sort.Direction d;
            if (StrUtil.isBlank(order)) {
                d = Sort.Direction.DESC;
            } else {
                d = Sort.Direction.valueOf(order.toUpperCase());
            }
            Sort s = Sort.by(d, sort);
            pageable = PageRequest.of(pageNumber, pageSize, s);
        } else {
            pageable = PageRequest.of(pageNumber, pageSize);
        }
        return pageable;
    }

    /**
     * List 分页
     *
     * @param page
     * @param list
     * @return
     */
    public static List listToPage(PageVo page, List list) {

        Map<String, Integer> pageParam = getPageParam(page);

        int fromIndex = pageParam.get("fromIndex");
        int toIndex = pageParam.get("toIndex");

        if (fromIndex > list.size()) {
            return new ArrayList();
        } else if (toIndex >= list.size()) {
            return list.subList(fromIndex, list.size());
        } else {
            return list.subList(fromIndex, toIndex);
        }
    }

    /**
     * set 分页
     * @param page
     * @param set
     * @return
     */
    public static Set SetToPage(PageVo page, Set set) {

        Map<String, Integer> pageParam = getPageParam(page);

        int fromIndex = pageParam.get("fromIndex");
        int toIndex = pageParam.get("toIndex");

        List<Object> objList = new ArrayList<>(set);

        if (fromIndex > set.size()) {
            return Collections.emptySet();
        } else if (toIndex >= set.size()) {
            return new LinkedHashSet(objList.subList(fromIndex, set.size()));
        } else {
            return new LinkedHashSet(objList.subList(fromIndex, toIndex));
        }
    }

    /**
     * 获取分页参数
     * @param page
     * @return
     */
    public static Map<String, Integer> getPageParam(PageVo page) {

        Map<String, Integer> param = new HashMap<>();

        int pageNumber = page.getPageNumber() - 1;
        int pageSize = page.getPageSize();

        if (pageNumber < 0) {
            pageNumber = 0;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }

        int fromIndex = pageNumber * pageSize;
        int toIndex = pageNumber * pageSize + pageSize;

        param.put("fromIndex", fromIndex);
        param.put("toIndex", toIndex);

        return param;
    }
}
