package kccf.ton.web;

import java.util.ArrayList;
import java.util.List;

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

import kccf.ton.service.LocalToonService;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cmm.Constant;
import cmm.service.CmmActivityHstVO;
import cmm.service.CmmAmsAssetMediaVO;
import cmm.service.CmmAmsAssetsVO;
import cmm.service.CmmAmsDocTableOfContentsVO;
import cmm.service.CmmCmsContentsVO;
import cmm.service.CmmMetaVO;
import cmm.service.CmmService;
import cmm.service.ExtService;
import cmm.util.CmmUtil;
import cmm.util.DataMap;
//import egovframework.com.cmm.EgovMessageSource;
import egovframework.rte.fdl.cmmn.exception.EgovBizException;
import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;



@Controller
public class LocalToonController {
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(LocalToonController.class);

	@Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
	    
//    @Resource(name="egovMessageSource")
//    EgovMessageSource egovMessageSource;
	
	@Resource(name = "localToonService")
    private LocalToonService localToonService;

	@Resource(name = "extService")
	private ExtService extService;
	
   	@Resource(name = "cmmService")
    private CmmService cmmService;
	
    @RequestMapping(value="/ton/localToonList.do")
   	public String selectLocalToonList (
   			@ModelAttribute("cmmAmsAssetMedia") CmmAmsAssetMediaVO cmmAmsAssetMediaVO
   			, ModelMap model
   			, HttpServletRequest req
			, HttpServletResponse res
   			) throws EgovBizException  {
    	
    	model.addAttribute("paramVO", cmmAmsAssetMediaVO);
    	
    	/**** paging ****/
       	PaginationInfo paginationInfo = new PaginationInfo();
   		paginationInfo.setCurrentPageNo(cmmAmsAssetMediaVO.getPageIndex());
   		paginationInfo.setRecordCountPerPage(cmmAmsAssetMediaVO.getPageUnit());
   		paginationInfo.setPageSize((CmmUtil.isMobile(req)) ? cmmAmsAssetMediaVO.getMobilePageSize() : cmmAmsAssetMediaVO.getPageSize());
   		
   		cmmAmsAssetMediaVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
   		cmmAmsAssetMediaVO.setLastIndex(paginationInfo.getLastRecordIndex());
   		cmmAmsAssetMediaVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());
   		/**** paging ****/

   		
   		// 키
   		cmmAmsAssetMediaVO.setSessionId(CmmUtil.getCookieId(req));
   		// 이달의 툰
   		model.addAttribute("resultTopList", localToonService.selectLocalToonTopList(cmmAmsAssetMediaVO));
   		// 지역문화툰		
   		int totCnt = localToonService.selectLocalToonListTotCnt(cmmAmsAssetMediaVO);
   		paginationInfo.setTotalRecordCount(totCnt);	
   		model.addAttribute("resultList", localToonService.selectLocalToonList(cmmAmsAssetMediaVO));
   		model.addAttribute("totCnt", totCnt);
   		model.addAttribute("paginationInfo", paginationInfo);
   		
   		// 최근열람툰
   		CmmActivityHstVO cmmActivityHstVO = new CmmActivityHstVO();
   		cmmActivityHstVO.setTargetId(CmmUtil.getCookieId(req)); 
		cmmActivityHstVO.setUaType(Constant.UA_TYPE_W);
		cmmActivityHstVO.setActivityType(Constant.ACTIVITY_TYPE_O);
		cmmActivityHstVO.setContentType(Constant.CONTENT_TYPE_T);
		model.addAttribute("openPageList", localToonService.selectLocalToonOpenPageList(cmmActivityHstVO));
		model.addAttribute("openPageHotList", localToonService.selectLocalToonOpenPageHotList(cmmActivityHstVO));
		
