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

import kr.co.hsnc.common.util.ValueObject;

public class PageAnchor {

	public static int TYPE_DEFAULT = 2;
	public static int TYPE_LIST = 1;
	public static int TYPE_INPUTBOX = 2;

	/**
	 * HTML Paging Anchor Source를 리턴한다.
	 * 
	 * @param info
	 * @return
	 */
	public static String getPageAnchor(PageAnchorInfo info) {
		return getPageAnchor(info, TYPE_DEFAULT);
	}

	/**
	 * HTML Paging Anchor Source를 리턴한다.
	 * 
	 * @param info
	 * @return
	 */
	public static String getPageAnchor(PageAnchorInfo info, int type) {
		if (type == TYPE_LIST) {
			return getPageAnchor1(info);
		} else if (type == TYPE_INPUTBOX) {
			return getPageAnchor2(info);
		} else {
			return "";
		}
	}

	private static String getPageAnchor1(PageAnchorInfo info) {
		String paramText = getParamText(info);

		if (info.totalCnt == 0)
			return "";

		String anchor = "";
		String leftanchor = "";
		String rightanchor = "";

		int k = 0;
		int totalpage = info.getTotalPage();
		int start = info.currPage / info.pageRange;

		if ((info.currPage % info.pageRange) == 0)
			start = start - 1;

		/*
		 * Front 처리
		 */
		if (info.currPage > info.pageRange) {
			leftanchor += "&nbsp;" + "<A href=\"" + info.uriPath
					+ "?currPage=1" + paramText + "\">" + info.front + "</A>"
					+ info.space;
		} else {
			leftanchor += "&nbsp;" + info.disableFront + info.space;
		}

		/*
		 * Prev 처리
		 */
		if (info.currPage > info.pageRange) {
			leftanchor += "<A href=\"" + info.uriPath + "?currPage="
					+ Integer.toString(info.currPage - info.pageRange)
					+ paramText + "\">" + info.prev + "</A>" + info.space;
			k++;
		} else {
			leftanchor += info.disablePrev + info.space;
		}

		for (int i = start * info.pageRange + 1; i < (start + 1)
				* info.pageRange + 1; i++) {
			k++;
			if (i == totalpage)
				break;
		}

		/*
		 * Next 처리
		 */
		if (totalpage > (start + 1) * info.pageRange) {
			rightanchor += "<A href=\"" + info.uriPath + "?currPage="
					+ Integer.toString((start + 1) * info.pageRange + 1)
					+ paramText + "\">" + info.next + "</A>";
		} else {
			rightanchor += info.disableNext;
		}

		/*
		 * End 처리
		 */
		if (totalpage > (start + 1) * info.pageRange) {

			rightanchor += "&nbsp;" + "<A href=\"" + info.uriPath
					+ "?currPage=" + totalpage + paramText + "\">" + info.end
					+ "</A>" + info.space;
		} else {
			rightanchor += "&nbsp;" + info.disableEnd + info.space;
		}

		k = 0;
		/*
		 * PageAnchor 처리
		 */
		for (int i = start * info.pageRange + 1; i < (start + 1)
				* info.pageRange + 1; i++) {
			if (i == info.currPage) {
				anchor += info.currPageFontTagLeft + info.pageNumLeft
						+ Integer.toString(info.currPage) + info.pageNumRight
						+ info.currPageFontTagRight + info.space;
			} else {
				anchor += "<A href=\"" + info.uriPath + "?currPage="
						+ Integer.toString(i) + paramText + "\">"
						+ info.otherPageFontTagLeft + info.pageNumLeft
						+ Integer.toString(i) + info.pageNumRight
						+ info.otherPageFontTagRight + "</A>" + info.space;
			}
			k++;
			if (i == totalpage)
				break;
		}

		return leftanchor + "&nbsp;&nbsp;" + anchor + "&nbsp;&nbsp;"
				+ rightanchor;
	}

