package de.lcag.common;

import java.util.Set;

import routines.LcagGeneral;

public class GenericComparisonResultTable extends ExcelHashTable {
	public static final String FILE_PATH_COLUMN = "FilePath";
	public static final String SOURCE_ROW_NUMBER_COLUMN = "Src_RowNo";
	public static final String COMPARE_ROW_NUMBER_COLUMN = "Cmp_RowNo";
	public static final String COLUMN_NAME_COLUMN = "Col_Name";
	public static final String SOURCE_VALUE_COLUMN = "Src_Value";
	public static final String COMPARE_VALUE_COLUMN = "Cmp_Value";
	public static final String SOURCE_COLUMN_NUMBER_COLUMN = "Src_ColNo";
	public static final String COMPARE_COLUMN_NUMBER_COLUMN = "Cmp_ColNo";

	private AbstractTable sourceTable = null;
	private AbstractTable compareTable = null;

	public GenericComparisonResultTable(AbstractTable pSrcTable, AbstractTable pCmpTable) {
		super(KEY_COLUMN, SOURCE_ROW_NUMBER_COLUMN, COMPARE_ROW_NUMBER_COLUMN, COLUMN_NAME_COLUMN, SOURCE_VALUE_COLUMN,
				COMPARE_VALUE_COLUMN, SOURCE_COLUMN_NUMBER_COLUMN, COMPARE_COLUMN_NUMBER_COLUMN);

		this.sourceTable = pSrcTable;
		this.compareTable = pCmpTable;

		compareTables();
	}

	public GenericComparisonResultTable(ExcelWorkbook pSrcTable, ExcelWorkbook pCmpTable) {
		super(KEY_COLUMN, SOURCE_ROW_NUMBER_COLUMN, COMPARE_ROW_NUMBER_COLUMN, COLUMN_NAME_COLUMN, SOURCE_VALUE_COLUMN,
				COMPARE_VALUE_COLUMN, SOURCE_COLUMN_NUMBER_COLUMN, COMPARE_COLUMN_NUMBER_COLUMN);

		this.sourceTable = pSrcTable.getFirstTable();
		this.compareTable = pCmpTable.getFirstTable();

		compareTables();
	}

	private void compareTables() {
		int noRows = sourceTable.lenght();
		Set<String> columnNames = sourceTable.getColumnNames();
		Set<String> compareColumnNames = compareTable.getColumnNames();

		for (int srcRowNumber = 0; srcRowNumber < noRows; srcRowNumber++) {
			int columnNumber = 0;

			for (String columnName : columnNames) {
				Object srcValue = sourceTable.getCellValue(srcRowNumber, columnName);
				Object cmpValue = null;

				if (compareColumnNames.contains(columnName)) {
					cmpValue = compareTable.getCellValue(srcRowNumber, columnName);
				}

				String compareResult = LcagGeneral.compareValue(srcValue, cmpValue);
				int n = this.newRow();

				columnNumber += 1;

				this.setCellValue(n, KEY_COLUMN, srcRowNumber);
				this.setCellValue(n, SOURCE_ROW_NUMBER_COLUMN, srcRowNumber);
				this.setCellValue(n, COMPARE_ROW_NUMBER_COLUMN, srcRowNumber);
				this.setCellValue(n, COLUMN_NAME_COLUMN, columnName);
				this.setCellValue(n, SOURCE_VALUE_COLUMN, srcValue);
				this.setCellValue(n, COMPARE_VALUE_COLUMN, compareResult);
				this.setCellValue(n, SOURCE_COLUMN_NUMBER_COLUMN, columnNumber);
				this.setCellValue(n, COMPARE_COLUMN_NUMBER_COLUMN, columnNumber);
			}

		}
	}

}
