package com.hs.api.model.respmodel;

import java.util.List;

public class ListPage<T> {
    int totalCount;
    int totalPageCount;
    int curPageIndex;
    int pageSize;
    List<T> listData;

    public ListPage() {
    }

    public ListPage(List<T> list,Integer pageSize,Integer pageIndex,int cost) {
        totalCount = list.size();
        this.pageSize = pageSize == null ? totalCount : pageSize;
        curPageIndex = pageIndex == null ? 1 : pageIndex;
        totalPageCount = (int)Math.ceil((double)totalCount / this.pageSize);
        int startIndex = this.pageSize * (curPageIndex - 1) - cost;
        int endIndex = this.pageSize * curPageIndex + cost;
        listData = list.subList(startIndex > totalCount ? totalCount : startIndex < 0 ? 0 : startIndex,endIndex < totalCount ? endIndex : totalCount);
    }

    public ListPage(List<T> list,Integer pageSize,Integer pageIndex) {
        this(list,pageSize,pageIndex,1);
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public int getCurPageIndex() {
        return curPageIndex;
    }

    public void setCurPageIndex(int curPageIndex) {
        this.curPageIndex = curPageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getListData() {
        return listData;
    }

    public void setListData(List<T> listData) {
        this.listData = listData;
    }

    @Override
    public String toString() {
        return "ListPage{" +
                "totalCount=" + totalCount +
                ", totalPageCount=" + totalPageCount +
                ", curPageIndex=" + curPageIndex +
                ", pageSize=" + pageSize +
                ", listData=" + listData +
                '}';
    }
}
