package de.lcag.common;

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

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

import javax.naming.ConfigurationException;

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

import routines.LcagFileTools;

class XmlTableTest {

	@BeforeAll
	static void setUpBeforeClass() throws Exception {
	}

	@BeforeEach
	void setUp() throws Exception {
	}

	@Test
	void testRead() throws IOException {
		String xmlFilePath = "src/test/resources/CompareTables/MDM_compare_FilePairs/020-14713764_getShipment_reply.xml";
		XmlTable aTable = null;
		List<String> wantedColumns = List.of(
				"Origin;Destination;Issuedate;AWBCurrency;CustomsIndicator;CDBNo;PaymentResponsibility;ValuationCharge;ChargeType;TotalValuationCharge"
						.split(";"));
		;
		int n;

		// TC1: Read a XML file without any configuration done:
		aTable = new XmlTable(xmlFilePath);
		aTable.read();
		n = aTable.lenght();
		assertEquals(1, n, "Unexpected number of rows found");

		n = aTable.getColumnNames().size();
		assertEquals(126, n, "Unexpected number of columns found");

		// TC2: Read a XML file from a start Xpath and filter-out unwanted columns
		aTable = new XmlTable(xmlFilePath);

		aTable.setStartXPath("//ns2:AWB");
		aTable.setWantedColumns(wantedColumns);

		aTable.read();
		n = aTable.lenght();
		assertEquals(1, n, "Unexpected number of rows found");

		n = aTable.getColumnNames().size();
		assertEquals(wantedColumns.size(), n, "Unexpected number of columns found");

	}

	@Test
	void testReadAndMap() throws IOException, ConfigurationException {
		String xmlFilePath = LcagFileTools.getResourceFilePath("MDS_City.xml");
		CsvTable mappingTableCSV = new CsvTable("EntityFieldMapping.csv");

		mappingTableCSV.read();
		MappingTable mappingTable = new MappingTable(mappingTableCSV);

		XmlTable aTable = null;
		List<String> wantedColumns = List.of(
				"Origin;Destination;Issuedate;AWBCurrency;CustomsIndicator;CDBNo;PaymentResponsibility;ValuationCharge;ChargeType;TotalValuationCharge"
						.split(";"));
		;
		int n;

		// TC1: Read a XML file without any configuration done:
		aTable = new XmlTable(xmlFilePath);

		aTable.setOption(HashTable.OPTION_USE_MAPPING, mappingTable);
		aTable.read();
		n = aTable.lenght();
		assertEquals(1, n, "Unexpected number of rows found");

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

	@Test
	void testAppend() throws IOException {
		XmlTable aTable = new XmlTable();
		List<String> fileList = List.of("14713764", "14713775", "14713786");
		List<String> wantedColumns = List.of(
				"Origin;Destination;Issuedate;AWBCurrency;CustomsIndicator;CDBNo;PaymentResponsibility;ValuationCharge;ChargeType;TotalValuationCharge"
						.split(";"));

		aTable.setStartXPath("//ns2:AWB");
		aTable.setWantedColumns(wantedColumns);

		for (String aFile : fileList) {
			String xmlFilePath = String.format(
					"src/test/resources/CompareTables/MDM_compare_FilePairs/020-%s_getShipment_reply.xml", aFile);

			aTable.append(xmlFilePath);
		}

		int n = aTable.lenght();
		assertEquals(fileList.size(), n, "Unexpected number of rows found");
	}
}
