package com.ffii.core.utils;

import java.util.Collections;
import java.util.List;

/**
 * MapUtils
 *
 * @author Jason Lam
 */

public class PagingUtils {

    /**
     * Create target sub list from a full list
     *
     * @param fullList
     *            List to be split
     * @param pageSize
     *            targeted record per page
     * @param pageNum
     *            targeted page to be display
     *
     * @return List
     */
    public static <T> List<T> getPaginatedList(List<T> fullList, int pageSize, int pageNum) {
        // Calculate the start and end indices for subList
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, fullList.size());

        // If startIndex is beyond the fullList size, return the remaining records
        if (startIndex < fullList.size()) {
            return fullList.subList(startIndex, endIndex);
        } else {
            // If there are records available, return the remaining records
            if (!fullList.isEmpty()) {
                return fullList.subList(fullList.size() - Math.min(pageSize, fullList.size()), fullList.size());
            } else {
                return Collections.emptyList(); // Return empty list if fullList is empty
            }
        }
    }
}
