package cokr.xit.fims.stat.web;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.code.CommonCode;
import cokr.xit.base.user.SigunguQuery;
import cokr.xit.base.user.dao.UserMapper;
import cokr.xit.base.user.service.bean.SigunguBean;
import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.base.FimsUser;
import cokr.xit.fims.cmmn.service.bean.StngBean;
import cokr.xit.fims.stat.CodeSubsetInfo;
import cokr.xit.fims.stat.GroupInfo;
import cokr.xit.fims.stat.NumberValueInfo;
import cokr.xit.fims.stat.Stat;
import cokr.xit.fims.stat.StatQuery;
import cokr.xit.fims.stat.service.StatService;
import cokr.xit.foundation.data.DataObject;

/**통계 관리 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/stat 로 접근할 수 있다.
 * @author leebj
 */
@Controller
@RequestMapping(name="통계", value=Stat01Controller.CLASS_URL)
public class Stat01Controller extends ApplicationController {

	public static final String CLASS_URL = "/stat/stat01";

	public class METHOD_URL {
		public static final String
		getStatistics                = "/010/info.do",
		comprehensiveStatisticsMain  = "/010/main.do",
		crackdownStatisticsMain      = "/020/main.do",
		levyStatisticsMain           = "/030/main.do",
		sendStatisticsMain           = "/040/main.do",
		receivingStatisticsMain      = "/050/main.do"
		;
	}

	@Resource(name="statService")
	private StatService statService;

	@Resource(name="sigunguBean")
	protected SigunguBean sigunguBean;

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

	@Resource(name = "stngBean")
	private StngBean stngBean;

	/**
	 * @return
	 */
	@RequestMapping(name="통계 요청", value=METHOD_URL.getStatistics)
	public ModelAndView getStatistics(StatQuery statQuery) {
		ModelAndView mav = new ModelAndView("jsonView");

		//FimsUser currentUser = (FimsUser) currentUser().getUser();
		//String sggCd = currentUser.getOrgID();
		//statQuery.setSggCd(sggCd);

		List<String> codeGroup = new ArrayList<String>();

		List<GroupInfo> groupInfoList = statQuery.getGroupInfoList();
		if(groupInfoList != null) {
			for(int i=0; i < groupInfoList.size(); i++) {
				GroupInfo groupInfo = groupInfoList.get(i);
				String ctgrType = ifEmpty(groupInfo.getCtgrType(), () -> "");
				String dtlCtgr = ifEmpty(groupInfo.getDtlCtgr(), () -> "");
				if(ctgrType.equals("code")) {
					codeGroup.add(dtlCtgr);
				} else if(ctgrType.equals("otherCode")) {

					if(dtlCtgr.equals("VLTN_CD")) {
						codeGroup.add("FIM054");
						codeGroup.add("FIM004");
						codeGroup.add("FIM005");
						codeGroup.add("FIM006");
						codeGroup.add("FIM061");
						codeGroup.add("FIM064");
					}

				}
			}
		}

		List<NumberValueInfo> numberValueInfoList = statQuery.getNumberValueInfoList();
		if(numberValueInfoList != null) {
			for(int i=0; i < numberValueInfoList.size(); i++) {
				NumberValueInfo numberValueInfo = numberValueInfoList.get(i);
				String ctgrType = ifEmpty(numberValueInfo.getSeperatorCtgrType(), () -> "");
				String dtlCtgr = ifEmpty(numberValueInfo.getSeperatorDtlCtgr(), () -> "");
				if(ctgrType.equals("code")) {
					codeGroup.add(dtlCtgr);
				} else if(ctgrType.equals("otherCode")) {

					if(dtlCtgr.equals("VLTN_CD")) {
						codeGroup.add("FIM054");
						codeGroup.add("FIM004");
						codeGroup.add("FIM005");
						codeGroup.add("FIM006");
						codeGroup.add("FIM061");
						codeGroup.add("FIM064");
					}

				}
			}
		}

		Map<String, List<CommonCode>> commonCodes = new HashMap<String, List<CommonCode>>();
		if(codeGroup.size() > 0) {
			commonCodes = getCodesOf(codeGroup.toArray(new String[codeGroup.size()]));
		}

		if(statQuery.getCodeSubsetInfoList() != null && !statQuery.getCodeSubsetInfoList().isEmpty()) {
			for(CodeSubsetInfo subsetInfo : statQuery.getCodeSubsetInfoList()) {
				CommonCode newCode = new CommonCode();
				newCode.setGroupID(subsetInfo.getSourceGroup());
				newCode.setCode(subsetInfo.getTargetCode());
				newCode.setValue(subsetInfo.getTargetCodeVal());
				commonCodes.get(subsetInfo.getSourceGroup()).add(newCode);
			}
		}

		Stat stat = statService.getStatistics(statQuery, commonCodes);

		mav.addObject("stat", stat);
		return mav;
	}

