package cokr.xit.fims.cmmn;

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

import cokr.xit.base.code.CommonCode;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.data.DataObject;


public class CodeConverter extends AbstractComponent {
	private Map<String, List<CommonCode>> commonCodes = new HashMap<String, List<CommonCode>>();

	private List<DataObject> sggList = new ArrayList<DataObject>();
	private List<DataObject> deptList = new ArrayList<DataObject>();
	private List<DataObject> vltnList = new ArrayList<DataObject>();
	private List<DataObject> userList = new ArrayList<DataObject>();
	private List<DataObject> teamList = new ArrayList<DataObject>();

	public CodeConverter(Map<String, List<CommonCode>> commonCodes){
		this.commonCodes = commonCodes;
	}

	/** 코드값으로 코드를 반환한다.
	 * @param codeGroupName 코드그룹명, value 코드값
	 * @return 코드
	 */
	public String valueToCode(String codeGroupName, String value){
		String result = "";

		List<CommonCode> commonCodeList = commonCodes.get(codeGroupName);

		for (int i = 0, size = commonCodeList != null ? commonCodeList.size() : 0; i < size; i++) {
			if (value.replaceAll(Matcher.quoteReplacement(" "), "").equals(commonCodeList.get(i).getValue().replaceAll(Matcher.quoteReplacement(" "), ""))) {
				result = commonCodeList.get(i).getCode();
			}
		}
		return result;
	}

	/** 코드로 코드값을 반환한다.
	 * @param codeGroupName 코드그룹명, code 코드
	 * @return 코드값
	 */
	public String codeToValue(String codeGroupName, String code){
		String result = "";

		List<CommonCode> commonCodeList = commonCodes.get(codeGroupName);

		for (int i = 0, size = commonCodeList != null ? commonCodeList.size() : 0; i < size; i++) {
			if (code.equals(commonCodeList.get(i).getCode())) {
				result = commonCodeList.get(i).getValue();
			}
		}
		return result;
	}

	/** 코드나 코드명이 비어있을 경우 변환하여 값을 채운다.
	 * @param dataObject 맵 데이터, codeGroupName 코드그룹명, codeName 코드가 저장된 키 이름, valueName 코드값이 저장된 키 이름
	 * @return
	 */
	public void fillIfEmpty(DataObject dataObject, String codeGroupName, String codeName, String valueName) {
		if (!isEmpty(dataObject.get(valueName)) && isEmpty(dataObject.get(codeName))) {
			dataObject.put(codeName, this.valueToCode(codeGroupName, dataObject.string(valueName)));
		} else if (isEmpty(dataObject.get(valueName)) && !isEmpty(dataObject.get(codeName))) {
			dataObject.put(valueName, this.codeToValue(codeGroupName, dataObject.string(codeName)));
		}
	}

	/** 위반코드로 위반항목명을 반환한다.
	 * @param codeGroup 업무구분코드, vltnCd 위반항목코드
	 * @return 위반항목명
	 */
	public String vltnCdToValue(String codeGroup, String vltnCd) {
		if (codeGroup == null)
			throw new RuntimeException("위반코드 변환 오류");

		List<CommonCode> commonCodeList = commonCodes.get(codeGroup);

		String result = "";
		for (int i = 0; i < commonCodeList.size(); i++) {
			if (vltnCd.equals(commonCodeList.get(i).getCode())) {
				result = commonCodeList.get(i).getValue();
			}
		}

		return result;
	}

	private List<DataObject> getList(String type) {
		return switch (type) {
		case "SGG_CD" -> sggList;
		case "DEPT_CD" -> deptList;
		case "VLTN_ID" -> vltnList;
		case "USER_ID" -> userList;
		case "TEAM_ID" -> teamList;
		default -> null;
		};
	}

	public void putOtherCode(String type, DataObject dataobject){
		List<DataObject> list = getList(type);
		if (list != null)
			list.add(dataobject);
	}

	public String uniqOtherCodeToValue(String type, String uniqId) {
		String result = "";

		List<DataObject> otherCodeList = getList(type);
		if (otherCodeList == null)
			return result;

		for (int i = 0; i < otherCodeList.size(); i++) {
			if (uniqId.equals(otherCodeList.get(i).string(type))) {
				result = otherCodeList.get(i).string("NAME");
			}
		}

		return result;
	}
}