package com.hzya.frame.page;

import java.io.Serializable;
import java.util.List;

/***
 * @Content:
 * @Author 👻👻👻👻👻👻👻👻 gjh
 * @Date  2023-07-18 16:52
 * @Param
 * @return
 **/
public class PageAttribute<T> implements Serializable {
	private int pageNum;
	private int pageSize;
	private int size;
	private int startRow;
	private int endRow;
	private long total;
	private int pages;
	private int page;
	private List<T> rows;
	private int firstPage;
	private int prePage;
	private int nextPage;

	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	private int lastPage;
	private boolean isFirstPage;
	private boolean isLastPage;
	private boolean hasPreviousPage;
	private boolean hasNextPage;
	private int navigatePages;
	private int[] navigatepageNums;

	public PageAttribute(List<T> list) {
		this(list, 8);
	}

	public PageAttribute(List<T> list, int navigatePages) {
		this.isFirstPage = false;
		this.isLastPage = false;
		this.hasPreviousPage = false;
		this.hasNextPage = false;

		if (list instanceof Page) {
			Page page = (Page) list;
			this.pageNum = page.getPageNum();
			this.pageSize = page.getPageSize();
			this.total = page.getTotal();
			this.pages = page.getPages();
			this.page = page.getPages();
			this.rows = page;
			this.size = page.size();
			this.startRow = (page.getStartRow() + 1);
			this.endRow = (this.startRow - 1 + this.size);
			this.navigatePages = navigatePages;
			calcNavigatepageNums();
			calcPage();
			judgePageBoudary();
		}
	}

	private void calcNavigatepageNums() {
		if (this.pages <= this.navigatePages) {
			this.navigatepageNums = new int[this.pages];
			for (int i = 0; i < this.pages; ++i) {
                this.navigatepageNums[i] = (i + 1);
            }
		} else {
			int i;
			this.navigatepageNums = new int[this.navigatePages];
			int startNum = this.pageNum - this.navigatePages / 2;
			int endNum = this.pageNum + this.navigatePages / 2;
			if (startNum < 1) {
				startNum = 1;
				for (i = 0; i < this.navigatePages; ++i) {
                    this.navigatepageNums[i] = (startNum++);
                }
			} else if (endNum > this.pages) {
				endNum = this.pages;

				for (i = this.navigatePages - 1; i >= 0; --i) {
                    this.navigatepageNums[i] = (endNum--);
                }
			} else {
				for (i = 0; i < this.navigatePages; ++i) {
                    this.navigatepageNums[i] = (startNum++);
                }
			}
		}
	}

	public int getPage() {
		return this.page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private void calcPage() {
		if ((this.navigatepageNums != null) && (this.navigatepageNums.length > 0)) {
			this.firstPage = this.navigatepageNums[0];
			this.lastPage = this.navigatepageNums[(this.navigatepageNums.length - 1)];
			if (this.pageNum > 1) {
                this.prePage = (this.pageNum - 1);
            }

			if (this.pageNum < this.pages) {
                this.nextPage = (this.pageNum + 1);
            }
		}
	}

	private void judgePageBoudary() {
		this.isFirstPage = (this.pageNum == 1);
		this.isLastPage = ((this.pageNum == this.pages) && (this.pageNum != 1));
		this.hasPreviousPage = (this.pageNum > 1);
		this.hasNextPage = (this.pageNum < this.pages);
	}

	public int getPageNum() {
		return this.pageNum;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public int getStartRow() {
		return this.startRow;
	}

	public int getEndRow() {
		return this.endRow;
	}

	public long getTotal() {
		return this.total;
	}

	public int getPages() {
		return this.pages;
	}

	public List<T> getRows() {
		return this.rows;
	}

	public int getFirstPage() {
		return this.firstPage;
	}

	public int getPrePage() {
		return this.prePage;
	}

	public int getNextPage() {
		return this.nextPage;
	}

	public int getLastPage() {
		return this.lastPage;
	}

	public boolean isFirstPage() {
		return this.isFirstPage;
	}

	public boolean isLastPage() {
		return this.isLastPage;
	}

	public boolean isHasPreviousPage() {
		return this.hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return this.hasNextPage;
	}

	public int getNavigatePages() {
		return this.navigatePages;
	}

	public int[] getNavigatepageNums() {
		return this.navigatepageNums;
	}

	@Override
    public String toString() {
		StringBuffer sb = new StringBuffer("PageInfo{");
		sb.append("pageNum=").append(this.pageNum);
		sb.append(", pageSize=").append(this.pageSize);
		sb.append(", size=").append(this.size);
		sb.append(", startRow=").append(this.startRow);
		sb.append(", endRow=").append(this.endRow);
		sb.append(", total=").append(this.total);
		sb.append(", pages=").append(this.pages);
		sb.append(", rows=").append(this.rows);
		sb.append(", firstPage=").append(this.firstPage);
		sb.append(", prePage=").append(this.prePage);
		sb.append(", nextPage=").append(this.nextPage);
		sb.append(", lastPage=").append(this.lastPage);
		sb.append(", isFirstPage=").append(this.isFirstPage);
		sb.append(", isLastPage=").append(this.isLastPage);
		sb.append(", hasPreviousPage=").append(this.hasPreviousPage);
		sb.append(", hasNextPage=").append(this.hasNextPage);
		sb.append(", navigatePages=").append(this.navigatePages);
		sb.append(", navigatepageNums=");
		if (this.navigatepageNums == null) {
			sb.append("null");
		} else {
			sb.append('[');
			for (int i = 0; i < this.navigatepageNums.length; ++i) {
                sb.append((i == 0) ? "" : ", ").append(this.navigatepageNums[i]);
            }
			sb.append(']');
		}
		sb.append('}');
		return sb.toString();
	}
}
