/***********************************************************************************
 * @@ Program Name : StaticMReportViewer.java Description : 보고한 보고자료와 문서 양식을 조합하여 보고자료 보기 처리 해줌 Author : 강원중 Create Date
 *    : 2005-02-04 History :
 * @@
 ***********************************************************************************/

package kr.co.kihyun.beans.totsys.report;

import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.moumi.report.MReportData;
import kr.co.kihyun.moumi.report.MReportMaker;
import kr.co.kihyun.moumi.doc.table.MTable;
import kr.co.kihyun.moumi.doc.table.MTableAnalysis;

/**
 * @author bhs
 * @deprecated
 * @see {@link TotReport#getData()} {@link TotReportUtil#toHTML(TotReport)}
 */
public class DynaReportViewer extends ReportViewFormElement implements ReportViewer {

	private StringBuffer reportBuff;

	public DynaReportViewer(TotDoc totDoc, int outType) {
		super(totDoc, outType);
	}

    public DynaReportViewer(TotDoc totDoc, TotDoc slaveDoc, int outType) {
		super(totDoc, slaveDoc,outType);
	}

	public String getReport() {

		if (super.getDatas() == null)
			return null;

		String[] dataList = MReportData.getDataArray(super.getDatas());

		String[] totDataList = MReportData.getDataArray(super.getTotDatas());

		String[] tableTypesList = super.getTableTypes().split(",");

		int[] tableColNumList = MReportData.getColNumList(super.getDatas());
		int[] tableRowNumList = MReportData.getRowNumList(super.getDatas());

		String[] titleForms = new String[tableRowNumList.length];
		String[] dataForms = new String[tableRowNumList.length];
		String[] tailForms = new String[tableRowNumList.length];

		String[] subForm = new String[tableRowNumList.length];
		String Form = super.getForm();
		int here = 0;
		for (int i = 0; i < tableRowNumList.length; i++) {
			if (Form.indexOf("</table>", here) < 0)
				break;

			// subForm[i]=Form.substring(here, Form.indexOf("</table>", here)+8);
			// here=Form.indexOf("</table>", here)+8;
			if (i == tableRowNumList.length - 1)
				subForm[i] = Form.substring(here, Form.length());
			else
				subForm[i] = Form.substring(here, Form.indexOf("</table>", here) + 8);

			here = Form.indexOf("</table>", here) + 8;
		}

		int tablePosition = 0;
		int tp = 0;
		reportBuff = new StringBuffer();

		for (int i = 0; i < tableRowNumList.length; i++) {
			/****** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 ******/
			for (tp = tablePosition; tp < super.getItemList().getTableNumList().length; tp++) {
				if ((i + 1) < super.getItemList().getTableNumList()[tp])
					break;
			}

			int[] tmpItemNumList = new int[tp - tablePosition];
			String[] tmpItemNameList = new String[tp - tablePosition];
			int[] tmpItemBNWList = new int[tp - tablePosition];
			int[] tmpItemTypeList = new int[tp - tablePosition];
			String[] tmpItemExprList = new String[tp - tablePosition];
			int[] tmpItemRowList = new int[tp - tablePosition];

			for (int in = 0, t = tablePosition; t < tp; in++, t++) {
				tmpItemNumList[in] = super.getItemList().getNumList()[t];
				tmpItemNameList[in] = super.getItemList().getNameList()[t];
				tmpItemBNWList[in] = super.getItemList().getBNWList()[t];
				tmpItemTypeList[in] = super.getItemList().getTypeList()[t];
				tmpItemExprList[in] = super.getItemList().getExprList()[t];
				tmpItemRowList[in] = super.getItemList().getRecordNumList()[t];
			}
			int tmpInItemNum = 0;
			int tmpIN = 0;
			for (int a = 0; a < tmpItemTypeList.length; a++) {
				if (tmpItemTypeList[a] != 0) { // MItem.TITLE == 0
					tmpIN = a;
					break;
				}
			}
			for (int a = 0; a < tmpItemRowList.length; a++) {
				if (tmpItemRowList[a] < tmpItemRowList[tmpIN])
					tmpInItemNum++;
				else
					break;
			}

			int tmpTitleRowCount = 0;
			if (tmpInItemNum > 0) {
				tmpTitleRowCount = tmpItemRowList[tmpInItemNum - 1] - tmpItemRowList[0] + 1;
			}
			/***** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 끝 *****/

			String[] subDataList = new String[tableColNumList[i]];
			String[] subTotDataList = new String[tableColNumList[i]];

			int stData = 0;
			int stTotData = 0;
			for (int c = 0; c < i; c++)
            {
				stData = stData + (tableColNumList[c] * tableRowNumList[c]);
				stTotData = stTotData + tableColNumList[c];
			}

			MTableAnalysis tbAnalysis = new MTableAnalysis(subForm[i], tmpItemTypeList);

			titleForms[i] = tbAnalysis.getTitleForm();
			dataForms[i] = tbAnalysis.getDataForm();
			tailForms[i] = tbAnalysis.getTailForm();

			reportBuff.append(titleForms[i]);




			for (int r = 0; r < tableRowNumList[i]; r++)
            {
				for (int cp = 0; cp < (tableColNumList[i]); cp++)
                {
					subDataList[cp] = dataList[stData + cp];
				}
				reportBuff.append(MReportMaker.reportMake(dataForms[i], subDataList, tmpItemNumList, tmpItemNameList,
						tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum, tmpTitleRowCount,
						tableRowNumList[i], Integer.parseInt(tableTypesList[i]), super.getOutType()));
				stData = stData + tableColNumList[i];
			}
			int totView = 0;
			for (int cp = 0; cp < (tableColNumList[i]); cp++) {
				subTotDataList[cp] = totDataList[stTotData + cp];
				if (!subTotDataList[cp].equals(" "))
					totView = 1;
			}
			if (tableRowNumList[i] > 1 && totView == 1)
				reportBuff.append(MReportMaker.reportMake(dataForms[i], subTotDataList, tmpItemNumList,
						tmpItemNameList, tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum,
						tmpTitleRowCount, tableRowNumList[i], MTable.DYNATOTLINE, super.getOutType()));
			reportBuff.append(tailForms[i]);

			tablePosition = tp;
		}
		return reportBuff.toString();
	}

