/*********************************************************************************************************
 * 프로그램명 : MItem.java 프로그램설명 : 집계문서의 표의 각각의 셀에 대한 정보 처리 작성자 : 강원중 작성일 : 2003.12.10 변경일 : 2004.09.16
 **********************************************************************************************************/

package kr.co.kihyun.moumi.doc.table.item;

import kr.co.kihyun.beans.entity.TotItemType;
import kr.co.kihyun.lang.MNumber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MItem {
        
        private static final Logger LOG = LoggerFactory.getLogger(MItem.class);
        
	// 아이템 종류별 상수 값
	@Deprecated
	public final static int TITLE = TotItemType.TITLE.ordinal();
	@Deprecated
	public final static int CHAR = TotItemType.CHAR.ordinal();
	@Deprecated
	public final static int INT = TotItemType.INT.ordinal();
	@Deprecated
	public final static int DATE = TotItemType.DATE.ordinal();
	@Deprecated
	public final static int SPCHAR = TotItemType.SPCHAR.ordinal();

	public final static String LIST_DELIM = "!@!";

	private static final String[] typeNameList = { TotItemType.TITLE.toString(), TotItemType.CHAR.toString(),
			TotItemType.INT.toString(), TotItemType.DATE.toString(), TotItemType.SPCHAR.toString() };

	public static String getTypeName(int index) {
		if (index < 0 || index >= typeNameList.length) {
			return "";
		}

                //v2 27.Public 메소드로부터 반환된 Private배열_CWE-495 : Update by  YOUNGJUN,CHO
		//return typeNameList[index];
                String ret[] = new String[typeNameList.length];
                
                for (int i = 0 ; i < typeNameList.length ; i++) {
                    ret[i] = typeNameList[i];
                }
		
                return ret[index];
                //====================================================================
	}

	public static int getTypeNum(String typeName) {
		for (int i = 0; i < typeNameList.length; i++) {
			if (typeNameList[i].equals(typeName)) {
				return i;
			}
		}
		return 0;
	}

	// ////////////////////////////////////////// 동적인 표에서 사용 ///////////////////////////////////////////////////////

	// 동적 집계문서에서 사용
	public static String[] getInNameList(String inBodyForm, int rowCount, int colCount) {
		String[] startName = getStartNameList(inBodyForm, colCount);
		int endNum = getEndNum(inBodyForm);

		return getInNameList(startName, endNum, rowCount, colCount);
	}

	// 동적 집계문서에서 사용
	public static String[] getInNameList(String inBodyForm, int endNum, int rowCount, int colCount) {
		String[] startName = getStartNameList(inBodyForm, colCount);
		return getInNameList(startName, endNum, rowCount, colCount);
	}

	public static String[] getInNameList(String[] startName, int endNum, int rowCount, int colCount) {
		String[] name = null;
		name = new String[rowCount * colCount];
		int index = 0;

		for (int r = 0, c = 0; r < rowCount; r++) {
			for (c = 0; c < colCount; c++) {
				name[index] = startName[c] + (endNum + r);
				index++;
			}
		}

		return name;
	}

	public static String[] getInNameList(String[] startName, String[] startNum, int rowCount, int colCount) {
		String[] name = null;
		name = new String[rowCount * colCount];
		int index = 0;

		for (int r = 0, c = 0; r < rowCount; r++) {
			for (c = 0; c < colCount; c++) {
				name[index] = startName[c] + startNum[c];
				index++;
			}
		}

		return name;
	}

	public static String[] getInNameList(String[] startName, String[] startNum, int[] tableColNumList,
			String[] tableRowList) {
		String[] name = null;

		if (tableColNumList == null || tableRowList == null)
			return null;

		// 입력창의 총 갯수
		int size = 0;
		for (int i = 0; i < tableColNumList.length && i < tableRowList.length; i++) {
			size = size + tableColNumList[i] * Integer.parseInt(tableRowList[i]);
		}
		name = new String[size];

		int index = 0;
		int here = 0;
		for (int i = 0; i < tableRowList.length && i < tableColNumList.length; i++) {
			for (int stp = 0; stp < Integer.parseInt(tableRowList[i]); stp++) {
				if (stp != 0) {
					int gap = getGap(startNum, here, here + tableColNumList[i]);
					for (int nn = here; nn < startNum.length; nn++) {
						startNum[nn] = Integer.toString(Integer.parseInt(startNum[nn]) + gap);
					}
				}
				for (int p = here; p < here + tableColNumList[i] && p < startName.length && p < startNum.length && index < name.length; p++) {
					name[index] = startName[p] + startNum[p];
					index++;
				}
			}
			here = here + tableColNumList[i];
		}
		return name;
	}

	public static int getGap(String[] startNum, int start, int end) {
		int top = -1;
		int bottom = 100000;
		for (int i = start; i < end; i++) {
			if (bottom > Integer.parseInt(startNum[i]))
				bottom = Integer.parseInt(startNum[i]);
			if (top < Integer.parseInt(startNum[i]))
				top = Integer.parseInt(startNum[i]);
		}
		if (top - bottom < 0)
			return 0;
		return (top - bottom + 1);
	}

	// item 이름 생성
	public static String[] getStartNameList(String inBodyForm, int inCount) {
		String[] itemNameList = null;

		try {
			if (inBodyForm == null) {
				return itemNameList;
			}

			int inputIndex = 0;
			int selectIndex = 0;
			int textareaIndex = 0;
			int quoteIndex = 0;
			String stName = "";
			int index = 0;

			String inputTag = "<input type='text' name='";
			String selectTag = "<select name='";
			String textAreaTag = "<textarea name='";

			int inputLength = inputTag.length();
			int selectLength = selectTag.length();
			int textAreaLength = textAreaTag.length();

			String tmp[] = inBodyForm.split("<td");
			itemNameList = new String[inCount];

			for (int i = 0; (i < tmp.length) && (index < inCount); i++) {
				inputIndex = tmp[i].indexOf(inputTag);
				selectIndex = tmp[i].indexOf(selectTag);// 콤보
				textareaIndex = tmp[i].indexOf(textAreaTag);// text area

				if (inputIndex >= 0) {
					quoteIndex = tmp[i].indexOf("'", inputIndex + inputLength);
					if (quoteIndex == -1)
						break;
					stName = tmp[i].substring(inputIndex + inputLength, quoteIndex).replaceAll("[0-9]", "");
					stName = stName.trim(); // A~Z
					stName = stName.toUpperCase();
					itemNameList[index] = stName;
                    //System.out.println("inputIndex::::test1"+inputIndex);
					index++;
				} else if (selectIndex >= 0) {
					quoteIndex = tmp[i].indexOf("'", selectIndex + selectLength);
					if (quoteIndex == -1)
						break;

					stName = tmp[i].substring(selectIndex + selectLength, quoteIndex).replaceAll("[0-9]", "");
					stName = stName.trim(); // A~Z
					stName = stName.toUpperCase();
					itemNameList[index] = stName;
                     //System.out.println("inputIndex::::test2"+inputIndex);
					index++;
				} else if (textareaIndex >= 0) {
					quoteIndex = tmp[i].indexOf("'", textareaIndex + textAreaLength);
					if (quoteIndex == -1)
						break;

					stName = tmp[i].substring(textareaIndex + textAreaLength, quoteIndex).replaceAll("[0-9]", "");
					stName = stName.trim(); // A~Z
					stName = stName.toUpperCase();
					itemNameList[index] = stName;
                    //System.out.println("inputIndex::::test3"+inputIndex);
					index++;
				}
			}
			return itemNameList;
		} catch (Exception ex) {
			ex.printStackTrace();
			return itemNameList;
		}
	}

	// item 이름 생성
	public static String[] getStartNumList(String inBodyForm, int inCount) {
		String[] itemNameList = null;

		try {
			if (inBodyForm == null) {
				return itemNameList;
			}

			int inputIndex = 0;
			int selectIndex = 0;
			int textareaIndex = 0;
			int quoteIndex = 0;
			String stName = "";
			int index = 0;

			String inputTag = "<input type='text' name='";
			String selectTag = "<select name='";
			String textAreaTag = "<textarea name='";

			int inputLength = inputTag.length();
			int selectLength = selectTag.length();
			int textAreaLength = textAreaTag.length();

			String tmp[] = inBodyForm.split("<td");
			itemNameList = new String[inCount];

			for (int i = 0; (i < tmp.length) && (index < inCount); i++) {
				inputIndex = tmp[i].indexOf(inputTag);
				selectIndex = tmp[i].indexOf(selectTag);// 콤보
				textareaIndex = tmp[i].indexOf(textAreaTag);// text area

				if (inputIndex >= 0) {
					quoteIndex = tmp[i].indexOf("'", inputIndex + inputLength);
					if (quoteIndex == -1)
						break;
					stName = tmp[i].substring(inputIndex + inputLength, quoteIndex).replaceAll("[a-zA-Z]", "");
					stName = stName.trim(); // 숫자
					stName = stName.toUpperCase();
					itemNameList[index] = stName;
					index++;
				} else if (selectIndex >= 0) {
					quoteIndex = tmp[i].indexOf("'", selectIndex + selectLength);
					if (quoteIndex == -1)
						break;

					stName = tmp[i].substring(selectIndex + selectLength, quoteIndex).replaceAll("[a-zA-Z]", "");
					stName = stName.trim(); // 숫자
					stName = stName.toUpperCase();
					itemNameList[index] = stName;
					index++;
				} else if (textareaIndex >= 0) {
					quoteIndex = tmp[i].indexOf("'", textareaIndex + textAreaLength);
					if (quoteIndex == -1)
						break;

					stName = tmp[i].substring(textareaIndex + textAreaLength, quoteIndex).replaceAll("[a-zA-Z]", "");
					stName = stName.trim(); // 숫자
					itemNameList[index] = stName;
					index++;
				}
			}
			return itemNameList;
		} catch (Exception ex) {
			ex.printStackTrace();
			return itemNameList;
		}
	}

	// dyna_report_to.jsp에서 사용
	public static int getEndNum(String inBodyForm) {

		int endNum = 0;
		try {
			int stInput = 0;
			int stSelete = 0;
			int stTextarea = 0;
			int quoteIndex = 0;

			String stn = "";
			String edn = "";

			String inputTag = "<input type='text' name='";
			String selectTag = "<select name='";
			String textAreaTag = "<textarea name='";

			int inputLength = inputTag.length();
			int selectLength = selectTag.length();
			int textAreaLength = textAreaTag.length();

			stInput = inBodyForm.indexOf(inputTag);// 입력창
			stSelete = inBodyForm.indexOf(selectTag);// 콤보
			stTextarea = inBodyForm.indexOf(textAreaTag);// text area

			if (stInput >= 0) {
                            
                            //v2 22.정수형 오버플로우_CWE-190 : Update by YOUNGJUN,CHO
                            if ((stInput + inputLength) < Integer.MAX_VALUE) {
                                quoteIndex = inBodyForm.indexOf("'", stInput + inputLength);
				stn = inBodyForm.substring(stInput + inputLength, quoteIndex).replaceAll("[0-9]", "");
				stn = stn.trim(); // A~Z
				edn = inBodyForm.substring(stInput + inputLength, quoteIndex).replaceAll("[a-zA-Z]", "");
				endNum = Integer.parseInt(edn.trim()); // 숫자
                            } else {
                                LOG.debug("MItem getEndNum() : 정수형 오버플로우_CWE-190");
                            }
                            //================================================
                            
			} else if (stSelete >= 0) {
                            
                            //v2 22.정수형 오버플로우_CWE-190 : Update by YOUNGJUN,CHO
                            if ((stSelete + selectLength) < Integer.MAX_VALUE) {
                                quoteIndex = inBodyForm.indexOf("'", stSelete + selectLength);

				stn = inBodyForm.substring(stSelete + selectLength, quoteIndex).replaceAll("[0-9]", "");
				stn = stn.trim(); // A~Z
				edn = inBodyForm.substring(stSelete + selectLength, quoteIndex).replaceAll("[a-zA-Z]", "");
				endNum = Integer.parseInt(edn.trim()); // 숫자
                            } else {
                                LOG.debug("MItem getEndNum() : 정수형 오버플로우_CWE-190");
                            }
                            //================================================
                            
			} else if (stTextarea >= 0) {
                            
                            //v2 22.정수형 오버플로우_CWE-190 : Update by YOUNGJUN,CHO
                            if ((stTextarea + textAreaLength) < Integer.MAX_VALUE) {
                                quoteIndex = inBodyForm.indexOf("'", stTextarea + textAreaLength);

				stn = inBodyForm.substring(stTextarea + textAreaLength, quoteIndex).replaceAll("[0-9]", "");
				stn = stn.trim(); // A~Z
				edn = inBodyForm.substring(stTextarea + textAreaLength, quoteIndex).replaceAll("[a-zA-Z]", "");
				endNum = Integer.parseInt(edn.trim()); // 숫자
                            } else {
                                LOG.debug("MItem getEndNum() : 정수형 오버플로우_CWE-190");
                            }
			    //================================================
                            
			}
			return endNum;
		} catch (Exception ex) {
			ex.printStackTrace();
			return endNum;
		}
	}

	public static int getTitleRowCount(String[] itemNameList, String[] itemDescList) {
		String tmpNum = "";
		String[] strarry = null;
		for (int i = 0, j = 0; i < itemDescList.length; i++) {
			if (itemDescList[i].equals("empty")) {
				strarry = itemNameList[i].split("");
				for (j = 0; j < strarry.length; j++) {
					if (MNumber.isNumber(strarry[j])) {
						tmpNum += strarry[j];
					}
				}
				return Integer.parseInt(tmpNum);
			}
		}

		return 0;
	}
}
