package itn.com.utl.response;

import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;
import java.util.Map;

@Getter
@Setter
@NoArgsConstructor
public class PageResponse<T> {
    private List<T> list;
    private int totalItems;
    private int totalPages;
    private int currentPage;
    private int size;
    private Map<String, Object> extraData;
    
    public PageResponse(List<T> list, Map<String, Object> extraData, int currentPage, int totalItems, int totalPages, int size) {
        this.list = list;
        this.extraData = extraData;
        this.currentPage = currentPage;
        this.totalItems = totalItems;
        this.totalPages = totalPages;
        this.size = size;
    }

    public static <T> PageResponse<T> withList(List<T> list, PaginationInfo pageInfo) {
        PageResponse<T> res = new PageResponse<>();
        res.setList(list);
        res.setPageInfo(pageInfo);
        return res;
    }

    public static <T> PageResponse<T> withExtra(Map<String, Object> extraData, PaginationInfo pageInfo) {
        PageResponse<T> res = new PageResponse<>();
        res.setExtraData(extraData);
        res.setPageInfo(pageInfo);
        return res;
    }

    public static <T> PageResponse<T> withAll(List<T> list, Map<String, Object> extraData, PaginationInfo pageInfo) {
        PageResponse<T> res = new PageResponse<>();
        res.setList(list);
        res.setExtraData(extraData);
        res.setPageInfo(pageInfo);
        return res;
    }

    private void setPageInfo(PaginationInfo pageInfo) {
        this.currentPage = pageInfo.getCurrentPageNo();
        this.totalItems = pageInfo.getTotalRecordCount();
        this.totalPages = pageInfo.getTotalPageCount();
        this.size = pageInfo.getRecordCountPerPage();
    }
}
