package com.capgemini.reports.jasper.fillers;

import com.capgemini.reports.jasper.compilers.JasperCompiler;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import org.testng.annotations.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
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 MultiFillerTest {

  private static final String SUBREPORT_P1 = "SUBREPORT_P1";
  private static final String JASPER_TEMPLATE_PAGE1 = "/Jasper/template/report_r20/report_r20_p1.jrxml";
  private static final String JASPER_TEMPLATE_MASTER = "/Jasper/template/report_r20/report_r20_master.jrxml";


  @Test
  public void fillTest() throws JRException, IOException {
    InputStream templateStream = getClass().getResourceAsStream(JASPER_TEMPLATE_MASTER);
    InputStream page1Stream = getClass().getResourceAsStream(JASPER_TEMPLATE_PAGE1);
    JasperReport mainReport = JasperCompiler.compile(templateStream);
    JasperReport page1Report = JasperCompiler.compile(page1Stream);
    MultiFiller filler = new MultiFiller(mainReport, setupEmptyData(), setupMainParameters());
    filler.addSubReport(SUBREPORT_P1, page1Report, setupPage1Data(5), setupPage1Parameters());
    JasperPrint print = filler.generate();
    assertThat(print.getPages()).isNotEmpty();
  }

  private List<Map<String, ?>> setupEmptyData() {
    List<Map<String, ?>> list = new ArrayList<>();
    list.add(new HashMap<String, Object>());
    return list;
  }

  private Map<String, Object> setupMainParameters() throws IOException {
    Map<String, Object> parametersMap = new HashMap<>();
    try (InputStream imageStream = getClass().getResourceAsStream("/Jasper/images/Snecma_logo.png")) {
      BufferedImage image = ImageIO.read(imageStream);
      parametersMap.put("SNECMA_LOGO", image);
      parametersMap.put("report_title", "Titre rapport principal");
      return parametersMap;
    }
  }

  private List<Map<String, ?>> setupPage1Data(final int numberOfElements) {
    List<Map<String, ?>> lmrMaps = new ArrayList<>();
    for (int num = 0; num < numberOfElements; num++) {
      Map<String, String> lmrMap = new HashMap<>();
      lmrMap.put("CHANGE_REQUEST_APPLI", "APPLI-" + num);
      lmrMap.put("CHANGE_REQUEST_IDENTIFIANT", "ID-" + num);
      lmrMap.put("CHANGE_REQUEST_NOM", "NOM-" + num);
      lmrMap.put("CHANGE_REQUEST_NO_APPRO_SO", "NO_APPRO-" + num);
      lmrMaps.add(lmrMap);
    }
    return lmrMaps;
  }

  private Map<String, Object> setupPage1Parameters() {
    Map<String, Object> map = new HashMap<>();
    map.put("PRODUIT", "produit");
    map.put("UNIT_AVANT", "30");
    map.put("UNIT_APRES", "40");
    map.put("EXCEPTIONNEL_AVANT", "oui");
    map.put("EXCEPTIONNEL_APRES", "non");
    map.put("TITRE_P1", "titre 1");
    map.put("TITRE_P2", "titre 2");
    map.put("TITRE_P3", "titre 3");
    map.put("TITRE_P4", "titre 4");
    map.put("DOMAINE_PRODUIT_AVANT", "domaine avant");
    map.put("DOMAINE_PRODUIT_APRES", "domaine apres");
    map.put("LIGNE_PRODUIT_AVANT", "ligne produit avant");
    map.put("LIGNE_PRODUIT_APRES", "ligne produit apres");
    map.put("MATRICULE_MOTEUR", "03435841351");
    return map;
  }
}
