package de.lcag.common;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import routines.LcagGeneral;

class GenericComparisonResultTableTest {

	@BeforeAll
	static void setUpBeforeClass() throws Exception {
	}

	@BeforeEach
	void setUp() throws Exception {
	}

	@Test
	void testGenericComparisonResultTableXML() throws IOException {
		String xmlFilePath1 = "src/test/resources/CompareTables/MDM_compare_FilePairs/020-14713764_getShipment_reply.xml";
		String xmlFilePath2 = "src/test/resources/CompareTables/MDM_compare_FilePairs/020-14713764_storeShipment_request.xml";
		XmlTable aTable1 = null;
		XmlTable aTable2 = null;
		List<String> wantedColumns = null;

		wantedColumns = List.of(
				"Origin;Destination;Issuedate;AWBCurrency;CustomsIndicator;CDBNo;PaymentResponsibility;ValuationCharge;ChargeType;TotalValuationCharge"
						.split(";"));

		// TC1: Compare both files
		aTable1 = new XmlTable(xmlFilePath1);
		aTable1.setStartXPath("//ns2:AWB");
		aTable1.setWantedColumns(wantedColumns);
		aTable1.read();

		aTable2 = new XmlTable(xmlFilePath2);
		aTable2.setStartXPath("//ns2:AWB");
		aTable2.setWantedColumns(wantedColumns);
		aTable2.read();

		GenericComparisonResultTable result = new GenericComparisonResultTable(aTable1, aTable2);

		result.saveAs("result.csv");
	}

	@Test
	void testGenericComparisonResultTableExcel() throws IOException {
		String xmlFilePath1 = "src/test/resources/CompareTables/MDM_compare_FilePairs/020-14713764_getShipment_reply.xml";
		String xmlFilePath2 = "src/test/resources/CompareTables/MDM_compare_FilePairs/020-14713764_storeShipment_request.xml";
		ExcelWorkbook aTable1 = null;
		ExcelWorkbook aTable2 = null;
		List<String> wantedColumns = null;
		int noRows = 0;

		wantedColumns = List.of("Code", "Name", "Latitude", "Longitude", "X", "Y");

		// TC1: Compare both files
		aTable1 = new ExcelWorkbook("src/test/resources/ExcelTable/City-BeforeCorrection.xlsx");
		aTable1.setWantedColumns(wantedColumns);
		aTable1.read();

		noRows = aTable1.lenght();
		assertEquals(11496, noRows);

		aTable2 = new ExcelWorkbook("src/test/resources/ExcelTable/City-XY-calculated.xlsx");
		aTable2.setWantedColumns(wantedColumns);
		aTable2.read();

		noRows = aTable2.lenght();
		assertEquals(9087, noRows);

		// fail("Comparing with may lines not yet implemented");
		GenericComparisonResultTable result = new GenericComparisonResultTable(aTable1, aTable2);

		result.saveAs("result.csv");
	}

	@Test
	void testGenericComparisonResultTableFilePairs() throws IOException {
		String xmlTestDirectory = "src/test/resources/CompareTables/MDM_compare_FilePairs";
		String suffix = "_storeShipment_request.xml";
		String suffix2 = "_getShipment_reply.xml";
		String startXPath = "//ns2:AWB";
		List<String> wantedColumns = List.of(
				"Origin;Destination;Issuedate;AWBCurrency;CustomsIndicator;CDBNo;PaymentResponsibility;ValuationCharge;ChargeType;TotalValuationCharge"
						.split(";"));
		;
		File dir = new File(xmlTestDirectory);
		File[] files = dir.listFiles((d, name) -> name.endsWith(suffix));
		int n;
		Object value;

		XmlTable aTable1 = new XmlTable();
		XmlTable aTable2 = new XmlTable();
		ExcelWorkbook result = new ExcelWorkbook("result.xlsx");

		aTable1.setStartXPath(startXPath);
		aTable1.setWantedColumns(wantedColumns);
		aTable1.enableOption(HashTable.OPTION_ADD_SOURCE_COLUMN);
		aTable2.setStartXPath(startXPath);
		aTable2.setWantedColumns(wantedColumns);
		aTable2.enableOption(HashTable.OPTION_ADD_SOURCE_COLUMN);

		for (File f : files) {
			String xmlFilePath1 = f.getCanonicalPath();
			String xmlFilePath2 = xmlFilePath1.replaceAll(suffix, suffix2);
			String fileName1 = f.getName();
			String fileName2 = fileName1.replaceAll(suffix, suffix2);

			aTable1.append(xmlFilePath1, fileName1);
			aTable2.append(xmlFilePath2, fileName2);

		}

		GenericComparisonResultTable cmpResult = new GenericComparisonResultTable(aTable1, aTable2);

		n = cmpResult.lenght();
		assertEquals(45, n, "Unexpected number of rows in comparison result table");

		value = cmpResult.getCellValue(22, GenericComparisonResultTable.COLUMN_NAME_COLUMN);
		assertEquals("DESTINATION", value, "Unexpected content in comparison result table");
		value = cmpResult.getCellValue(22, GenericComparisonResultTable.COMPARE_VALUE_COLUMN);
		assertEquals(LcagGeneral.COMPARE_EQUALS, value, "Unexpected content in comparison result table");

		result.addSheet("ComparisonResult", cmpResult);
		cmpResult.saveAs("result.csv");
		result.save();
	}

}
