package com.capgemini.reports.templates;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;


@Slf4j
public class CellTest {

  private static final int INITIAL_HEIGHT = 20;
  private static final int EXTRA_HEIGHT = 8;
  private static final String CONTENT = "Some Content";

  private final String expectedCell = ""
      + "\n            <staticText>"
      + "\n                <reportElement x=\"0\" y=\"30\" width=\"50\" height=\"20\"/>"
      + "\n                <box leftPadding=\"5\">"
      + "\n                    <topPen lineWidth=\"0.5\"/>"
      + "\n                    <leftPen lineWidth=\"0.5\"/>"
      + "\n                    <bottomPen lineWidth=\"0.5\"/>"
      + "\n                    <rightPen lineWidth=\"0.5\"/>"
      + "\n                </box>"
      + "\n                <textElement markup=\"html\">"
      + "\n\n                </textElement>"
      + "\n                <text><![CDATA[Some Content]]></text>"
      + "\n            </staticText>";

  private final String expectedStretchCell = ""
      + "\n            <textField isStretchWithOverflow=\"true\">"
      + "\n                <reportElement stretchType=\"RelativeToTallestObject\" x=\"0\" y=\"30\" width=\"50\" height=\"20\"/>"
      + "\n                <box leftPadding=\"5\">"
      + "\n                    <topPen lineWidth=\"0.5\"/>"
      + "\n                    <leftPen lineWidth=\"0.5\"/>"
      + "\n                    <bottomPen lineWidth=\"0.5\"/>"
      + "\n                    <rightPen lineWidth=\"0.5\"/>"
      + "\n                </box>"
      + "\n                <textElement markup=\"html\">"
      + "\n\n                </textElement>"
      + "\n                <textFieldExpression><![CDATA[$P{Some Content}]]></textFieldExpression>"
      + "\n            </textField>";

  private final String expectedGreyCell = ""
      + "\n            <staticText>"
      + "\n                <reportElement x=\"0\" y=\"30\" width=\"50\" height=\"20\" mode=\"Opaque\" backcolor=\"#E3E3E3\"/>"
      + "\n                <box leftPadding=\"5\">"
      + "\n                    <topPen lineWidth=\"0.5\"/>"
      + "\n                    <leftPen lineWidth=\"0.5\"/>"
      + "\n                    <bottomPen lineWidth=\"0.5\"/>"
      + "\n                    <rightPen lineWidth=\"0.5\"/>"
      + "\n                </box>"
      + "\n                <textElement markup=\"html\">"
      + "\n\n                </textElement>"
      + "\n                <text><![CDATA[Some Content]]></text>"
      + "\n            </staticText>";

  private final String expectedJustifiedCell = ""
      + "\n            <staticText>"
      + "\n                <reportElement x=\"0\" y=\"30\" width=\"50\" height=\"20\"/>"
      + "\n                <box leftPadding=\"5\">"
      + "\n                    <topPen lineWidth=\"0.5\"/>"
      + "\n                    <leftPen lineWidth=\"0.5\"/>"
      + "\n                    <bottomPen lineWidth=\"0.5\"/>"
      + "\n                    <rightPen lineWidth=\"0.5\"/>"
      + "\n                </box>"
      + "\n                <textElement textAlignment=\"Justified\" markup=\"html\">"
      + "\n\n                </textElement>"
      + "\n                <text><![CDATA[Some Content]]></text>"
      + "\n            </staticText>";

  @Test
  public void testMakeStaticCell() {
    Cell cell = Cell.makeStaticCell(CONTENT, INITIAL_HEIGHT, 50, 12, 30, null);
    assertThat(cell.isDynamic()).isFalse();
    assertThat(CONTENT).isEqualTo(cell.getContent());
    assertThat(INITIAL_HEIGHT).isEqualTo(cell.getHeight());
    assertThat("50").isEqualTo(cell.getWidth());
    assertThat("12").isEqualTo(cell.getXPosition());
    assertThat("30").isEqualTo(cell.getYPosition());
    assertThat(cell.isStretchWithOverflow()).isFalse();
  }

  @Test
  public void testMakeParameterCell() {
    Cell cell = Cell.makeParameterCell(CONTENT, INITIAL_HEIGHT, 50, false, 12, 30, null);
    assertThat(cell.isDynamic()).isTrue();
    assertThat(cell.isHeader()).isTrue();
    assertThat(CONTENT).isEqualTo(cell.getContent());
    assertThat(INITIAL_HEIGHT).isEqualTo(cell.getHeight());
    assertThat("50").isEqualTo(cell.getWidth());
    assertThat("12").isEqualTo(cell.getXPosition());
    assertThat("30").isEqualTo(cell.getYPosition());
    assertThat(cell.isStretchWithOverflow()).isFalse();
  }

  @Test
  public void testCellWithOverflow() {
    Cell cell = Cell.makeParameterCell(CONTENT, INITIAL_HEIGHT, 50, true, 12, 30, null);
    assertThat(cell.isDynamic()).isTrue();
    assertThat(CONTENT).isEqualTo(cell.getContent());
    assertThat(INITIAL_HEIGHT).isEqualTo(cell.getHeight());
    assertThat("50").isEqualTo(cell.getWidth());
    assertThat("12").isEqualTo(cell.getXPosition());
    assertThat("30").isEqualTo(cell.getYPosition());
    assertThat(cell.isStretchWithOverflow()).isTrue();
  }

  @Test
  public void testMakeFieldCell() {
    Cell cell = Cell.makeFieldCell(CONTENT, INITIAL_HEIGHT, 50, false, 0, 30, null);
    assertThat(cell.isDynamic()).isTrue();
    assertThat(cell.isHeader()).isFalse();
    assertThat(CONTENT).isEqualTo(cell.getContent());
  }

  @Test
  public void testMakeCell() {
    Cell cell = Cell.makeStaticCell(CONTENT, INITIAL_HEIGHT, 50, 0, 30, null);
    assertThat(expectedCell).isEqualTo(cell.makeCell());
  }

  @Test
  public void testStretchCell() {
    Cell cell = Cell.makeParameterCell(CONTENT, INITIAL_HEIGHT, 50, true, 0, 30, null);
    assertThat(expectedStretchCell).isEqualTo(cell.makeCell());
  }

  @Test
  public void testAddHeight() {
    Cell cell = Cell.makeStaticCell(CONTENT, INITIAL_HEIGHT, 50, 0, 30, null);
    assertThat(INITIAL_HEIGHT).isEqualTo(cell.getHeight());
    cell.addHeight(EXTRA_HEIGHT);
    assertThat(INITIAL_HEIGHT + EXTRA_HEIGHT).isEqualTo(cell.getHeight());
  }

  @Test
  public void testGreyCell() {
    Cell cell = Cell.makeStaticCell(CONTENT, INITIAL_HEIGHT, 50, 0, 30, new CellFormat().setColor("#E3E3E3"));
    assertThat(expectedGreyCell).isEqualTo(cell.makeCell());
  }

  @Test
  public void testJustifiedCell() {
    Cell cell = Cell.makeStaticCell(CONTENT, INITIAL_HEIGHT, 50, 0, 30, new CellFormat().setAlignment(Alignment.JUSTIFIED));
    assertThat(expectedJustifiedCell).isEqualTo(cell.makeCell());
  }
}
