package nlib.cmm.web;

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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 org.springframework.web.servlet.mvc.support.RedirectAttributes;

import nlib.cmm.NlibCommonController;
import nlib.cmm.service.NotificationService;
import nlib.cmm.service.NotificationVO;
import nlib.cmm.service.PagingVO;
import nlib.cmm.service.NlibProperty;
import nlib.restful.service.DataApiReqVO;
import nlib.restful.service.DataApiResVO;
import nlib.util.StringUtil;

/**
 * <pre>
 * @Class Name : NotificationController.java
 * 
 * @Description : 사용자 홈페이지 알림 안내 컨트롤러 
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 9. 6.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 9. 6.
 * @version 1.0
 *
 */
@Controller
public class NotificationController extends NlibCommonController {
	
	private static final Logger log = LoggerFactory.getLogger(NotificationController.class);
	
	static final int DEFUALT_PAGE_SIZE = NlibProperty.getInt("list.paging.page.size", 10);
	
	@Resource(name="notificationService")
	private NotificationService notificationService;


	/**
	 * 알림 목록 화면을 표시한다. 
	 * 
	 * @param req
	 * @return
	 */
	@Secured("ROLE_USER")
	@RequestMapping("/cmm/listNotifications.do")
	public String listNotifications(
			HttpServletRequest req, 
			@RequestParam Map<String, String> paramMap,
			ModelMap model
			) throws Exception {

		String mbInfoId = getMbInfoId(req);
		int pageIndex = StringUtil.toNumber(paramMap.get("pageIndex"), 1);
		int pageSize  = StringUtil.toNumber(paramMap.get("pageSize"), DEFUALT_PAGE_SIZE);
		
		log.debug("listNotifications > pageIndex = " + pageIndex);
		log.debug("listNotifications > pageSize = " + pageSize);
    	
		NotificationVO searchNotificationVO = new NotificationVO();
		searchNotificationVO.setPageIndex(pageIndex);
		searchNotificationVO.setPageSize(pageSize);
		searchNotificationVO.setRecvUserId(mbInfoId);
		List<NotificationVO> list = notificationService.listNotifications(searchNotificationVO);
		
		int totRecordCount = (list == null) ? 0 : list.size();
		searchNotificationVO.setTotRecordCount(totRecordCount);
		
		model.addAttribute("notificationList", list);
		model.addAttribute("searchNotificationVO", searchNotificationVO);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("pageIndex", pageIndex);
		
    	
    	return "nlib/cmm/listNotifications";
	}

	/**
	 * 알림 목록 조회한다.
	 * 
	 * @param req
	 * @return
	 */
	@Secured("ROLE_USER")
	@RequestMapping(value="/cmm/listNotificationsAjax.do")
	public ResponseEntity<String> listNotificationsAjax(HttpServletRequest req, 
			Authentication authentication, @RequestBody(required = false) Map<String, String> paramMap) throws Exception {

		String mbInfoId = getMbInfoId(req);
		if(paramMap == null) paramMap = new HashMap<String, String>();
		int pageIndex = StringUtil.toNumber(paramMap.get("pageIndex"), 1);
		int pageSize  = StringUtil.toNumber(paramMap.get("pageSize"), DEFUALT_PAGE_SIZE);
		
		log.debug("listNotificationsAjax > pageIndex = " + pageIndex);
		log.debug("listNotificationsAjax > pageSize = " + pageSize);
		

		NotificationVO searchNotificationtVO = new NotificationVO();
		searchNotificationtVO.setPageIndex(pageIndex);
		searchNotificationtVO.setPageSize(pageSize);
		searchNotificationtVO.setRecvUserId(mbInfoId);
		List<NotificationVO> list = notificationService.listNotifications(searchNotificationtVO);
		
		int totRecordCount = notificationService.countNotifications(mbInfoId);
		
		//-------------------------------
		// JSON변환 응답 처리
		//-------------------------------
    	// JS-GRID 페이징 처리를 포함한 응답값 처리
    	// {data: [{...}], 
    	//  itemsCount: 255
    	// }
    	HashMap<String, Object> retMap = new HashMap<String, Object>();
    	retMap.put("data", list);
    	retMap.put("itemsCount", totRecordCount);
    	
    	PagingVO pageVO = new PagingVO();
	  	pageVO.setPagingVO(totRecordCount, pageIndex, pageSize);
	  	retMap.put("pagingPageIndex"     , pageVO.getPageIndex());
	  	retMap.put("pagingTotRecordCount", pageVO.getTotRecordCount());
	  	retMap.put("pagingStartPage"     , pageVO.getStartPage());
	  	retMap.put("pagingEndPage"       , pageVO.getEndPage());
	  	retMap.put("pagingLastPage"      , pageVO.getLastPage());
    	
    	return makeResponseEntityJson(retMap);
	}


	/**
	 * 알림 상세 내용 조회한다.
	 * 
	 * @param req
	 * @return
	 */
	@Secured("ROLE_USER")
	@RequestMapping("/cmm/selectNotificationAjax.do")
	public ResponseEntity<String> selectNotificationAjax(HttpServletRequest req, Authentication authentication, @RequestBody Map<String, String> paramMap) throws Exception {

		String notiId = paramMap.get("notiId"); // 알림ID 
		String pageIndex = paramMap.get("pageIndex");
		String pageSize = paramMap.get("pageSize");
		
		log.debug("selectNotificationAjax > notiId = " + notiId);
		
		NotificationVO paramNotiVO = new NotificationVO();
		paramNotiVO.setNotiId(notiId);
		paramNotiVO.setRecvUserId(getMbInfoId(req));
		paramNotiVO.setPageIndex(pageIndex);
		paramNotiVO.setPageSize(pageSize);
		
		// 읽음처리 및 상세내용 조회 
		NotificationVO notiVO = notificationService.selectNotification(paramNotiVO);

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

}