	private static String getPageAnchor2(PageAnchorInfo info) {

		String paramText = getParamText(info);
		;
		if (info.totalCnt == 0)
			return "";
		String anchor = "";
		String leftanchor = "";
		String rightanchor = "";
		int totalpage = info.getTotalPage();
		int start = info.currPage / info.pageRange;

		if ((info.currPage % info.pageRange) == 0)
			start = start - 1;

		/*
		 * Front 처리
		 */
		if (info.currPage > 1) {
			leftanchor += "&nbsp;" + "<A href=\"" + info.uriPath
					+ "?currPage=1" + "&range=" + info.range + paramText
					+ "\">" + info.front + "</A>" + info.space;
		} else {
			leftanchor += "&nbsp;" + info.disableFront + info.space;
		}

		/*
		 * Prev 처리
		 */
		if (info.currPage > 1) {
			leftanchor += "<A href=\"" + info.uriPath + "?currPage="
					+ (info.currPage - 1) + "&range=" + info.range + paramText
					+ "\">" + info.prev + "</A>" + info.space;
		} else {
			leftanchor += info.disablePrev + info.space;
		}

		/*
		 * Next 처리
		 */
		if (info.currPage < totalpage) {
			rightanchor += "<A href=\"" + info.uriPath + "?currPage="
					+ (info.currPage + 1) + "&range=" + info.range + paramText
					+ "\">" + info.next + "</A>";
		} else {
			rightanchor += info.disableNext;
		}

		/*
		 * End 처리
		 */
		if (info.currPage < totalpage) {

			rightanchor += "&nbsp;" + "<A href=\"" + info.uriPath
					+ "?currPage=" + totalpage + "&range=" + info.range
					+ paramText + "\">" + info.end + "</A>" + info.space;
		} else {
			rightanchor += "&nbsp;" + info.disableEnd + info.space;
		}

		anchor += "<INPUT type='text' name='currPage' class='input' value='"
				+ info.currPage + "'";
		String temp = "";
		temp += "{" + "  if( event.keyCode == 13 ) { "
				+ "    self.location = '" + info.uriPath + "?" + paramText
				+ "&currPage=' + this.value + '&range=" + info.range + "';"
				+ "  } " + "} ";
		anchor += " onKeyPress=\"JavaScript:doKeyPressEvent(this, 'NUMBER'); "
				+ temp + " \"";
		anchor += " onFocus=\"JavaScript:doNumberFloatFocusEvent(this); this.select();\"";
		anchor += " onBlur=\"JavaScript:doNumberFloatBlurEvent(this);\"";
		anchor += " style='width:30px' id='currPage'>";
		anchor += " / " + info.getTotalPage();

		return leftanchor + "&nbsp;&nbsp;" + anchor + "&nbsp;&nbsp;"
				+ rightanchor;
	}

	/**
	 * Paging 처리된 정보를 리턴한다. ex> 총건수: 10 페이지: 1/5
	 * 
	 * @param info
	 * @return
	 */
	public static String getPageTextInfo(PageAnchorInfo info) {
		return "총건수: " + info.getTotalCount() + " &nbsp; 페이지: "
				+ info.getCurrPage() + "/" + info.getTotalPage();
	}

	/**
	 * Paging 처리시에 사용한 Parameter 정보를 get방식으로 리턴한다. ex>
	 * param1=aaaa&param2=bbb&param3=ccc
	 * 
	 * @param info
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getParamText(PageAnchorInfo info) {
		String paramText = "";

		ValueObject params = info.getParameter();

		if (params == null || params.size() == 0)
			return "";

		for (int i = 0; i < params.size(); i++) {
			String key = params.getKey(i);
			paramText += "&"
					+ key
					+ "="
					+ java.net.URLEncoder.encode(toEncoding(params.get(params.getKey(i))));
		}

		return paramText;
	}

	/**
	 * Paging 처리시에 사용한 Parameter 정보를 get방식으로 리턴한다. currPage, range 항목을 포함해서
	 * 리턴한다. ex> param1=aaaa&param2=bbb&param3=ccc&currPage=1&range=10
	 * 
	 * @param info
	 * @return
	 */
	public static String getAllParamText(PageAnchorInfo info) {
		String paramText = "";
		paramText = getParamText(info);
		paramText += "&currPage=" + info.currPage + "&range=" + info.range;
		return paramText;
	}

	/**
	 * 페이지 Range를 HTML 코드를 리턴
	 * 
	 * @param info
	 * @return
	 */
	public static String getRangeSelectBox(PageAnchorInfo info) {
		String paramText = getParamText(info);
		String temp = "";
		temp += "<SELECT name=\"range\" class=\"input_select\"";
		temp += "onchange=\"self.location = '"
				+ info.uriPath
				+ "?"
				+ paramText
				+ "&currPage=1&range=' + this.options[this.selectedIndex].value;"
				+ "\"";
		temp += ">";

		boolean isSelected = false;
		int[] ranges = { 2, 3, 5, 10, 50, 100 };
		for (int i = 0; i < ranges.length; i++) {
			String selected = (info.range == ranges[i] ? "selected" : "");
			temp += "<option value='" + ranges[i] + "'  " + selected + ">"
					+ ranges[i] + "</option>";
			if (!"".equals(selected))
				isSelected = true;
		}
		if (!isSelected) {
			temp += "<option value='" + info.range + "'  selected>"
					+ info.range + "</option>";
		}
		temp += "<SELECT>";
		return temp;
	}

	private static String toEncoding(String value) {
		return value;
		// return Translator.translate("UTF-8", "ISO-8859-1", value);
	}
}
