package de.lcag.common;

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

import java.awt.Desktop;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
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 de.lcag.common.Table.TableColumn;
import routines.LcagFileTools;

class ExcelWorkbookTest {
	private Path resDir = Path.of("src/test/resources/ExcelTable");

	@BeforeAll
	static void setUpBeforeClass() throws Exception {
	}

	@BeforeEach
	void setUp() throws Exception {
	}

	@Test
	void testSave() throws IOException {
		CsvTable csv = new CsvTable("CsvTable/NameList.csv");
		csv.read();

		ExcelWorkbook excel = new ExcelWorkbook("src/test/resources/results/NameList.xlsx");

		excel.append(csv);
		excel.save();
	}

	@Test
	void testInitColumn() throws IOException, ConfigurationException {
		ExcelWorkbook excel;
		int noRows = -1;

		excel = new ExcelWorkbook("src/test/resources/ExcelTable/NameList.xlsx");
		excel.read();

		TableColumn nameColumn = excel.getColumnByLabel("LastName");
		TableColumn testColumn = excel.addColumn("TestCol");

		testColumn.init(nameColumn);

		assertEquals(testColumn, excel.getColumnByLabel("TestCol"));

		String expectedName = excel.getCellValue(0, "LastName");
		String actualName = excel.getCellValue(0, "TestCol");
		assertEquals(expectedName, actualName, "Copied field name does not match");

		excel.saveAs("src/test/resources/results/result");
	}

	@Test
	void testRead() throws IOException, ConfigurationException {
		ExcelWorkbook excel;
		int noRows = -1;
		CsvTable mappingTableCSV = new CsvTable("EntityFieldMapping.csv");

		mappingTableCSV.read();
		noRows = mappingTableCSV.lenght();
		assertEquals(37, noRows);

		excel = new ExcelWorkbook("src/test/resources/results/NameList.xlsx");
		excel.read();
		noRows = excel.lenght();
		assertEquals(4, noRows);

		// TODO: Test read using mapping table
		MappingTable mappingTable = new MappingTable(mappingTableCSV);

	}

	@Test
	void testReadLimitedWithKeys() throws IOException, ConfigurationException {
		ExcelWorkbook excel;
		int noRows = -1;
		int noRowsLimit = 5;
		List<String> wantedColumns = List.of("Code", "Valid From", "Name", "Latitude", "Longitude", "X", "Y");
		List<String> keyFields = List.of("Code", "Valid From");

		excel = new ExcelWorkbook("src/test/resources/ExcelTable/City-BeforeCorrection.xlsx");
		excel.setOption(Table.OPTION_LIMIT_NUMBER_ROWS, noRowsLimit);
		excel.setWantedColumns(wantedColumns);
		excel.setKeyColumns(keyFields);
		excel.read();

		noRows = excel.lenght();
		assertEquals(noRowsLimit, noRows);

		String key = excel.getKeyValue(1);
		assertEquals("ACO/2023-12-01T00:00:00Z", key);

	}

	@Test
	void testCompareToTinyTables() throws Exception {
		Path masterFilePath = Path.of("src/test/resources/CompareTables/MDM_TwoTodaysStation.xlsx");
		Path clientFilePath = Path.of("src/test/resources/CompareTables/MDS_OneAirport.xlsx");
		Path diffFilePath = LcagFileTools.appendStringToFilePath(masterFilePath, "-diff");
		Path resultFilePath = LcagFileTools.appendStringToFilePath(masterFilePath, "-merged");

		Files.copy(masterFilePath, resultFilePath, StandardCopyOption.REPLACE_EXISTING);

		ExcelWorkbook masterExcel = new ExcelWorkbook(resultFilePath.toString());
		ExcelWorkbook clientExcel = new ExcelWorkbook(clientFilePath.toString());
		List<String> keyFields = List.of("Code");

		masterExcel.setKeyColumns(keyFields);

		masterExcel.read();
		clientExcel.setKeyColumns(keyFields);
		clientExcel.read();

		WorkbookComparisonResult compareResult = masterExcel.compareTo(clientExcel);
		compareResult.save(diffFilePath);
		// Desktop.getDesktop().open(diffFilePath.toFile());

		ExcelWorkbook integratedTable = (ExcelWorkbook) compareResult.integrateDifferencesIntoMaster();
		integratedTable.save();

		Desktop.getDesktop().open(resultFilePath.toFile());
	}

	@Test
	void testCompareToBigTables() throws Exception {
		Path masterFilePath = Path.of("src/test/resources/CompareTables/MDM_TodaysStation.xlsx");
		Path clientFilePath = Path.of("src/test/resources/CompareTables/MDS_Airport.xlsx");
		Path diffFilePath = LcagFileTools.appendStringToFilePath(masterFilePath, "-diff");
		Path resultFilePath = LcagFileTools.appendStringToFilePath(masterFilePath, "-merged");

		Files.copy(masterFilePath, resultFilePath, StandardCopyOption.REPLACE_EXISTING);

		ExcelWorkbook masterExcel = new ExcelWorkbook(resultFilePath.toString());
		ExcelWorkbook clientExcel = new ExcelWorkbook(clientFilePath.toString());
		List<String> keyFields = List.of("Code");

		masterExcel.setKeyColumns(keyFields);
		// masterExcel.setMaxRows(10);

		masterExcel.read();
		clientExcel.setKeyColumns(keyFields);
		clientExcel.read();

		WorkbookComparisonResult compareResult = masterExcel.compareTo(clientExcel);
		compareResult.save(diffFilePath);
		// Desktop.getDesktop().open(diffFilePath.toFile());

		ExcelWorkbook integratedTable = (ExcelWorkbook) compareResult.integrateDifferencesIntoMaster();
		integratedTable.save();

		Desktop.getDesktop().open(resultFilePath.toFile());
	}
}
