/**
 * 
 */
package org.springframework.samples.kyuriboard.web.spring;

import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.samples.kyuriboard.domain.logic.KyuriBoardFacade;

import org.apache.log4j.Logger;

/**
 * @author DAMI(archy712@naver.com)
 *
 */
public class ListBoardController implements Controller {
	
	protected Logger logger = Logger.getLogger(getClass());
	
	private String successView;
	
	// List화면인지 Search화면인지 구분하는 변수
	private String mode;
	
	private KyuriBoardFacade kyuriBoard;	
	
	/**
	 * 
	 * @param mode the mode to set
	 */
	public void setMode(String mode) {
		this.mode = mode;
	}

	/**
	 * 
	 * @param successView
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}
	
	/**
	 * 
	 * @param kyuriBoard
	 */
	public void setKyuriBoard(KyuriBoardFacade kyuriBoard) {
		this.kyuriBoard = kyuriBoard;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		
		Map model = new HashMap();
		String page = request.getParameter("page");
		String searchKey = request.getParameter("searchKey");
		String searchVal = request.getParameter("searchVal");
		
		if (logger.isInfoEnabled()) {
			logger.info("searchKey : " + searchKey);
			logger.info("searchVal : " + searchVal);
		}
		
		if (searchVal != null) {
			searchVal = new String(searchVal.getBytes("8859_1"), "euc-kr");		// 톰캣 문자열 처리
		}
		
		//System.out.println("searchKey : " + searchKey);
		//System.out.println("searchVal : " + searchVal);
		
		model.put("searchKey", searchKey);
		model.put("searchVal", searchVal);
		
		if (page == null) {
			page = "1";
		}
		
		// 현재 페이지에 해당하는 게시물 리스트만 담음
		PagedListHolder boardList = null;
		if ("search".equals(this.mode)) {
			if ("writer".equals(searchKey)) {
				boardList = new PagedListHolder(this.kyuriBoard.getBoardListByUserName(searchVal));
			} else {
				boardList = new PagedListHolder(this.kyuriBoard.getBoardListByTitle(searchVal));
			}
		} else {
			boardList = new PagedListHolder(this.kyuriBoard.getBoardList());
		}
		boardList.setPageSize(20);	// 한 페이지의 레코드 수 20 개
		boardList.setPage(new Integer(page).intValue() - 1);	// PagedListHolder는 인덱스가 0부터 시작
		model.put("boardList", boardList);
		
		// 페이지 바로 가기
		int pagedGroup = 10;
		int nPage = new Integer(page).intValue();	// 현재 페이지를 정수형을 변환
		int pagedLoop = pagedGroup;		// 페이지를 Loop 돌릴 MAX 페이지 설정
		if (pagedLoop > boardList.getPageCount()) {
			pagedLoop = boardList.getPageCount();	// 페이지 Loop 변수가 전제 페이지 갯수보다 클 경우 보정
		}
		String deli = new String(" ");	// 구분자
		StringBuffer sb = new StringBuffer();
		
		if ((boardList.getPageCount() > pagedGroup) && (nPage > pagedGroup)) {
			sb.append("<a href=\"listBoard.do?page=" + (nPage - pagedGroup));
			if ((searchKey != null) && (searchVal != null)) {
				sb.append("?searchKey=" + searchKey + "&searchVal=" + searchVal);
			}
			sb.append("\">");
			sb.append("[이전 " + pagedGroup + "개]");
			sb.append("</a>");
			sb.append(deli);
		}
		
		for (int i = 0; i < pagedLoop; i++) {
			sb.append("<a href=\"listBoard.do?page=" + (i + 1));
			if ((searchKey != null) && (searchVal != null)) {
				sb.append("?searchKey=" + searchKey + "&searchVal=" + searchVal);
			}			
			sb.append("\">");
			sb.append(i + 1);
			sb.append("</a>");
			sb.append(deli);
		}
		
		if ((boardList.getPageCount() > pagedGroup) && (nPage < pagedGroup)) {
			sb.append("<a href=\"listBoard.do?page=" + (nPage + pagedGroup));
			if ((searchKey != null) && (searchVal != null)) {
				sb.append("?searchKey=" + searchKey + "&searchVal=" + searchVal);
			}			
			sb.append("\">");
			sb.append("[다음 " + pagedGroup + "개]");
			sb.append("</a>");
		}
		model.put("pagedLink", sb.toString());	// 모델 객체에 삽입
		
		return new ModelAndView(this.successView, model);
	}

}
