/***********************************************************************************
 * @@ Program Name : TotReportViewManager.java Description : 집계표 작성에 사용 Author : 강원중 Create Date : 2004-10-26 History :
 * @@
 ***********************************************************************************/

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

import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.beans.entity.TotReport;
import kr.co.kihyun.beans.totsys.report.Report;
import kr.co.kihyun.beans.totsys.report.ReportFormElement;
import kr.co.kihyun.beans.totsys.report.ReportsView;
import kr.co.kihyun.moumi.doc.table.MTableAnalysis;
import kr.co.kihyun.moumi.report.MReport;
import kr.co.kihyun.moumi.report.MReportData;
import kr.co.kihyun.moumi.report.MReportMaker;

public class TotReportViewer extends ReportFormElement {

	private TotDoc totDoc = null;
	private int reportCount = 0;
	private int allReportCount = 0;
	private int notReportCount = 0;
	protected String datas = "";
	protected String totDatas = "";
	protected String[] allDataList = null;
	protected String[] allTotDataList = null;

	public TotReportViewer(TotDoc totDoc) {
		this(totDoc, MReport.HTML);
	}

	public TotReportViewer(TotDoc totDoc, int outType) {
		super(totDoc, outType);
		this.totDoc = totDoc;
	}

	public void setReportData(String partID) {
		/*********** Doc & Table vlaues ***********/
		itemAnalysis(totDoc);
		ReportsView reposView = new ReportsView();
                //v2. 6.SQL 삽입 : reposView.executeQuery에서 prepare SQL 문으로 되어 있다.
		reposView.executeQuery(super.getTotDoc().getId(), partID);
                //================
		reportCount = reposView.getCount();
		notReportCount = reposView.getNotCount();
		totDatas = reposView.getTotDatas();
		datas = reposView.getDatas();
		allTotDataList = reposView.getTotDatasList();
		allDataList = reposView.getDatasList();
		Report report = new Report();
		allReportCount = report.getAllCount(totDoc.getId(), partID);
	}

	public int getReportCount() {
		return reportCount;
	}

	public int getAllReportCount() {
		return allReportCount;
	}

	public int getNotReportCount() {
		return notReportCount;
	}

	public String getReport(String actionType, String dataType) {
		// actionType 값이 view 이면 <br> 붙이고 그외에는 붙이지 않고 가져옴
		// dataType 값이 onlyData 이면 data 만 가져옴, 그외에는 title 와 data 붙여서 가져옴
		String[] tableTypesList = super.getTableTypes().split(",");

		StringBuilder allDatas = new StringBuilder();
		StringBuilder allTotDatas = new StringBuilder();
		for (int ad = 0; ad < tableTypesList.length; ad++) {
			for (int rc = 0; rc < reportCount; rc++) {
				String[] adl = allDataList[rc].split(TotReport.TABLE_DELIMITER);
				String[] atdl = allTotDataList[rc].split(TotReport.TABLE_DELIMITER);

				if (!allDatas.toString().equals("")) {
					if (rc == 0) {
						allDatas.append(TotReport.TABLE_DELIMITER);
						allDatas.append(adl[ad]);
					} else {
						allDatas.append(TotReport.TABLE_DELIMITER);
						allDatas.append(adl[ad]);
					}
				} else
					allDatas.append(adl[ad]);

				if (!allTotDatas.toString().equals("")) {
					if (rc == 0) {
						allTotDatas.append(TotReport.TABLE_DELIMITER);
						allTotDatas.append(atdl[ad]);
					} else {
						allTotDatas.append(TotReport.TABLE_DELIMITER);
						allTotDatas.append(atdl[ad]);
					}
				} else
					allTotDatas.append(atdl[ad]);
			}
		}

		int[] tableColNumList = MReportData.getColNumList(allDatas.toString());
		int[] tableRowNumList = MReportData.getRowNumList(allDatas.toString());

		String[] tableRowList = new String[tableRowNumList.length];
		for (int tr = 0; tr < tableRowNumList.length; tr++)
			tableRowList[tr] = Integer.toString(tableRowNumList[tr]);

		int inItemCount = 0;
		for (int tc = 0; tc < tableColNumList.length; tc++) {
			inItemCount = inItemCount + tableColNumList[tc];
		}

		String[] tmpdataList = MReportData.getDataArray(allDatas.toString());

		allDataList = MReportData.getTotalDataList(0, inItemCount, tableColNumList, tableRowList,
				super.getItemList().getTypeList(), super.getItemList().getExprList(), tmpdataList, super.getItemList().getNameList());

		StringBuffer reportBuff = new StringBuffer();

		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 stTABLE = 0;
		int edTABLE = 0;

		int here = 0;
		for (int i = 0; i < tableRowNumList.length; i++) {
			if (Form.indexOf("</table>", here) < 0)
				break;
			subForm[i] = MReport.geteExtractTable(Form, here);
			here = Form.indexOf("</table>", here) + 8;
		}

		subForm = MReport.geteExtractTable(Form);
		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]];
			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();

			for (int a = 0; a < tableRowNumList.length; a++) {
				tableRowNumList[a] = 1;
			}

			if ("onlyData".equals(dataType)) {
				stTABLE = titleForms[i].indexOf("<table");
				edTABLE = titleForms[i].indexOf("<tr", stTABLE);

				if (edTABLE > 0) {
					reportBuff.append(titleForms[i].substring(stTABLE, edTABLE));
				} else {
					reportBuff.append(titleForms[i]);
				}
			} else {
				reportBuff.append(titleForms[i]);
			}

			for (int r = 0; r < tableRowNumList[i]; r++) {
				for (int cp = 0; cp < (tableColNumList[i]); cp++) {
					subDataList[cp] = allDataList[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];
			}
			reportBuff.append(tailForms[i]);
			if ("view".equals(actionType)) {
				if (i < (tableRowNumList.length - 1)) {
					reportBuff.append("<br>");
				}
			}
			tablePosition = tp;
		}
		return reportBuff.toString();
	}
}
