package com.capgemini.reports.definition.r16report;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.InvalidConfigurationException;
import com.capgemini.framework.teamcenter.exceptions.KurtException;
import com.capgemini.framework.teamcenter.exceptions.NoTeamcenterConnectionException;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.capgemini.reports.builder.R16Builder;
import com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum;
import com.capgemini.reports.definition.r15r16r17reports.r16report.DataReportPreparatorR16;
import com.capgemini.reports.definition.r15r16r17reports.r16report.ExecutionR16;
import com.capgemini.reports.execution.strategies.ImmediateExecutionStrategy;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.SoftAssertions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

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


@Slf4j
public class Test_R16 {
  private static final TcSession session = new TcSession();
  private SoftAssertions softAssert = new SoftAssertions();
  // Dossier parent du fichier de référence
  private static final String INPUT_FOLDER = "com.capgemini.report.location.default.input";
  // Sous-dossier du fichier de référence
  private static final String SPECIFIC_INPUT_FOLDER = "r16";
  public static final String PRINTINGFOLDER = "com.capgemini.report.location.default.output";
  // Dossier de stockage temporaire des fichiers générés initialement
  private static final String TEMPORARY_OUTPUT_FOLDER = "com.capgemini.report.location.default.output";
  private static final PolicyLocation XML_POLICY = PolicyLocation.make("test_policy", "/xml/policies/r16report/R16_policy_full.xml");
  public static final String SUFFIX = ".pdf";
  public Properties localproperties;


  @BeforeClass
  public void login() throws NoTeamcenterConnectionException, InvalidConfigurationException, IOException {
    session.login();
    session.setObjectPropPolicy(XML_POLICY);
    localproperties = new Properties();
    localproperties.load(getClass().getResourceAsStream("/reporting.properties"));
  }

  @AfterClass
  public void logout() {
    session.close();
  }

  @BeforeMethod
  //TODO : Supprimer pdf files dans Generated, checker Tempfile TempDirectory


  @Parameters({"product", "unitStr", "idTopGeneric", "typeImpression", "language", "refFile"})
  @Test
  public void test16(String product, String unitStr, String idTopGeneric,
                     String typeImpression, String language, String refFile) throws KurtException, IOException {

    // Génération du rapport de test
    Map<ReportCriteriaEnum, String> data = new HashMap<>();
    data.put(ReportCriteriaEnum.PRODUCT, product);
    data.put(ReportCriteriaEnum.PART_ID, idTopGeneric);
    data.put(ReportCriteriaEnum.UNIT, unitStr);
    data.put(ReportCriteriaEnum.TYPE_IMPRESSION, typeImpression);
    data.put(ReportCriteriaEnum.LANGUAGE, language);

    ExecutionR16 execution = new R16Builder().build(data);
    new DataReportPreparatorR16().print(session, execution, new ImmediateExecutionStrategy());


    PdfReader readerRef = new PdfReader(localproperties.getProperty(INPUT_FOLDER) + SPECIFIC_INPUT_FOLDER + "/" + refFile);
    PdfReader readerGen = new PdfReader(localproperties.getProperty((TEMPORARY_OUTPUT_FOLDER)) + execution.getFileNameOnDisc() + SUFFIX);

    try {
      softAssert.assertThat(readerRef.getNumberOfPages()).isEqualTo(readerGen.getNumberOfPages());
      for (int i = 1; i < readerRef.getNumberOfPages(); i++) {
        String textFromPageRef = new PdfTextExtractor(readerRef).getTextFromPage(i);
        String textFromPageGen = new PdfTextExtractor(readerGen).getTextFromPage(i);
        softAssert.assertThat(textFromPageRef).isEqualTo(textFromPageGen);
      }
    }
    finally {
      readerGen.close();
      readerRef.close();
    }
    softAssert.assertAll();
  }
}