package com.style24.persistence;

import java.io.Serializable;

import lombok.Data;

/**
 * Paging
 *
 * @author gagamel
 * @since 2020. 01. 14
 */
@SuppressWarnings("serial")
@Data
public class TscPageRequest implements Serializable {

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

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

	public TscPageRequest(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;	// oracle
//		return pageNo;		// mysql
	}

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

	public int getStartRow() {
		return pageNo * pageSize + 1;
	}
	
	public int getLimitStartRow() {
		return (pageNo * pageSize + 1) - 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;
	}

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

}
