
package nlib.col.web;

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

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import nlib.cmm.NlibCommonController;
import nlib.cmm.service.CodeService;
import nlib.cmm.service.NlibProperty;
import nlib.col.service.CollectionService;
import nlib.restful.service.DataApiReqVO;
import nlib.restful.service.DataApiResVO;
import nlib.util.StringUtil;

@Controller
public class CollectionController extends NlibCommonController
{
	private static final Logger log = LoggerFactory.getLogger(CollectionController.class);
	static final String DEFUALT_PAGE_SIZE = NlibProperty.getProperty("list.paging.page.size");
	
	@Resource(name = "codeService") 
	private CodeService codeService;
	
	@Resource(name="collectionService")
	private CollectionService collectionService;

	/**
	 * 소장자료 화면을 호출한다.
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("/collection/listItems.do")
	public String listItems(HttpServletRequest req, Authentication authentication, @RequestParam Map<String, String> paramMap, ModelMap model) throws Exception {
		String searchKeyword = paramMap.get("searchKeyword");
		String pageIndex = StringUtil.getString(paramMap.get("pageIndex"), "1");
		String pageSize  = StringUtil.getString(paramMap.get("pageSize") , DEFUALT_PAGE_SIZE);

		// 페이징 사이즈 코드 목록 조회 (DB방식으로 변경 2021.08.06 KNKIM)
		List<HashMap<String,String>> pageSizeCodes = codeService.listCodes("PAGE_SIZE_OPTION");
		
		// 반환 정보 
    	model.addAttribute("pageIndex"    , pageIndex);
    	model.addAttribute("pageSize"     , pageSize);
    	model.addAttribute("searchKeyword", searchKeyword);
    	model.addAttribute("pageSizeCodes", pageSizeCodes);
		return "nlib/collection/listItems";
	}
	
	/**
	 * 소장자료를 조회한다.
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/collection/listItemsAjax.do")
	public ResponseEntity<String> listItemsAjax(HttpServletRequest req, 
			Authentication authentication, @RequestBody Map<String, String> paramMap)  {

		String searchKeyword = paramMap.get("searchKeyword");
		String pageIndex = StringUtil.getString(paramMap.get("pageIndex"), "1");
		String pageSize  = StringUtil.getString(paramMap.get("pageSize") , DEFUALT_PAGE_SIZE);
		
		//-------------------------------
		// REQ VO 구성 
		//-------------------------------
    	DataApiReqVO reqVO = new DataApiReqVO();
    	reqVO.setAuthKey(createAuthKey(req, authentication));
    	reqVO.setPageIndex(pageIndex);
    	reqVO.setPageSize(pageSize);
    	reqVO.addInfoItem("searchKeyword", searchKeyword);

    	// 요청 
    	DataApiResVO resVO = collectionService.listItems(reqVO);
    	
		//-------------------------------
		// JSON변환 응답 처리
		//-------------------------------
    	// JS-GRID 페이징 처리를 포함한 응답값 처리
    	// {data: [{...}], 
    	//  itemsCount: 255
    	// }
    	HashMap<String, Object> retMap = new HashMap<String, Object>();
    	retMap.put("data", resVO.getList());
    	retMap.put("itemsCount", resVO.getRecordTotCount());
    	
    	return makeResponseEntityJson(retMap);
	}

	public ModelMap insertInterest(HttpServletRequest req)  {
		return null;
	}

	public ModelMap reserveItem(HttpServletRequest req)  {
		return null;
	}

	public ModelMap viewOriginalCopy(HttpServletRequest req)  {
		return null;
	}

	public ModelMap requestViewingItem(HttpServletRequest req)  {
		return null;
	}
	/**
	 * 소장자료 상세 내용 조회한다.
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("/collection/selectItemInfo.do")
	public String selectItemInfo(HttpServletRequest req, Authentication authentication, @RequestParam Map<String, String> paramMap, ModelMap model)  {
		// 목록 이동시 전달할 매개변수 
				String searchKeyword = paramMap.get("searchKeyword");
				String pageIndex = paramMap.get("pageIndex");
				String pageSize = paramMap.get("pageSize");
				String message = paramMap.get("message");
				
				// 게시물 번호 
				String articleNo = paramMap.get("articleNo");
				
				log.debug("selectQnA > pageIndex = " + pageIndex);
				log.debug("selectQnA > pageSize = " + pageSize);
				log.debug("selectQnA > searchKeyword = " + searchKeyword);
				log.debug("selectQnA > articleNo = " + articleNo);
				log.debug("selectQnA > message = " + message);

				//-------------------------------
				// REQ VO 구성 
				//-------------------------------
		    	DataApiReqVO reqVO = new DataApiReqVO();
		    	reqVO.setAuthKey(createAuthKey(req, authentication));
		    	reqVO.setPageIndex(pageIndex);
		    	reqVO.setPageSize(pageSize);
		    	reqVO.addInfoItem("boardNo", "QNA");       // 게시판ID
		    	reqVO.addInfoItem("articleNo", articleNo); // 게시물번호

		    	// 요청 
		    	DataApiResVO resVO = collectionService.selectItemInfo(reqVO);
		    	
		    	// 반환 정보 
		    	model.addAttribute("articleNo"    , articleNo);
		    	model.addAttribute("article"      , resVO.getInfo()); // 게시물 상세 
		    	model.addAttribute("message"      , message);
		    	model.addAttribute("pageIndex"    , pageIndex);
		    	model.addAttribute("pageSize"     , pageSize);
		    	model.addAttribute("searchKeyword", searchKeyword);

				return "nlib/collection/selectItemInfo";
	}
	/**
	 * 관심 자료 화면을 호출한다.
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("/collection/listInterests.do")
	public String listInterests(HttpServletRequest req, Authentication authentication, @RequestParam Map<String, String> paramMap, ModelMap model) throws Exception  {
		String searchKeyword = paramMap.get("searchKeyword");
		String pageIndex = StringUtil.getString(paramMap.get("pageIndex"), "1");
		String pageSize  = StringUtil.getString(paramMap.get("pageSize") , DEFUALT_PAGE_SIZE);

		// 페이징 사이즈 코드 목록 조회 (DB방식으로 변경 2021.08.06 KNKIM)
		List<HashMap<String,String>> pageSizeCodes = codeService.listCodes("PAGE_SIZE_OPTION");
		
		// 반환 정보 
    	model.addAttribute("pageIndex"    , pageIndex);
    	model.addAttribute("pageSize"     , pageSize);
    	model.addAttribute("searchKeyword", searchKeyword);
    	model.addAttribute("pageSizeCodes", pageSizeCodes);
		return "nlib/collection/listInterests";
	}

	/**
	 * 관심 자료를 조회한다.
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/collection/listInterestsAjax.do")
	public ResponseEntity<String> listInterestsAjax(HttpServletRequest req, 
			Authentication authentication, @RequestBody Map<String, String> paramMap)  {

		String searchKeyword = paramMap.get("searchKeyword");
		String pageIndex = StringUtil.getString(paramMap.get("pageIndex"), "1");
		String pageSize  = StringUtil.getString(paramMap.get("pageSize") , DEFUALT_PAGE_SIZE);
		
		//-------------------------------
		// REQ VO 구성 
		//-------------------------------
    	DataApiReqVO reqVO = new DataApiReqVO();
    	reqVO.setAuthKey(createAuthKey(req, authentication));
    	reqVO.setPageIndex(pageIndex);
    	reqVO.setPageSize(pageSize);
    	reqVO.addInfoItem("searchKeyword", searchKeyword);

    	// 요청 
    	DataApiResVO resVO = collectionService.listInterests(reqVO);
    	
		//-------------------------------
		// JSON변환 응답 처리
		//-------------------------------
    	// JS-GRID 페이징 처리를 포함한 응답값 처리
    	// {data: [{...}], 
    	//  itemsCount: 255
    	// }
    	HashMap<String, Object> retMap = new HashMap<String, Object>();
    	retMap.put("data", resVO.getList());
    	retMap.put("itemsCount", resVO.getRecordTotCount());
    	
    	return makeResponseEntityJson(retMap);
	}
	public ModelMap deleteInterest(HttpServletRequest req)  {
		return null;
	}


}