/*********************************************************************************************************
 * 프로그램명 : MreportMake.java 프로그램설명 : 보고한 보고자료와 문서 양식을 조합하여 보고자료 보기 처리 해줌 작성자 : 강원중 작성일 : 2003.11.30 변경일 : 2004.09.16
 **********************************************************************************************************/

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

import kr.co.kihyun.lang.MString;
import kr.co.kihyun.moumi.doc.table.item.MItemInfo;
import kr.co.kihyun.text.excel.CalculationForm;
import kr.co.kihyun.text.excel.Excel;

public class MTableRemaker {

	public static String remake(String form, MItemInfo info) {
		form = Excel.removeFmla(form);

		int stTD = 0;
		int edTD = 0;
		int itemCount = info.getItemNumList().length;

		StringBuffer reportBuf = new StringBuffer();

		int rowNum = 1;

		for (int i = 0; i < itemCount; i++) {
			if (i > 0) {
				if (info.getTableNumList()[i] > info.getTableNumList()[i - 1]) {
					rowNum = 1;
				} else if (info.getRowNumList()[i] > info.getRowNumList()[i - 1]) {
					rowNum++;
				}
			}

			stTD = form.indexOf("<td");
			if (stTD == -1)
				break;

			edTD = form.indexOf(">", stTD + 1);
			if (edTD == -1)
				break;

			if (!MString.isNull(info.getItemExprList()[i]) && info.getItemExprList()[i].indexOf("^") < 0
					&& info.getItemExprList()[i].indexOf("~") < 0) { // 숫짜일때
				reportBuf.append(form.substring(0, edTD));
				reportBuf.append(" ");
				reportBuf.append(CalculationForm.getCalculationForm(CalculationForm.getCalculationForm(
						info.getItemExprList()[i], (info.getRowNumList()[i] - rowNum), "sub")));
				reportBuf.append(">");
			} else {
				reportBuf.append(form.substring(0, edTD));
				reportBuf.append(">");
			}

			form = form.substring(edTD + 1, form.length());
		}

		reportBuf.append(form);
		return reportBuf.toString();
	}

}
