package de.lcag.common;

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

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

import de.lcag.common.SelectStatement.Condition;

class SelectStatementTest {

	@BeforeAll
	static void setUpBeforeClass() throws Exception {
	}

	@BeforeEach
	void setUp() throws Exception {
	}

	@Test
	void testSelectStatement() {
		SelectStatement result;
		Map<String, String> expectedCols = new LinkedHashMap<>();
		expectedCols.put("C1", "s.C1");
		expectedCols.put("C2", "s.C2");
		expectedCols.put("B", "s.C3");

		List<Condition> expectedWhereClause = new ArrayList<>();
		List<Condition> whereClause;

		result = new SelectStatement("select s.C1, s.C2, s.C3 AS B, xx.C4 as C FROM MyTable s");
		assertEquals("MyTable", result.getFromTable());
		assertEquals("s", result.getFromTableVariable());
		assertEquals(expectedCols, result.getColumns(), "Only columns expected that match the table variable s");
		whereClause = result.getWhereClause();
		assertTrue(whereClause.isEmpty());

		expectedCols = new LinkedHashMap<>();
		expectedCols.put("C1", "C1");
		expectedCols.put("C2", "C2");
		expectedCols.put("B", "C3");

		result = new SelectStatement("select s.C1, s.C2, C3 AS B, xx.C4 as C FROM MyTable");
		assertEquals("MyTable", result.getFromTable());
		assertEquals(null, result.getFromTableVariable());
		assertEquals(Map.of("B", "C3"), result.getColumns(),
				"Only columns without name shall match if table has variable");
		whereClause = result.getWhereClause();
		assertTrue(whereClause.isEmpty());

		expectedCols.put("C", "C4");
		Double d = Double.parseDouble("28.2");
		expectedWhereClause.add(result.new Condition("C1", "=", d));
		expectedWhereClause.add(result.new Condition("C3", "=", "XX"));
		result = new SelectStatement("select C1, C2, C3 AS B, C4 as C FROM MyTable WHERE C1 = 28.2 AND C3 = 'XX'");
		assertEquals("MyTable", result.getFromTable());
		assertEquals(expectedCols, result.getColumns());
		whereClause = result.getWhereClause();
		assertEquals(expectedWhereClause.toString(), whereClause.toString());

		expectedWhereClause = new ArrayList<>();
		expectedWhereClause.add(result.new Condition("C1", "=", Integer.parseInt("-28")));
		result = new SelectStatement("select C1, C2, C3 AS B, C4 as C FROM MyTable WHERE C1 = -28");
		assertEquals("MyTable", result.getFromTable());
		assertEquals(Map.of("C1", "C1", "C2", "C2", "B", "C3", "C", "C4"), result.getColumns());
		whereClause = result.getWhereClause();
		assertEquals(expectedWhereClause.toString(), whereClause.toString());

		result = new SelectStatement("select C1, C2, C3 AS B, C4 as C FROM MyTable");
		assertEquals("MyTable", result.getFromTable());
		assertEquals(Map.of("C1", "C1", "C2", "C2", "B", "C3", "C", "C4"), result.getColumns());
		whereClause = result.getWhereClause();
		assertTrue(whereClause.isEmpty());
	}

}
