package cokr.xit.fims.cvlc.web;

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

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

import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.code.CommonCode;
import cokr.xit.base.user.ManagedUser;
import cokr.xit.base.user.dao.UserMapper;
import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.dao.GlobalStngMapper;
import cokr.xit.fims.cvlc.CrdnCvlcpt;
import cokr.xit.fims.cvlc.service.CrdnCvlcptService;
import cokr.xit.foundation.UserInfo;
import cokr.xit.foundation.data.DataObject;

/**민원신고답변 문구 관리 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/cvlc/cvlc05 로 접근할 수 있다.
 * @author leebj
 */
public class Cvlc05Controller extends ApplicationController {

	public static final String CLASS_URL = "/cvlc/cvlc05";

	public class METHOD_URL {
		public static final String
		cvlcptAnswerWordsManageMain = "/010/main.do",
		getCvlcptAnswerWordsInfo = "/010/info.do",
		saveCvlcptAnswerWordsInfo = "/010/save.do",
		removeCvlcptAnswerWordsInfo = "/010/remove.do"
		;
	}

	@Resource(name="crdnCvlcptService")
	private CrdnCvlcptService crdnCvlcptService;

	@Resource(name="globalStngMapper")
	private GlobalStngMapper globalStngMapper;

	@Resource(name="userMapper")
	private UserMapper userMapper;

	/** 민원신고 답변 문구 관리 메인화면을 연다.
	 * @return fims/cvlc/cvlc05010-main
	 */
	public ModelAndView cvlcptAnswerWordsManageMain() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/cvlc/cvlc05010-main");
		mav.addObject("pageName", "cvlc05010-main");
		mav.addObject("module", "type='module'");

		mav.addObject("currentUserName", UserInfo.current().getName());
		ManagedUser currentUser = userMapper.getUser(UserInfo.current().getAccount(), UserInfo.current().getInstitute());
		mav.addObject("currentUserTelno", currentUser.getPhoneNo());

		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM054");
		mav.addObject("FIM054List", commonCodes.get("FIM054"));

		return mav;
	}

	/** 민원신고 답변 문구 설정을 조회한다.
	 * @return fims/cvlc/cvlc05010-info
	 */
	public ModelAndView getCvlcptAnswerWordsInfo(CrdnQuery crdnQuery) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("jsonView");

		String currentSggCd = globalStngMapper.selectSggCdForCurrentUser();
		crdnQuery.setSggCd(currentSggCd);
		Map<String, Object> info = crdnCvlcptService.getCvlcptAnswerWordsInfo(crdnQuery);

		DataObject basicInfo = (DataObject) info.get("basic");
		List<DataObject> levyCaseList = (List<DataObject>) info.get("levyCaseList");
		List<DataObject> warningCaseList = (List<DataObject>) info.get("warningCaseList");
		List<DataObject> nonlevyCaseList = (List<DataObject>) info.get("nonlevyCaseList");

		if(basicInfo == null){
			mav.addObject("headerCn", "");
			mav.addObject("footerCn", "");
		} else {
			mav.addObject("headerCn", basicInfo.string("HEADER_CN"));
			mav.addObject("footerCn", basicInfo.string("FOOTER_CN"));
		}

		mav.addObject("levyCaseList", levyCaseList);
		mav.addObject("warningCaseList", warningCaseList);
		mav.addObject("nonlevyCaseList", nonlevyCaseList);

		return mav;
	}

	/** 민원신고 답변 문구 설정을 저장한다.
	 * @param hReq 저장 요청
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView saveCvlcptAnswerWordsInfo(HttpServletRequest hReq, CrdnCvlcpt crdnCvlcpt) {
		boolean saved = false;

		String currentSggCd = globalStngMapper.selectSggCdForCurrentUser();
		crdnCvlcpt.setSggCd(currentSggCd);

		String saveData = hReq.getParameter("saveData");
		if(saveData.equals("basic") || saveData.equals("body")) {
			saved = crdnCvlcptService.saveAnswerWordsInfo(saveData, crdnCvlcpt);
		}

		return new ModelAndView("jsonView")
				.addObject("saved", saved);
	}

	/** 미부과처리건 답변 문구를 삭제한다.
	 * @param ansWordsId 답변 문구 ID
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView removeCvlcptAnswerWordsInfo(String ansWordsId) {
		boolean saved = false;

		saved = crdnCvlcptService.removeCvlcptAnswerWordsInfo(ansWordsId);

		return new ModelAndView("jsonView")
				.addObject("saved", saved);
	}
}
