package com.style24.persistence;

import java.io.Serializable;

import lombok.Data;

/**
 * Paging
 *
 * @author gagamel
 * @since 2020. 10. 19
 */
@SuppressWarnings("serial")
@Data
public class TssPageRequest implements Serializable {

	private final int pageNo;   // 페이지번호
	private final int pageSize; // 조회할 row수
	private final int pageUnit; // 그룹핑 페이지 단위
	private int totalCount = 0; // 전체 row 건수

	public TssPageRequest(int pageNo, int pageSize) {
		this(pageNo, pageSize, 10);
	}

	public TssPageRequest(int pageNo, int pageSize, int pageUnit) {
		if (pageNo < 0) {
			throw new IllegalArgumentException("Current page index must not be less than zero!");
		}

		if (pageSize < 1) {
			throw new IllegalArgumentException("Page size must not be less than one!");
		}

		if (pageUnit < 1) {
			throw new IllegalArgumentException("Page unit must not be less than one!");
		}

		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.pageUnit = pageUnit;
	}

	public int getPageNo() {
		return pageNo + 1;
	}

	public int getOffset() {
		return pageNo * pageSize;
	}

	public int getStartRow() {
		return pageNo * pageSize + 1;
	}

	public int getEndRow() {
		return getOffset() + pageSize;
	}

	public int getPageGroup() {
		return pageNo / pageUnit + 1;
	}

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

	public int getTotalPage() {
		int totalPage = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			totalPage++;
		}
		return totalPage;
	}

	public String getGeneratedPagination() {
		int firstCount = (getPageGroup() - 1) * pageUnit + 1;
		int loopCount = firstCount + pageUnit;
		if (loopCount > getTotalPage()) {
			loopCount = getTotalPage() + 1;
		}

		StringBuffer pageTag = new StringBuffer();
		if (!(firstCount == 1 && loopCount == 1)) {
			if (getPageNo() == 1) {
				pageTag.append("<a class=\"arrow\" href=\"#\"><i class=\"fa fa-angle-double-left\" alt=\"맨처음\"></i></a>\n");
			} else {
				pageTag.append("<a class=\"arrow\" href=\"#pageNo=1\"><i class=\"fa fa-angle-double-left\" alt=\"맨처음\"></i></a>\n");
			}

			if (getPageGroup() == 1) {
				pageTag.append("<a class=\"arrow\" href=\"#\"><i class=\"fa fa-angle-left\" alt=\"이전페이지\"></i></a>\n");
			} else {
				pageTag.append("<a class=\"arrow\" href=\"#pageNo=").append((getPageGroup() - 1) * pageUnit).append("\"><i class=\"fa fa-angle-left\" alt=\"이전페이지\"></i></a>\n");
			}

			for (int i = firstCount; i < loopCount; i++) {
				if (getPageNo() == i) {
					pageTag.append("<a class=\"num on\" href=\"#\">").append(i).append("</a>\n");
				} else {
					pageTag.append("<a class=\"num\" href=\"#pageNo=").append(i).append("\">").append(i).append("</a>\n");
				}
			}

			if (loopCount <= (getTotalPage() + 1)) {
				if (getPageNo() == getTotalPage()) {
					pageTag.append("<a class=\"arrow\" href=\"#\"><i class=\"fa fa-angle-right\" alt=\"다음페이지\"></i></a>\n");
					pageTag.append("<a class=\"arrow\" href=\"#\"><i class=\"fa fa-angle-double-right\" alt=\"맨마지막\"></i></a>\n");
				} else {
					pageTag.append("<a class=\"arrow\" href=\"#pageNo=").append(getPageGroup() * pageUnit + 1).append("\"><i class=\"fa fa-angle-right\" alt=\"다음페이지\"></i></a>\n");
					pageTag.append("<a class=\"arrow\" href=\"#pageNo=").append(getTotalPage()).append("\"><i class=\"fa fa-angle-double-right\" alt=\"맨마지막\"></i></a>\n");
				}
			}
		}
		return pageTag.toString();
	}

	@Override
	public String toString() {
		return String.format("Page request [pageNo: %d, pageSize %d, pageUnit %d]", getPageNo(), pageSize, pageUnit);
	}

}
