package com.udapsoft.waf.common.web.paging;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import kr.co.hsnc.common.config.WAFConfig;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.util.ValueObject;

public class PageAnchorInfo {

	public String sqlstr = "";

	public int totalCnt = 0;
	public int currPage = 1;
	public int pageRange = 10;
	public int range = 20;

	public String uriPath = "";

	public String front = "<FONT face='돋움' size='2' color='black'>[맨앞으로]</FONT>";
	public String end = "<FONT face='돋움' size='2' color='black'>[맨뒤로]</FONT>";
	public String prev = "<FONT face='돋움' size='2' color='black'>[이전]</FONT>";
	public String next = "<FONT face='돋움' size='2' color='black'>[다음]</FONT>";
	public String disableFront = "<FONT face='돋움' size='2' color='silver'>[맨앞으로]</FONT>";
	public String disableEnd = "<FONT face='돋움' size='2' color='silver'>[맨뒤로]</FONT>";
	public String disablePrev = "<FONT face='돋움' size='2' color='silver'>[이전]</FONT>";
	public String disableNext = "<FONT face='돋움' size='2' color='silver'>[다음]</FONT>";

	public String space = "&nbsp";

	public String pageNumLeft = "[";
	public String pageNumRight = "]";

	public String currPageFontTagLeft = "<FONT face='돋움' size='2' color='red'><B>";
	public String currPageFontTagRight = "</B></FONT>";

	public String otherPageFontTagLeft = "<FONT face='돋움' size='2' color='black'>";
	public String otherPageFontTagRight = "</FONT>";

	private ValueObject params = new ValueObject();

	/**
	 * 생성자
	 * 
	 * @param request
	 */
	public PageAnchorInfo(HttpServletRequest request) {
		this.uriPath = request.getRequestURI();

		String currPage = request.getParameter("currPage");
		if (currPage != null && !currPage.equals(""))
			this.currPage = Integer.parseInt(request.getParameter("currPage"));
		else
			this.currPage = 1;

		this.pageRange = WAFConfig.getInt("waf.anchor.pageRange");
		this.range = WAFConfig.getInt("waf.anchor.range");
		String range = request.getParameter("range");
		if (range != null && !range.equals(""))
			this.range = Integer.parseInt(request.getParameter("range"));

		this.space = WAFConfig.get("waf.anchor.space");
		this.front = WAFConfig.get("waf.anchor.front");
		this.end = WAFConfig.get("waf.anchor.end");
		this.prev = WAFConfig.get("waf.anchor.prev");
		this.next = WAFConfig.get("waf.anchor.next");
		this.disableFront = WAFConfig.get("waf.anchor.disableFront");
		this.disableEnd = WAFConfig.get("waf.anchor.disableEnd");
		this.disablePrev = WAFConfig.get("waf.anchor.disablePrev");
		this.disableNext = WAFConfig.get("waf.anchor.disableNext");

		this.pageNumLeft = WAFConfig.get("waf.anchor.pageNumLeft");
		this.pageNumRight = WAFConfig.get("waf.anchor.pageNumRight");

		this.currPageFontTagLeft = WAFConfig
				.get("waf.anchor.currPageFontTagLeft");
		this.currPageFontTagRight = WAFConfig
				.get("waf.anchor.currPageFontTagRight");

		this.otherPageFontTagLeft = WAFConfig
				.get("waf.anchor.otherPageFontTagLeft");
		this.otherPageFontTagRight = WAFConfig
				.get("waf.anchor.otherPageFontTagRight");

	}

	/**
	 * Parameter를 세팅 currPage, range 는 파라미터에서 자동 삭제
	 * 
	 * @param params
	 */
	public void setParams(ValueObject params) {
		if (params != null) {
			this.params = params.copy();
			this.params.remove("currPage"); // currPage는 전송 필드에서 제외
			this.params.remove("range"); // range 는 전송 필드에서 제외
		}
	}

	/**
	 * Parameter를 세팅
	 * 
	 * @param key
	 * @param value
	 */
	public void setParameter(String key, String value) {
		params.set(key, value);
	}

	public void setParameter(HttpServletRequest request) {
		for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {

			String key = (String) e.nextElement();

			if (!key.equals("currPage"))
				params.set(key, request.getParameter(key));
		}
	}

	public ValueObject getParameter() {
		return this.params;
	}

	public String getParameter(String key) {
		return (String) params.get(key);
	}

	public void clearParameter() {

		this.params.clear();
	}

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

	public void setCurrPage(int currPage) {

		this.currPage = currPage;
	}

