package nlib.cmm.web;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
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.NotificationService;
import nlib.user.service.NlibLoginVO;
import nlib.util.StringUtil;

/**
 * <pre>
 * @Class Name : MainController.java
 * 
 * @Description : 메인화면에 표시할 정보를 처리하는 Controller 클래스
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 6. 22.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 6. 22.
 * @version 1.0
 *
 */
@Controller
public class MainController extends NlibCommonController {

	private static final Logger log = LoggerFactory.getLogger(MainController.class);
	
	@Resource(name="notificationService")
	private NotificationService notificationService; 

	@RequestMapping( {"/index.do"} )
	public String setContent(
			HttpServletRequest req, 
			HttpServletResponse res, 
			Authentication authentication, 
			ModelMap model) throws Exception {

		// 캐쉬된 Reqeust 히스토리 삭제 
		RequestCache cache = new HttpSessionRequestCache();
		cache.removeRequest(req, res);
		
		log.debug((authentication == null ? "손님 메인 접속" : authentication.getName() + " 메인 접속"));
		
		// 알림 확인
		String mbInfoId = getMbInfoId(req);
		int unreadNotiCnt = 0;
		if(!isAlerted(req) && StringUtil.isNotEmpty(mbInfoId)) {
			unreadNotiCnt = notificationService.countUnreadNotification(mbInfoId);
			setAlerted(req, true);
		}
		
		model.addAttribute("unreadNotiCnt", unreadNotiCnt);
		
		return "nlib/cmm/home";
	}
	
}