package nlib.col.web;

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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import nlib.cmm.NlibCommonController;
import nlib.cmm.service.NlibProperty;
import nlib.col.service.CollectionService;
import nlib.col.service.CollectionVO;
import nlib.col.service.ReadService;
import nlib.util.StringUtil;

/**
 * <pre>
 * @Class Name : ReadController.java
 * 
 * @Description : 방문열람을 관리하는 컨트롤러 
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 10. 27.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 10. 27.
 * @version 1.0
 *
 */
@Controller
public class ReadController extends NlibCommonController {
	
	private static final Logger log = LoggerFactory.getLogger(ReadController.class);
	
	@Resource(name = "readService")
	private ReadService readService;
	

	@Resource(name = "collectionService")
	private CollectionService collectionService;


    /**
     * 열람신청을 등록하는 화면을 표출한다. 
     * 
     * @param request
     * @param readVO
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value={"/read/confirmReadFromCart.do", "/read/confirmRead.do"})
    public String confirmRent(HttpServletRequest request, CollectionVO readVO, ModelMap model) throws Exception {
    	
    	String retJsp = "nlib/read/confirmRead";
    	boolean fromCart = request.getServletPath().contains("Cart.do");
    	
    	String mbInfoId = getMbInfoId(request);
    	
    	if(StringUtil.isEmpty(mbInfoId)) {
    		model.addAttribute("message", "로그인 후, 이용해 주시기 바랍니다.");
    		return retJsp;
    	}
    	
    	if(readVO == null || StringUtil.isEmpty(readVO.getSelItems())) {
    		model.addAttribute("message", "대출(예약)신청할 정보가 없습니다.");
    		return retJsp;
    	}
    	
		String selItems = readVO.getSelItems();
		
		List<CollectionVO> list = collectionService.makeInfoListOfCollectionVO(selItems, mbInfoId, NlibProperty.getString("thumbnail.image.url.small"));
		List<HashMap<String, String>> listDates = readService.listDates();
		
		model.addAttribute("readVO", readVO);
    	model.addAttribute("list", list);
    	model.addAttribute("listDates", listDates);
    	if(fromCart) model.addAttribute("fromCart", fromCart ? "Y" : "N");
    	
    	return retJsp;
    }
    
    /**
     * 열람신청을 등록한다. 
     * 
     * @param request
     * @param readVO
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value={"/read/insertReadItemsFromCart.do", "/read/insertReadItems.do"})
    public String insertReadItems(HttpServletRequest request, CollectionVO readVO, ModelMap model) throws Exception {
    	
    	boolean fromCart = request.getServletPath().contains("Cart.do");
    	String retJsp = "nlib/read/insertReadItems";
    	String mbInfoId = getMbInfoId(request);
    	
    	if(StringUtil.isEmpty(mbInfoId)) {
    		model.addAttribute("message", "로그인 후, 이용해 주시기 바랍니다.");
    		return retJsp;
    	}
    	
    	if(readVO == null || StringUtil.isEmpty(readVO.getSelItems())) {
    		model.addAttribute("message", "대출(예약)신청할 정보가 없습니다.");
    		return retJsp;
    	}

    	if(StringUtil.isEmpty(readVO.getMngOrgCd())) {
    		model.addAttribute("message", "관리문화원 정보가 없습니다.");
    		return retJsp;
    	}
    	
		if(StringUtil.isEmpty(readVO.getHopeReqPridDtmFrom() )) {
			model.addAttribute("message", "방문희망일 시작일자를 확인할 수 없습니다.");
    		return retJsp;
		}
		
		if(StringUtil.isEmpty(readVO.getHopeReqPridDtmTo() )) {
			model.addAttribute("message", "방문희망일 종료일자를 확인할 수 없습니다.");
    		return retJsp;
		}
		
		if(readVO.getHopeReqPridDtmFrom().compareTo(readVO.getHopeReqPridDtmTo()) > 0) {
			model.addAttribute("message", "방문희망일 종료일자는 시작일자 이후이어야 합니다.");
    		return retJsp;
		}
    	
    	
		String selItems = readVO.getSelItems();
		
		List<CollectionVO> list = collectionService.makeListOfCollectionVO(selItems);
		
		list = readService.insertReadItems(list, mbInfoId, readVO);
		list = collectionService.setDetailInfoForList(list, mbInfoId, NlibProperty.getString("thumbnail.image.url.small"));
		
		model.addAttribute("readVO", readVO);
    	model.addAttribute("list", list);
    	if(fromCart) model.addAttribute("fromCart", fromCart ? "Y" : "N");
    	
    	return retJsp;
    }
    
}
