package com.palmnest.application.bean;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <a href=
 * "http://appfuse.sinaapp.com/topic/3/%E5%88%86%E9%A1%B5%E8%BE%85%E5%8A%A9%E7%B1%BB%28java%E7%89%88%29"
 * >分页辅助类(java版)</a>
 *
 * @filename: Pagenation.java
 * @author: tianjie
 * @version: 1.0
 * @create time: 26 Mar 2014 14:59:44
 * @record
 */
public class Pagenation<T> {

	private Logger log = LoggerFactory.getLogger(Pagenation.class);

	public static final int DEFAULT_PAGE_SIZE = 10;

	private List<T> items;
	private int     page, totalCount, pageSize = DEFAULT_PAGE_SIZE;
	private Object extraData;


	// **********************
	public Pagenation(List<T> items, int page, int totalCount) {
		this.items = items;
		this.totalCount = totalCount;
		if (page <= 0 || (page > this.getPages() && this.getPages() != 0)) {
			log.warn("错误的页号。将页号重置为1");
			this.page = 1;
		} else
			this.page = page;
	}

	public Pagenation(List<T> items, int page, int totalCount, int pageSize) {
		if (pageSize < 0) {
			log.warn("页大小不能小于0。使用默认值10");
			this.pageSize = DEFAULT_PAGE_SIZE;
		} else {
			this.pageSize = pageSize;
		}
		this.items = items;
		this.totalCount = totalCount;

		if (page <= 0 || page > this.getPages()) {
			this.page = 1;
		} else {
			this.page = page;
		}

	}

	// getter and setter

	/**
	 * 默认展示最左（右）展示1个，中间展示5个，当前页居中
	 *
	 * @see Pagenation#getIterPages(int, int, int, int)
	 */
	public List<Integer> getIterPages() {
		int left_edge = 2, left_current = 3, right_current = 3, right_edge = 2;
		return getIterPages(left_edge, left_current, right_current, right_edge);
	}

	/**
	 * 返回分页工具条可用的页数<br>
	 * 如没页显示10条数据，166条数据，当前第6页。则结果为：<br>
	 * [1, null, 5, 6, 7, 8, 9, 10, null, 17]<br>
	 * 结果中值为null的元素用省略号等字符表示
	 *
	 * @return
	 * @author tianjie
	 * @date 26 Mar 2014 15:31:04
	 * @version v1.0
	 * @see flask_sqlalchemy#Pagination
	 */
	public List<Integer> getIterPages(int left_edge, int left_current,
									  int right_current, int right_edge) {
		List<Integer> ips = new ArrayList<Integer>();
		int last = 0;
		for (int num = 1; num < this.getPages() + 1; num++) {
			if (num < left_edge
					|| (num > this.page - left_current - 1 && num < this.page
					+ right_current)
					|| num > this.getPages() - right_edge) {
				if (last + 1 != num)
					ips.add(null);
				else
					ips.add(num);
				last = num;
			}
		}
		return ips;
	}

	/**
	 * 总页数
	 *
	 * @return
	 */
	public int getPages() {
		int pages = 0;
		if (pageSize == 0)
			pages = 0;
		else
			pages = (this.totalCount + pageSize - 1) / pageSize;
		return pages;
	}

	public int getPageSize() {
		return pageSize;
	}

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

	public List<T> getItems() {
		return items;
	}

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

	public boolean isHasFirst() {
		return this.getPages() >= 1;
	}

	public boolean isHasPre() {
		return this.page > 1;
	}

	public boolean isHasLast() {
		return this.page < this.getPages();
	}

	public boolean isHasNext() {
		return this.page < this.getPages();
	}

	public int getFirstPage() {
		return 1;
	}

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

	public int getNextPage() {
		return this.page + 1;
	}

	public int getPrePage() {
		return this.page - 1;
	}

	public Object getExtraData() {
		return extraData;
	}

	public void setExtraData(Object extraData) {
		this.extraData = extraData;
	}

	public int getTotalCount() {
		return totalCount;
	}

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

}
