package cokr.xit.fims.chongno;

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

import javax.annotation.Resource;

import cokr.xit.base.code.CommonCode;
import cokr.xit.base.code.dao.CodeMapper;
import cokr.xit.fims.crdn.Crdn;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.excl.LevyExcl;
import cokr.xit.fims.levy.LevyQuery;
import cokr.xit.fims.levy.dao.LevyMapper;
import cokr.xit.fims.mngt.service.bean.TaskProcessor;
import cokr.xit.foundation.data.DataObject;

public class ChongnoTaskProcessor extends TaskProcessor {
	private static final String TASK_SE_CD = "RVS";

	public ChongnoTaskProcessor() {
		vltnCdGrps.set(TASK_SE_CD, "FIM090");
	}

	@Resource(name = "levyMapper")
	private LevyMapper levyMapper;
	@Resource(name = "codeMapper")
	private CodeMapper codeMapper;

	@Override
	public String[] getExtraCdGrps(String taskSeCd) {
		if (TASK_SE_CD.equals(taskSeCd))
			return new String[] {"FIM002"};
		return super.getExtraCdGrps(taskSeCd);
	}

	@Override
	public boolean isPreImposable(String taskSeCd) {
		return TASK_SE_CD.equals(taskSeCd)
			|| super.isPreImposable(taskSeCd);
	}

	@Override
	public int[] getBasicAmt(Crdn crdn) {
		String taskSeCd = crdn.getTaskSeCd();
		if (TASK_SE_CD.equals(taskSeCd)) {
			String crdnYmd = crdn.getCrdnYmd();
			String sggCd = crdn.getSggCd();
			List<DataObject> levies = levyMapper.selectPrevLevyList(
				new LevyQuery()
				   .setSchVhrno(crdn.getVhrno())
				   .setSchVltnCd(crdn.getVltnCd())
				   .setRtpyrId(crdn.getRtpyrId())
				   .setDate(crdnYmd)
				   .setSggCd(sggCd)
				   .setTaskSeCd(taskSeCd)
			);
			String vcount = "0" + Math.min(levies.size() + 1, 3); // 위반횟수

			CrdnQuery query = new CrdnQuery()
				.setAplcnYmd(crdnYmd)
				.setAmtDcsnCdGroupId("FIM090")
				.setAmtDcsnCd("01")
				.setDtlAmtDcsnGroupId("FIM091")
				.setDtlAmtDcsnCd(vcount)
				.setTaskSeCd(taskSeCd)
				.setSggCd(sggCd);
			int amt = toInt(crdnStngMapper.selectBasicAmt(query));

			DataObject taskInfo = getTaskInfo(sggCd, taskSeCd);
			return new int[] {amt, amt * (100 - toInt(taskInfo.get("ADVNTCE_RDCRT"))) / 100};
		}
		return super.getBasicAmt(crdn);
	}

	@Override
	public String getSealCd3(String taskSeCd, String sndngSeCd) {
		if (TASK_SE_CD.equals(taskSeCd)) {
			if ("11".equals(sndngSeCd))
				return "E";
		}
		return super.getSealCd3(taskSeCd, sndngSeCd);
	}

	@Override
	public String getNtrisInterface(String taskSeCd) {
		return switch (taskSeCd) {
		case TASK_SE_CD -> "sntris";
		default -> super.getNtrisInterface(taskSeCd);
		};
	}

	@Override
	public String getNoticeDelivery(String taskSeCd) {
		if (TASK_SE_CD.equals(taskSeCd))
			return "postplus";
		else
			return super.getNoticeDelivery(taskSeCd);
	}

	private int capacity = -1;

	@Override
	public Map<Crdn, LevyExcl> getExcluded(List<Crdn> crdns) {
		Map<Crdn, LevyExcl> excluded = super.getExcluded(crdns);
		Crdn first = crdns.get(0);
		if (!TASK_SE_CD.equals(first.getTaskSeCd()))
			return excluded;

		if (capacity < 0) {
			CommonCode code = codeMapper.getCode("FIM022", "114");
			capacity = toInt(notEmpty(code, "capacity").getEtc1());
		}
		if (capacity == 0)
			return excluded;

		for (Crdn crdn: crdns) {
			if (excluded.containsKey(crdn)) continue;

			int vhclCpct = crdn.getVhclCpct();
			if (vhclCpct == 0) {// 정원미확인
//				excluded.put(crdn, newLevyExcl(crdn, "1", "115"));
			} else if (vhclCpct < capacity) // 정원미만
				excluded.put(crdn, newLevyExcl(crdn, "1", "114"));
		}

		return excluded;
	}

	@Override
	public LevyExcl getLevyExcl(Crdn crdn) {
		LevyExcl excl = super.getLevyExcl(crdn);
		if (!TASK_SE_CD.equals(crdn.getTaskSeCd()))
				return excl;
		if (excl != null)
			return excl;

		if (capacity < 0) {
			CommonCode code = codeMapper.getCode("FIM022", "114");
			capacity = toInt(notEmpty(code, "capacity").getEtc1());
		}
		if (capacity == 0)
			return null;

		int vhclCpct = crdn.getVhclCpct();
		if (vhclCpct == 0) // 정원미확인
			return null; //newLevyExcl(crdn, "1", "115");

		if (vhclCpct < capacity) // 정원미만
			return newLevyExcl(crdn, "1", "114");

		return null;
	}
}