	public void setPageRange(int pageRange) {
		this.pageRange = pageRange;
	}

	public void setRange(int range) {
		this.range = range;
	}

	public void setUriPath(String uriPath) {
		this.uriPath = uriPath;
	}

	public void setSpace(String string) {
		space = string;
	}

	public void setPrev(String string) {
		prev = string;
	}

	public void setNext(String string) {
		next = string;
	}

	public void setFront(String string) {
		front = string;
	}

	public void setEnd(String string) {
		end = string;
	}

	public void setDisableNext(String string) {
		disableNext = string;
	}

	public void setDisablePrev(String string) {
		disablePrev = string;
	}

	public void setDisableFront(String string) {
		disableFront = string;
	}

	public void setDisableEnd(String string) {
		disableEnd = string;
	}

	public void setPageNumLeft(String string) {
		pageNumLeft = string;
	}

	public void setPageNumRight(String string) {
		pageNumRight = string;
	}

	public void setCurrPageFontTagLeft(String string) {
		currPageFontTagLeft = string;
	}

	public void setCurrPageFontTagRight(String string) {
		currPageFontTagRight = string;
	}

	public void setOtherPageFontTagLeft(String string) {
		otherPageFontTagLeft = string;
	}

	public void setOtherPageFontTagRight(String string) {
		otherPageFontTagRight = string;
	}

	public int getTotalCount() {
		return this.totalCnt;
	}

	public int getCurrPage() {

		return this.currPage;
	}

	public int getPageRange() {
		return this.pageRange;
	}

	public int getRange() {
		return this.range;
	}

	public String getUriPath() {
		return this.uriPath;
	}

	public String getSpace() {
		return space;
	}

	public String getFront() {
		return front;
	}

	public String getEnd() {
		return end;
	}

	public String getPrev() {
		return prev;
	}

	public String getNext() {
		return next;
	}

	public String getDisableFront() {
		return disableFront;
	}

	public String getDisableEnd() {
		return disableEnd;
	}

	public String getDisablePrev() {
		return disablePrev;
	}

	public String getDisableNext() {
		return disableNext;
	}

	public String getPageNumLeft() {
		return pageNumLeft;
	}

	public String getPageNumRight() {
		return pageNumRight;
	}

	public String getCurrPageFontTagLeft() {
		return currPageFontTagLeft;
	}

	public String getCurrPageFontTagRight() {
		return currPageFontTagRight;
	}

	public String getOtherPageFontTagLeft() {
		return otherPageFontTagLeft;
	}

	public String getOtherPageFontTagRight() {
		return otherPageFontTagRight;
	}

	public int getTotalPage() {
		int totalpage = totalCnt % range;

		if (totalpage == 0)
			totalpage = totalCnt / range;
		else
			totalpage = totalCnt / range + 1;

		return totalpage;
	}

	public void setSQL(String sqlstr) {
		this.sqlstr = sqlstr;
	}

	public String getSQL() {
		return this.sqlstr;
	}

	/**
	 * 전체 레코드 건수를 조회
	 * 
	 * @throws Exception
	 */
	public void doTotalCountSQL() throws Exception {
		Search search = new SearchImpl();
		search.setStatement(getTotalCountSQL());
		RowSet rowSet = search.execute();
		setTotalCount(rowSet.getRow(0).getInt("RecordCount"));
	}

	/**
	 * 전체 레코드 건수를 조회용 레코드를 생성후 리턴
	 * 
	 * @return
	 */
	public String getTotalCountSQL() {
		String temp = " SELECT  COUNT(1) AS RecordCount \n" + " FROM    (  \n"
				+ getSQL() + " \n" + "         )  \n";
		return temp;
	}

	/**
	 * 해당 페이지 레코드를 조회하는
	 * 
	 * @return
	 */
	public String getPagingSQL() {
		String temp = " SELECT SECOND_SQL.*                         \n"
				+ " FROM   ( SELECT FIRST_SQL.*,                    \n"
				+ "                 ROWNUM AS RecordNumber          \n"
				+ "          FROM   (                               \n"
				+ getSQL() + " \n" + "                 ) FIRST_SQL \n"
				+ "        ) SECOND_SQL \n" + " WHERE  RecordNumber >= "
				+ getStartRowNum() + " \n" + " AND    RecordNumber <= "
				+ getEndRowNum() + " \n";
		return temp;
	}

	private int getStartRowNum() {
		return range * (currPage - 1) + 1;
	}

	private int getEndRowNum() {
		return (range * currPage);
	}
}