	/** 종합 통계 메인 화면을 연다.
	 * @return fims/stat/stat01010-main
	 */
	@RequestMapping(name="종합 통계 메인", value=METHOD_URL.comprehensiveStatisticsMain)
	public ModelAndView comprehensiveStatisticsMain() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/stat/stat01010-main");
		mav.addObject("pageName", "stat01010");

		FimsUser currentUser = (FimsUser) currentUser().getUser();
		String sggCd = currentUser.getOrgID();
		mav.addObject("sggCd", sggCd);
		DataObject sgg = sigunguBean.getSigunguList(new SigunguQuery().setSggIDs(sggCd)).get(0);
		String sggNm = sgg.string("SGG_NM");
		mav.addObject("sggNm", sggNm);

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

		return mav;
	}

	/** 단속 통계 메인 화면을 연다.
	 * @return fims/stat/stat01020-main
	 */
	@RequestMapping(name="단속 통계 메인", value=METHOD_URL.crackdownStatisticsMain)
	public ModelAndView crackdownStatisticsMain() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/stat/stat01020-main");
		mav.addObject("pageName", "stat01020");

		FimsUser currentUser = (FimsUser) currentUser().getUser();
		String sggCd = currentUser.getOrgID();
		mav.addObject("sggCd", sggCd);
		DataObject sgg = sigunguBean.getSigunguList(new SigunguQuery().setSggIDs(sggCd)).get(0);
		String sggNm = sgg.string("SGG_NM");
		mav.addObject("sggNm", sggNm);

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

		return mav;
	}


	/** 부과 통계 메인 화면을 연다.
	 * @return fims/stat/stat01030-main
	 */
	@RequestMapping(name="부과 통계 메인", value=METHOD_URL.levyStatisticsMain)
	public ModelAndView levyStatisticsMain() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/stat/stat01030-main");
		mav.addObject("pageName", "stat01030");

		FimsUser currentUser = (FimsUser) currentUser().getUser();
		String sggCd = currentUser.getOrgID();
		mav.addObject("sggCd", sggCd);
		DataObject sgg = sigunguBean.getSigunguList(new SigunguQuery().setSggIDs(sggCd)).get(0);
		String sggNm = sgg.string("SGG_NM");
		mav.addObject("sggNm", sggNm);

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

		return mav;
	}

	/** 발송 통계 메인 화면을 연다.
	 * @return fims/stat/stat01040-main
	 */
	@RequestMapping(name="발송 통계 메인", value=METHOD_URL.sendStatisticsMain)
	public ModelAndView sendStatisticsMain() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/stat/stat01040-main");
		mav.addObject("pageName", "stat01040");

		FimsUser currentUser = (FimsUser) currentUser().getUser();
		String sggCd = currentUser.getOrgID();
		mav.addObject("sggCd", sggCd);
		DataObject sgg = sigunguBean.getSigunguList(new SigunguQuery().setSggIDs(sggCd)).get(0);
		String sggNm = sgg.string("SGG_NM");
		mav.addObject("sggNm", sggNm);

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

		return mav;
	}

	/** 수납 통계 메인 화면을 연다.
	 * @return fims/stat/stat01050-main
	 */
	@RequestMapping(name="수납 통계 메인", value=METHOD_URL.receivingStatisticsMain)
	public ModelAndView receivingStatisticsMain() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/stat/stat01050-main");
		mav.addObject("pageName", "stat01050");

		FimsUser currentUser = (FimsUser) currentUser().getUser();
		String sggCd = currentUser.getOrgID();
		mav.addObject("sggCd", sggCd);
		DataObject sgg = sigunguBean.getSigunguList(new SigunguQuery().setSggIDs(sggCd)).get(0);
		String sggNm = sgg.string("SGG_NM");
		mav.addObject("sggNm", sggNm);

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

		return mav;
	}
}
