package de.lcag.common;

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

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

class HashTableTest {

	@BeforeAll
	static void setUpBeforeClass() throws Exception {
	}

	@BeforeEach
	void setUp() throws Exception {
	}

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

		names.read();
		int result = names.findRow("FirstName", "Hermann");

		assertEquals(1, result);
	}

	@Test
	void testAsHTML() {
		CsvTable t = new CsvTable();
		String result;

		t.read("A;B;C\n1;2;3\n4;#HTML#b 5#HTML#/b;6");
		result = t.asHTML();

		// HTML should include table structure:
		assertTrue(result.endsWith(">6</td></tr>\n" + "</tr></table>\n"));

		// Normally, we just support plain text in the table. However, a tag after a
		// #HTML# commands should be included in the target HTML:
		assertTrue(result.contains("</b>"));
	}

	@Test
	void testAsCsvData() {
		CsvTable t = new CsvTable();
		String result;

		t.read("A;B;C\n1;2;3\n4;5;6");
		result = t.asCsvData();

		assertEquals("1; 2; 3\n4; 5; 6\n", result);
	}

	@Test
	void testRowNumbering() {
		CsvTable t = new CsvTable();
		String result;

		t.enableOption(AbstractTable.OPTION_NUMBER_ROWS);
		t.read("A;B;C\n1;2;3\n4;5;6");
		result = t.asCSV();

		assertEquals(AbstractTable.ROW_NUMBER_COLUMN + "; A; B; C\n" + "00000; 1; 2; 3\n00001; 4; 5; 6\n", result);
	}

	@Test
	void testJoinTable() throws IOException {
		CsvTable names = new CsvTable("CsvTable/NameList.csv");
		CsvTable addresses = new CsvTable("CsvTable/NameAddressList.csv");
		Map<String, String> joinOn = new HashMap<>();

		names.read();
		addresses.read();

		joinOn.put("Name", "LastName");

		Table result = names.joinTable(addresses, joinOn);

		int len = result.lenght();

		assertEquals(2, len);
	}

	@Test
	void testJoinTableTableMap() throws IOException {
		ExcelWorkbook result = new ExcelWorkbook("src/test/resources/results/tmp.xlsx");

		CsvTable names = new CsvTable("CsvTable/NameList.csv");
		CsvTable addresses = new CsvTable("CsvTable/NameAddressList.csv");
		Map<String, String> joinOn = new HashMap<>();

		names.read();
		addresses.read();

		joinOn.put("Name", "LastName");

		names.joinTable(result, addresses, joinOn);

		result.save();

		int len = result.lenght();

		assertEquals(2, len);

	}

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

		names.read();
		String result = names.asCSV("FirstName", ", ");

		assertEquals("Frank, Hermann, Sabine, Toben", result);
	}

	@Test
	void testAppend() throws IOException {
		CsvTable csv = new CsvTable("CsvTable/City_change_history_ok.csv");
		ExcelHashTable excelTable = new ExcelHashTable("ExcelTable/City_change_history_ok.xlsx");

		csv.read();
		excelTable.append(csv);

		int csvLen = csv.lenght();
		int excelLen = excelTable.lenght();

		assertEquals(csvLen, excelLen);
	}

}