	public String getReportNBSP() {

		if (super.getDatas() == null)
			return null;

		String[] dataList = MReportData.getDataArray(super.getDatas());

		for (int i = 0; i < dataList.length; i++) {
			dataList[i] = dataList[i].replaceAll(" ", "&nbsp;");
			dataList[i] = dataList[i].replaceAll("<", "&lt");
			dataList[i] = dataList[i].replaceAll(">", "&gt");
		}

		String[] totDataList = MReportData.getDataArray(super.getTotDatas());

		String[] tableTypesList = super.getTableTypes().split(",");

		int[] tableColNumList = MReportData.getColNumList(super.getDatas());
		int[] tableRowNumList = MReportData.getRowNumList(super.getDatas());

		String[] titleForms = new String[tableRowNumList.length];
		String[] dataForms = new String[tableRowNumList.length];
		String[] tailForms = new String[tableRowNumList.length];

		String[] subForm = new String[tableRowNumList.length];
		String Form = super.getForm();
		int here = 0;
		for (int i = 0; i < tableRowNumList.length; i++) {
			if (Form.indexOf("</table>", here) < 0)
				break;

			if (i == tableRowNumList.length - 1)
				subForm[i] = Form.substring(here, Form.length());
			else
				subForm[i] = Form.substring(here, Form.indexOf("</table>", here) + 8);

			here = Form.indexOf("</table>", here) + 8;

		}

		int tablePosition = 0;
		int tp = 0;
		reportBuff = new StringBuffer();

		for (int i = 0; i < tableRowNumList.length; i++) {
			/****** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 ******/
			for (tp = tablePosition; tp < super.getItemList().getTableNumList().length; tp++) {
				if ((i + 1) < super.getItemList().getTableNumList()[tp])
					break;
			}

			int[] tmpItemNumList = new int[tp - tablePosition];
			String[] tmpItemNameList = new String[tp - tablePosition];
			int[] tmpItemBNWList = new int[tp - tablePosition];
			int[] tmpItemTypeList = new int[tp - tablePosition];
			String[] tmpItemExprList = new String[tp - tablePosition];
			int[] tmpItemRowList = new int[tp - tablePosition];

			for (int in = 0, t = tablePosition; t < tp; in++, t++) {
				tmpItemNumList[in] = super.getItemList().getNumList()[t];
				tmpItemNameList[in] = super.getItemList().getNameList()[t];
				tmpItemBNWList[in] = super.getItemList().getBNWList()[t];
				tmpItemTypeList[in] = super.getItemList().getTypeList()[t];
				tmpItemExprList[in] = super.getItemList().getExprList()[t];
				tmpItemRowList[in] = super.getItemList().getRecordNumList()[t];
			}
			int tmpInItemNum = 0;
			int tmpIN = 0;
			for (int a = 0; a < tmpItemTypeList.length; a++) {
				if (tmpItemTypeList[a] != 0) { // MItem.TITLE == 0
					tmpIN = a;
					break;
				}
			}
			for (int a = 0; a < tmpItemRowList.length; a++) {
				if (tmpItemRowList[a] < tmpItemRowList[tmpIN])
					tmpInItemNum++;
				else
					break;
			}

			int tmpTitleRowCount = 0;
			if (tmpInItemNum > 0) {
				tmpTitleRowCount = tmpItemRowList[tmpInItemNum - 1] - tmpItemRowList[0] + 1;
			}
			/***** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 끝 *****/

			String[] subDataList = new String[tableColNumList[i]];
			String[] subTotDataList = new String[tableColNumList[i]];

			int stData = 0;
			int stTotData = 0;
			for (int c = 0; c < i; c++) {
				stData = stData + (tableColNumList[c] * tableRowNumList[c]);
				stTotData = stTotData + tableColNumList[c];
			}

			MTableAnalysis tbAnalysis = new MTableAnalysis(subForm[i], tmpItemTypeList);

			titleForms[i] = tbAnalysis.getTitleForm();
			dataForms[i] = tbAnalysis.getDataForm();
			tailForms[i] = tbAnalysis.getTailForm();

			reportBuff.append(titleForms[i]);
			for (int r = 0; r < tableRowNumList[i]; r++) {
				for (int cp = 0; cp < (tableColNumList[i]); cp++) {
					subDataList[cp] = dataList[stData + cp];
				}
				reportBuff.append(MReportMaker.reportMake(dataForms[i], subDataList, tmpItemNumList, tmpItemNameList,
						tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum, tmpTitleRowCount,
						tableRowNumList[i], Integer.parseInt(tableTypesList[i]), super.getOutType()));
				stData = stData + tableColNumList[i];
			}
			int totView = 0;
			for (int cp = 0; cp < (tableColNumList[i]); cp++) {
				subTotDataList[cp] = totDataList[stTotData + cp];
				if (!subTotDataList[cp].equals(" "))
					totView = 1;
			}
			if (tableRowNumList[i] > 1 && totView == 1)
				reportBuff.append(MReportMaker.reportMake(dataForms[i], subTotDataList, tmpItemNumList,
						tmpItemNameList, tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum,
						tmpTitleRowCount, tableRowNumList[i], MTable.DYNATOTLINE, super.getOutType()));
			reportBuff.append(tailForms[i]);

			tablePosition = tp;
		}
		return reportBuff.toString();
	}
}