		// 메타태그정보
    	CmmMetaVO metaVO = new CmmMetaVO();
    	metaVO.setTitle("지역문화툰");
    	metaVO.setDescription("지역문화와 관련된 다양한 이야기와 정보로 재미있게 구성된 웹툰을 만나실 수 있습니다.");
    	metaVO.setUrl(CmmUtil.getPropertyStr(Constant.KCCF_HOME_KEY) + "/ton/localToonList.do");
    	metaVO.setImage(CmmUtil.getPropertyStr(Constant.KCCF_HOME_KEY) + "/images/kccf/bg/bg-subvisual-toon.jpg");
    	model.addAttribute("metaResult", metaVO);
    	
   		return "kccf/ton/localToonList.kccf";
   	}
    
    @RequestMapping(value="/ton/openPageList-data.do")
   	public ModelAndView selectOpenPageListAjax (
   			@ModelAttribute("cmmAmsAssetMedia") CmmAmsAssetMediaVO cmmAmsAssetMediaVO
				, ModelMap model
				, HttpServletRequest req
				, HttpServletResponse res
   			) throws EgovBizException {

    	ModelAndView modelAndView = new ModelAndView();
    	modelAndView.setViewName("jsonView");

    	CmmActivityHstVO cmmActivityHstVO = new CmmActivityHstVO();
   		cmmActivityHstVO.setTargetId(CmmUtil.getCookieId(req)); 
		cmmActivityHstVO.setUaType(Constant.UA_TYPE_W);
		cmmActivityHstVO.setActivityType(Constant.ACTIVITY_TYPE_O);
		cmmActivityHstVO.setContentType(Constant.CONTENT_TYPE_T);
    	
    	DataMap responseMap = new DataMap();

    	responseMap.put("contentsList", localToonService.selectLocalToonOpenPageList(cmmActivityHstVO));
    	model.addAttribute("responseData", responseMap);
    	return modelAndView;
   	}
    
    @RequestMapping(value="/ton/localToonDetailList.do")
   	public String selectLocalToonDetailList (
   			@ModelAttribute("cmmAmsAssetMedia") CmmAmsAssetMediaVO cmmAmsAssetMediaVO
   			, ModelMap model
   			, HttpServletRequest req
			, HttpServletResponse res
   			) throws EgovBizException  {
    	model.addAttribute("paramVO", cmmAmsAssetMediaVO);
    	
    	model.addAttribute("cookieId", CmmUtil.getCookieId(req));
		CmmUtil.setNfsPathModelMap(req,model);
		
		cmmAmsAssetMediaVO.setMediaId(cmmAmsAssetMediaVO.getContentId());
    	CmmAmsAssetMediaVO result = localToonService.selectLocalToonDetail(cmmAmsAssetMediaVO);
    	model.addAttribute("result", result);
    	
    	/**** paging ****/
       	PaginationInfo paginationInfo = new PaginationInfo();
   		paginationInfo.setCurrentPageNo(cmmAmsAssetMediaVO.getPageIndex());
   		paginationInfo.setRecordCountPerPage(cmmAmsAssetMediaVO.getPageUnit());
   		paginationInfo.setPageSize((CmmUtil.isMobile(req)) ? cmmAmsAssetMediaVO.getMobilePageSize() : cmmAmsAssetMediaVO.getPageSize());
   		
   		cmmAmsAssetMediaVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
   		cmmAmsAssetMediaVO.setLastIndex(paginationInfo.getLastRecordIndex());
   		cmmAmsAssetMediaVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());
   		/**** paging ****/
   		// 키
   		cmmAmsAssetMediaVO.setSessionId(CmmUtil.getCookieId(req));
    	
    	cmmAmsAssetMediaVO.setAssetId(result.getAssetId());
    	int totCnt = localToonService.selectLocalToonDetailListTotCnt(cmmAmsAssetMediaVO);
    	paginationInfo.setTotalRecordCount(totCnt);	
    	List<DataMap> resultList = localToonService.selectLocalToonDetailList(cmmAmsAssetMediaVO);
    	model.addAttribute("resultList", resultList);
   		model.addAttribute("totCnt", totCnt);
   		model.addAttribute("paginationInfo", paginationInfo);
   		
   	    // 최근열람툰
   		CmmActivityHstVO cmmActivityHstVO = new CmmActivityHstVO();
   		cmmActivityHstVO.setTargetId(CmmUtil.getCookieId(req)); 
		cmmActivityHstVO.setUaType(Constant.UA_TYPE_W);
		cmmActivityHstVO.setActivityType(Constant.ACTIVITY_TYPE_O);
		cmmActivityHstVO.setContentType(Constant.CONTENT_TYPE_T);

		cmmActivityHstVO.setParameter1(cmmAmsAssetMediaVO.getAssetId());
		cmmActivityHstVO.setParameter2(cmmAmsAssetMediaVO.getGroupId());
		
		model.addAttribute("openPageDetailList", localToonService.selectLocalToonOpenPageDetailList(cmmActivityHstVO));
   		
   		return "kccf/ton/localToonDetailList.kccf";
   	}
    
	@RequestMapping(value="/ton/localToonDetail.do")
 	public String selectLocalToonDetail (
 			 @ModelAttribute("cmmAmsAssetMedia") CmmAmsAssetMediaVO cmmAmsAssetMediaVO
 			, BindingResult bindingResult
 			, ModelMap model
 			, HttpServletRequest req
 			) throws EgovBizException {
		
		model.addAttribute("cookieId", CmmUtil.getCookieId(req));
		CmmUtil.setNfsPathModelMap(req,model);
		
		cmmAmsAssetMediaVO.setSessionId(CmmUtil.getCookieId(req));
		cmmAmsAssetMediaVO.setMediaId(cmmAmsAssetMediaVO.getContentId());
    	CmmAmsAssetMediaVO result = localToonService.selectLocalToonDetail(cmmAmsAssetMediaVO);
    	model.addAttribute("result", result);
    	
    	cmmAmsAssetMediaVO.setSessionId(CmmUtil.getCookieId(req));
    	cmmAmsAssetMediaVO.setAssetId(result.getAssetId());
    	cmmAmsAssetMediaVO.setGroupId(result.getGroupId());
    	List<DataMap> resultList = localToonService.selectLocalToonDetailListAll(cmmAmsAssetMediaVO);
    	model.addAttribute("resultList", resultList);
    	int defaultCount = 0;
    	for(int i=0; i<resultList.size(); i++){
    		if(result.getMediaId().equals(resultList.get(i).getString("id"))){
    			defaultCount = i;
    		}
			
		}
    	model.addAttribute("defaultCount", defaultCount);
    	
    	DataMap paramMap = DataMap.getParamMap(req);
    	paramMap.setString("jsessionId", CmmUtil.getCookieId(req)); 
    	paramMap.setString("contentId", cmmAmsAssetMediaVO.getContentId()); 
    	paramMap.setString("contentType", Constant.CONTENT_TYPE_T); 
    	cmmService.insertCmmActivityHst(paramMap);
    	
    	// 메타태그정보
    	CmmMetaVO metaVO = new CmmMetaVO();
    	if (result != null) {
    		if (StringUtils.isNotEmpty(result.getTitle())) metaVO.setTitle(result.getTitle());
    		if (StringUtils.isNotEmpty(result.getSummary())) metaVO.setDescription(result.getSummary());
    	}
    	if (result != null) {
    		if (StringUtils.isNotEmpty(result.getMediaId())) metaVO.setUrl(CmmUtil.getPropertyStr(Constant.KCCF_HOME_KEY) + "/ton/localToonDetail.do?contentId="+result.getMediaId());
    		if (StringUtils.isNotEmpty(result.getMainImg())) metaVO.setImage(CmmUtil.getAssetImageUrl(result.getMainImg()));
    	}
    	model.addAttribute("metaResult", metaVO);
    	
    	model.addAttribute("paramVO", cmmAmsAssetMediaVO); // 파라미터
    	
		return "kccf/ton/localToonDetail.kccf";
	}
}
