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.web.ApplicationController;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.service.CrdnService;
import cokr.xit.fims.cvlc.CrdnCvlcpt;
import cokr.xit.fims.cvlc.service.CrdnCvlcptService;
import cokr.xit.foundation.data.DataObject;

/**민원답변대상자료 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/cvlc/cvlc02 로 접근할 수 있다.
 * @author leebj
 */
public class Cvlc02Controller extends ApplicationController {

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

	public class METHOD_URL {
		public static final String
		civilComplaintAnswerTargetDataMain = "/010/main.do",
		selectCivilComplaintAnswerTargetDataList = "/030/list.do",
		updateAnswerTargetProcessStatus = "/030/update.do",
		executeDisabledTagInfoInquire = "/040/update.do",
		answerPreviewInfo = "/060/info.do",
		updateAnswerContent = "/060/update.do"
		;
	}

	@Resource(name="crdnService")
	private CrdnService crdnService;

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

	/** 민원 답변 대상 자료 메인화면을 연다.
	 * @return fims/cvlc/cvlc02010-main
	 */
	public ModelAndView civilComplaintAnswerTargetDataMain() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/cvlc/cvlc02010-main");
		mav.addObject("pageName", "cvlc02010-main");

		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM017","FIM022", "FIM026", "FIM054");
		mav.addObject("FIM017List", commonCodes.get("FIM017"));
		mav.addObject("FIM054List", commonCodes.get("FIM054"));
		addCodes(commonCodes, mav, "FIM022");
		addCodes(commonCodes, mav, "FIM026");
		return mav;
	}

	/**민원답변대상자료 목록을 조회한다.<br />
	 * {@link CrdnService#getCrackdownList(CrdnQuery)} 참고
	 * @param query 민원답변대상자료 목록 조회 조건
	 * @return jsonView
	 */
	public ModelAndView selectCivilComplaintAnswerTargetDataList(CrdnQuery query) {
		query.setGridType("answerTargetData");
		setFetchSize(query);
		return setCollectionInfo(new ModelAndView("jsonView"), crdnCvlcptService.getCivilComplaintList(query), "");
	}

	/** 민원답변내용 미리보기 화면을 연다.
	 * @return
	 */
	public ModelAndView answerPreviewInfo(HttpServletRequest hReq, CrdnQuery query) {
		boolean json = jsonResponse();
		ModelAndView mav = new ModelAndView(json ? "jsonView" : "fims/cvlc/cvlc02060-info");

		mav.addObject("pageName", "cvlc02060-info");
		String urlTaskSeCd = hReq.getServletPath().split("/")[1];
		mav.addObject("taskSeCd", urlTaskSeCd);

		query.setTaskSeCd(urlTaskSeCd);
		query.setCurrentInstitute(currentUser().getInstitute());
		query.setCurrentUserAcnt(currentUser().getAccount());
		DataObject cvlcptInfo = crdnCvlcptService.getAnswerPreview(query);
		mav.addObject("cvlcptInfo", json ? cvlcptInfo : toJson(cvlcptInfo));

		return mav;
	}

	/**지정한 민원 자료의 답변 내용을 저장한다.
	 * @param crdnCvlcpt 단속민원 정보
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView updateAnswerContent(CrdnCvlcpt crdnCvlcpt) {

		boolean saved = false;

		saved = crdnCvlcptService.updateAnswerContent(crdnCvlcpt);

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

	/**민원 답변 대상 자료의 처리 상태를 변경한다.
	 * @param crdnCvlcpt 단속민원 정보
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView updateAnswerTargetProcessStatus(String processType, String... crdnIDs) {

		boolean saved = false;
		String currentInstitute = currentUser().getInstitute();
		String currentUserAcnt = currentUser().getAccount();
		saved = crdnCvlcptService.updateAnswerTargetProcessStatus(processType, currentInstitute, currentUserAcnt, crdnIDs);

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

	/**지정한 단속자료에 대해 표지정보조회 실행 후 장애인차량이 아닌 건은 서손 처리한다.
	 * @param crdnIDs 단속ID 목록
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView executeDisabledTagInfoInquire(String... crdnIDs) {
		boolean saved = false;

		saved = crdnCvlcptService.executeDisabledTagInfoInquire(crdnIDs);

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