package cokr.xit.fims.crdn.service.bean;

import java.util.List;

import javax.annotation.Resource;

import org.assertj.core.util.Arrays;
import org.springframework.stereotype.Component;

import cokr.xit.fims.crdn.Crdn;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.dao.CrdnTeamMapper;
import cokr.xit.fims.crdn.dao.ExmptnVhclMapper;
import cokr.xit.fims.crdn.dao.GlobalStngMapper;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.data.DataObject;

/** 단속 설정 Bean
 * @author leebj
 */
@Component("crdnStngBean")
public class CrdnStngBean extends AbstractComponent {

	public static String[] SAFE_TAG_AREA = {"03","06","07"}; //안전표지구역

	public static String[] CAR_GROUP1 = {"12","14","15","21"}; //승합자동차등
	public static String[] CAR_GROUP2 = {"11","13"}; //승용자동차등
	public static String[] CAR_GROUP3 = {"31"}; //이륜자동차등

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

	@Resource(name = "crdnTeamMapper")
	private CrdnTeamMapper crdnTeamMapper;

	@Resource(name = "exmptnVhclMapper")
	private ExmptnVhclMapper exmptnVhclMapper;

	/**잔액을 계산한다.
	 * @param state 대장 자료 상태
	 * @param advntceAmt 사전통지금액
	 * @param ffnlgAmt 과태료금액
	 * @param minusAmt 감경금액
	 * @param adtnAmt 가산금액
	 * @param rcvmtAmt 수납금액
	 * @return 잔액
	 */
	public int getBalance(String state, int advntceAmt, int ffnlgAmt, int minusAmt, int adtnAmt, int rcvmtAmt) {
		int result = 0;
		if(Integer.parseInt(state) < Integer.parseInt("51")) {
			result = advntceAmt - rcvmtAmt - minusAmt;
		} else if(state.equals("71") ||
				state.equals("72") ||
				state.equals("73") ||
				state.equals("74") ||
				state.equals("75") ||
				state.equals("76") ||
				state.equals("80") ||
				state.equals("81") ||
				state.equals("82") ||
				state.equals("83") ||
				state.equals("84") ||
				state.equals("85")) {

		} else {
			result = ffnlgAmt - rcvmtAmt - minusAmt + adtnAmt;
		}

		return result;
	}

