package cokr.xit.fims.crdn.web;

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

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

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.code.CommonCode;
import cokr.xit.base.file.FileInfo;
import cokr.xit.base.file.web.FileInfoFactory;
import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.crdn.Crdn;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.dao.CrdnInstMapper;
import cokr.xit.fims.crdn.dao.GlobalStngMapper;
import cokr.xit.fims.crdn.service.CrdnService;
import cokr.xit.fims.crdn.service.CrdnStngService;
import cokr.xit.foundation.data.DataObject;

/**단속 자료 등록 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/crdn/crdn05 로 접근할 수 있다.
 * @author leebj
 */
public class Crdn05Controller extends ApplicationController {

	public static final String CLASS_URL = "/crdn/crdn05";

	public class METHOD_URL {
		public static final String
		crackdownDataRegistrationMain = "/010/main.do",
		getFileRegistrationScreen = "/020/info.do",
		getTodayCrdnDataList = "/010/list.do",
		getManualRegistrationScreen = "/030/info.do",
		createCrdnDataByManual = "/030/create.do"
		;
	}

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

	@Resource(name="crdnStngService")
	private CrdnStngService crdnStngService;

	@Resource(name="crdnInstMapper")
	private CrdnInstMapper crdnInstMapper;

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

	/** 단속 자료 등록 메인화면을 연다.
	 * @return fims/crdn/crdn05010-main
	 */
	public ModelAndView crackdownDataRegistrationMain() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/crdn/crdn05010-main");
		mav.addObject("pageName", "crdn05010-main");

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

	/**단속자료 파일 등록 팝업화면을 반환한다.
	 * @param
	 * @return fims/crdn/crdn05020-info
	 */
	public ModelAndView getFileRegistrationScreen() {
		return new ModelAndView("fims/crdn/crdn05020-info");
	}

	/**당일 등록한 단속자료 목록을 조회한다.
	 * {@link CrdnService#getCrackdownList(CrdnQuery)} 참고
	 * @param query 단속자료 목록 조회 조건
	 * @return jsonView
	 */
	public ModelAndView getTodayCrdnDataList(CrdnQuery query) {
		setFetchSize(query);
		query.setGridType("todayInsert");
		query.setCurrentUserId(currentUser().getId());
		return setCollectionInfo(new ModelAndView("jsonView"),crdnService.getCrackdownList(query),"");
	}

	/**단속자료 수기 등록 팝업화면을 반환한다.
	 * @param
	 * @return fims/crdn/crdn05030-info
	 */
	public ModelAndView getManualRegistrationScreen(HttpServletRequest hReq) {

		ModelAndView mav = new ModelAndView("fims/crdn/crdn05030-info");
		mav.addObject("pageName", "crdn05030-info");
		String urlTaskSeCd = hReq.getServletPath().split("/")[1];
		mav.addObject("taskSeCd", urlTaskSeCd);
		mav.addObject("sggCd", globalStngMapper.selectSggCdForCurrentUser());

		mav.addObject("stdgNmList", globalStngMapper.selectStdgNmListForCurrentUser());

		String vltnByTask = "";

		switch(urlTaskSeCd) {
			case "PVS" -> { vltnByTask = "WEB002"; }
			case "BPV" -> { vltnByTask = "FIM005"; }
			case "DPV" -> { vltnByTask = "FIM006"; }
			case "ECA" -> { vltnByTask = "FIM061"; }
		}
		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM011","FIM053","LVS005",vltnByTask);
		mav.addObject("FIM011List", commonCodes.get("FIM011"));
		mav.addObject("FIM053List", commonCodes.get("FIM053"));
		mav.addObject("LVS005List", commonCodes.get("LVS005"));
		mav.addObject("VLTNList", commonCodes.get(vltnByTask));

		CrdnQuery query = new CrdnQuery();
		query.setSggCd(globalStngMapper.selectSggCdForCurrentUser());
		query.setTaskSeCd(urlTaskSeCd);
		List<DataObject> teamList = crdnStngService.getTeamList(query);

		switch(urlTaskSeCd) {
			case "PVS" -> {
				commonCodes = getCodesOf("FIM002","FIM007","FIM009");
				mav.addObject("FIM002List", commonCodes.get("FIM002"));
				mav.addObject("FIM007List", commonCodes.get("FIM007"));
				mav.addObject("FIM009List", commonCodes.get("FIM009"));
				mav.addObject("TeamList", teamList);
			}
			case "BPV" -> {
				commonCodes = getCodesOf("FIM002","FIM009");
				mav.addObject("FIM002List", commonCodes.get("FIM002"));
				mav.addObject("FIM009List", commonCodes.get("FIM009"));
				mav.addObject("TeamList", teamList);
			}
			case "DPV" -> {
				commonCodes = getCodesOf("FIM034");
				mav.addObject("FIM034List", commonCodes.get("FIM034"));
			}
			case "ECA" -> { }
		}

		return mav;
	}

	/**단속 대장을 수기 등록한다.
	 * @param hReq 등록 요청, crdn 단속 대장 정보, newFileList 업로드한 이미지 파일
	 * @return jsonView
	 * <pre><code> {
	 *     "saved": 등록되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView createCrdnDataByManual(HttpServletRequest hReq, Crdn crdn, MultipartFile[] newFileList) {
		boolean saved = false;

		crdn.setSggCd(globalStngMapper.selectSggCdForCurrentUser());

		crdn.setCrdnRegSeCd("01");
		crdn.setCrdnInptSeCd("03");//TODO : 사용자 기관의 시청,구청 구분

		List<FileInfo> fileInfoList = new ArrayList<FileInfo>();
		if(newFileList != null && newFileList.length > 0) {
			fileInfoList = new FileInfoFactory().makeFileInfos(null, newFileList);
		}

		String doWarning = ifEmpty(hReq.getParameter("doWarning"), () -> "");
		if(doWarning.equals("on")) {
			crdn.setCrdnSttsCd("83");
		}
		Map<String, Object> nonQueryRequest = new HashMap<String, Object>();
		crdn.setCvlcptLinkYn("N");
		saved = crdnService.create(nonQueryRequest , crdn, fileInfoList);


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