package com.capgemini.reports.templates;

import com.capgemini.framework.common.ResourceHelper;
import com.capgemini.reports.exceptions.FileLoadingException;
import com.capgemini.reports.exceptions.InvalidInputException;
import com.capgemini.reports.jasper.compilers.JasperCompiler;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import org.testng.annotations.Test;

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

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


public class DynamicGridTemplateBuilderTest {

  private static final String COLOR = "#E3E3E3";

  @Test
  public void generateCodeTest() throws JRException, FileLoadingException, InvalidInputException {
    // iInitialise the data
    String refContent = ResourceHelper.getResourceContent("/Jasper/template/dynamic_table/reference.jrxml");
    List<String> header1 = new ArrayList<>();
    List<String> header2 = new ArrayList<>();
    List<Boolean> dynamicHeader = new ArrayList<>();
    List<Integer> widths = new ArrayList<>();
    List<String> keys = new ArrayList<>();

    Map<String, String> aContentLine = new HashMap<>();
    Map<String, Object> parameters = new HashMap<>();
    for (int col = 0; col < 5; col++) {
      if (col == 0) {
        header1.add("Top Left");
        header2.add("Line Headers");
        dynamicHeader.add(false);
        widths.add(200);
        String key = "ROW_HEADER" + col;
        keys.add(key);
        aContentLine.put(key, "Row Number 0");
      }
      else {
        String headerKey = "COL_HEADER" + col;
        header1.add(headerKey);
        parameters.put(headerKey, "Column Number " + col);
        header2.add(null); // Merge with the above
        dynamicHeader.add(true);
        widths.add(50);
        String contentKey = "COL" + col;
        keys.add(contentKey);
        aContentLine.put(contentKey, "Value " + col);
      }
    }

    // Build the template
    DynamicGridTemplateBuilder template = new DynamicGridTemplateBuilder(widths, keys, null, null, 13);
    template.addHeaderLine(13, header1, dynamicHeader, new CellFormat().setColor(COLOR), null);
    template.addHeaderLine(26, header2, dynamicHeader, new CellFormat().setColor(COLOR), null);
    String generatedTemplate = template.generateCode();
    assertThat(refContent).isEqualTo(generatedTemplate);

    // Compile
    JasperReport compiledTemplate = JasperCompiler.compile(generatedTemplate);
    assertThat(compiledTemplate).isNotNull();
  }
}