	/**기본금액을 계산한다.
	 * @param crdn 단속 정보
	 * @return 최초금액, 사전통지금액
	 */
	public int[] getBasicAmt(Crdn crdn) {
		int[] amt = {0, 0};

		String taskSeCd = crdn.getTaskSeCd();

		CrdnQuery query = new CrdnQuery();
		query.setTaskSeCd(crdn.getTaskSeCd());
		query.setAplcnYmd(crdn.getCrdnYmd());

		String ffnlgCarmdlCd = crdn.getFfnlgCarmdlCd();
		if(taskSeCd.equals("PVS") || taskSeCd.equals("BPV") || taskSeCd.equals("TPV")) {
			if(ifEmpty(ffnlgCarmdlCd, () -> "").equals("")) {
				return amt;
			}
		}

		if(taskSeCd.equals("PVS")) {
			query.setAmtDcsnCdGroupId("FIM055");
			String crdnSpareaCd = crdn.getCrdnSpareaCd();
			if(Arrays.asList(SAFE_TAG_AREA).contains(crdnSpareaCd)) {
				query.setAmtDcsnCd("02");
			} else {

				String crdnTm = crdn.getCrdnTm();
				boolean workTimeYn = Integer.parseInt(crdnTm) >= 80000 && Integer.parseInt(crdnTm) <= 200000;

				if(crdnSpareaCd.equals("01") && workTimeYn) {
					query.setAmtDcsnCd("03");
				} else if((crdnSpareaCd.equals("02") || crdnSpareaCd.equals("05")) && workTimeYn) {
					query.setAmtDcsnCd("04");
				} else {
					query.setAmtDcsnCd("01");
				}
			}

			query.setDtlAmtDcsnCdGroupId("FIM056");
			if(Arrays.asList(CAR_GROUP1).contains(ffnlgCarmdlCd)) {
				query.setDtlAmtDcsnCd("01");
			}
			if(Arrays.asList(CAR_GROUP2).contains(ffnlgCarmdlCd)) {
				query.setDtlAmtDcsnCd("02");
			}

			int ffnlgCrdnAmt = Integer.parseInt(ifEmpty(globalStngMapper.selectBasicAmt(query), () -> "0"));

			if(ffnlgCrdnAmt > 0 && crdn.getOvtimeYn().equals("Y")) {
				ffnlgCrdnAmt += 10000;
			}

			amt[0] = ffnlgCrdnAmt;
			amt[1] = (int)(ffnlgCrdnAmt * 0.8);

		} else if(taskSeCd.equals("BPV")) {

			query.setAmtDcsnCdGroupId("FIM056");
			if(Arrays.asList(CAR_GROUP1).contains(ffnlgCarmdlCd)) {
				query.setAmtDcsnCd("01");
			}
			if(Arrays.asList(CAR_GROUP2).contains(ffnlgCarmdlCd)) {
				query.setAmtDcsnCd("02");
			}
			if(Arrays.asList(CAR_GROUP3).contains(ffnlgCarmdlCd)) {
				query.setAmtDcsnCd("03");
			}

			int ffnlgCrdnAmt = Integer.parseInt(ifEmpty(globalStngMapper.selectBasicAmt(query), () -> "0"));
			amt[0] = ffnlgCrdnAmt;
			amt[1] = ffnlgCrdnAmt;

		} else if(taskSeCd.equals("DPV")) {

			query.setAmtDcsnCdGroupId("FIM006");
			query.setAmtDcsnCd(crdn.getVltnCd());

			int ffnlgCrdnAmt = Integer.parseInt(ifEmpty(globalStngMapper.selectBasicAmt(query), () -> "0"));
			amt[0] = ffnlgCrdnAmt;
			amt[1] = (int)(ffnlgCrdnAmt * 0.8);

		} else if(taskSeCd.equals("ECA")) {

			query.setAmtDcsnCdGroupId("FIM061");
			query.setAmtDcsnCd(crdn.getVltnCd());

			int ffnlgCrdnAmt = Integer.parseInt(ifEmpty(globalStngMapper.selectBasicAmt(query), () -> "0"));
			amt[0] = ffnlgCrdnAmt;
			amt[1] = (int)(ffnlgCrdnAmt * 0.8);

		} else if(taskSeCd.equals("TPV")) {

			query.setAmtDcsnCdGroupId("FIM057");

			if(ffnlgCarmdlCd.equals("21")) {
				query.setAmtDcsnCd("01");
				query.setDtlAmtDcsnCdGroupId("FIM059");

				String vltnNmtm = crdn.getVltnNmtm();
				if(vltnNmtm == null || vltnNmtm.equals("") || vltnNmtm.equals("0") || vltnNmtm.equals("1")) {
					query.setDtlAmtDcsnCd("01");
				} else if(vltnNmtm.equals("2")) {
					query.setDtlAmtDcsnCd("02");
				} else {
					query.setDtlAmtDcsnCd("03");
				}
			}


			if(ffnlgCarmdlCd.equals("13") || ffnlgCarmdlCd.equals("14")) {
				query.setAmtDcsnCd("02");
				query.setDtlAmtDcsnCdGroupId("FIM058");
				//query.setDtlAmtDcsnCd(업종);   //  TODO : 차주 업종
			}
			;

			int ffnlgCrdnAmt = Integer.parseInt(ifEmpty(globalStngMapper.selectBasicAmt(query), () -> "0"));
			amt[0] = ffnlgCrdnAmt;
			boolean isPenaltyAmt = (crdn.getFfnlgCarmdlCd().equals("13") || crdn.getFfnlgCarmdlCd().equals("14")); //과징금
			if(isPenaltyAmt) {
				amt[1] = (ffnlgCrdnAmt);
			} else {
				amt[1] = (int)(ffnlgCrdnAmt * 0.8);
			}

		}

		return amt;
	}

	/**단속팀 목록을 조회한다.
	 * @param query 단속팀 목록 조회조건
	 * @return 단속팀 목록
	 */
	public List<DataObject> selectTeamList(CrdnQuery query){
		return crdnTeamMapper.selectTeamList(query);
	}

	/**단속팀 정보를 조회한다.
	 * @param teamId 단속팀 ID
	 * @return 단속팀 정보
	 */
	public DataObject selectTeamInfo(String teamId) {
		return crdnTeamMapper.selectTeamInfo(teamId);
	}

}
