package com.ecep.contract;

import lombok.Data;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import java.util.List;

@Data
public class PageContent<T> {

    private List<T> content;
    private PageArgument page;
    private int totalElements;
    private int totalPages;

    public static <T> PageContent<T> of(Page<T> page) {
        PageContent<T> content = new PageContent<>();
        content.setContent(page.getContent());
        content.setPage(PageArgument.of(page.getPageable()));
        content.setTotalElements((int) page.getTotalElements());
        content.setTotalPages(page.getTotalPages());
        return content;
    }

    public Page<T> toPage() {
        PageImpl<T> page = new PageImpl<>(content == null ? List.of() : content, getPage().toPageable(), totalElements);
        return page;
    }

}
