package com.axelor.apps.purchase.service.print;

import com.axelor.apps.ReportFactory;
import com.axelor.apps.base.exceptions.IExceptionMessage;
import com.axelor.apps.base.service.app.AppBaseService;
import com.axelor.apps.purchase.db.ImportationFolder;
import com.axelor.apps.purchase.report.IReport;
import com.axelor.apps.report.engine.ReportSettings;
import com.axelor.apps.tool.ModelTool;
import com.axelor.apps.tool.ThrowConsumer;
import com.axelor.apps.tool.file.PdfTool;
import com.axelor.exception.AxelorException;
import com.axelor.exception.db.repo.TraceBackRepository;
import com.axelor.i18n.I18n;
import com.axelor.inject.Beans;
import java.io.File;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ImportationFolderPrintService {

  public String printCostPriceSheet(ImportationFolder importationFolder, String formatPdf)
      throws AxelorException {

    String fileName = getImportationFolderName(false, formatPdf);

    return PdfTool.getFileLinkFromPdfFile(print(importationFolder, formatPdf), fileName);
  }

  public String printImportationfolders(List<Long> ids) throws IOException {
    List<File> importationFolders = new ArrayList<>();
    ModelTool.apply(
        ImportationFolder.class,
        ids,
        new ThrowConsumer<ImportationFolder>() {

          public void accept(ImportationFolder importationFolder) throws Exception {
            importationFolders.add(print(importationFolder, ReportSettings.FORMAT_PDF));
          }
        });
    String fileName = getImportationFolderName(true, ReportSettings.FORMAT_PDF);
    return PdfTool.mergePdfToFileLink(importationFolders, fileName);
  }

  public File print(ImportationFolder importationFolder, String formatPdf) throws AxelorException {
    ReportSettings reportSettings = prepareReportSettings(importationFolder, formatPdf);
    return reportSettings.generate().getFile();
  }

  public ReportSettings prepareReportSettings(ImportationFolder importationFolder, String formatPdf)
      throws AxelorException {
    if (importationFolder.getPrintingSettings() == null) {
      throw new AxelorException(
          TraceBackRepository.CATEGORY_MISSING_FIELD,
          String.format(I18n.get(IExceptionMessage.UNIT_CONVERSION_2), importationFolder.getName()),
          importationFolder);
    }
    String locale = ReportSettings.getPrintingLocale(null);
    String title = getFileName(importationFolder);
    ReportSettings reportSetting =
        ReportFactory.createReport(IReport.COST_PRICE_SHEET, title + " - ${date}");

    return reportSetting
        .addParam("importationFolderId", importationFolder.getId())
        .addParam("Locale", locale)
        .addParam("HeaderHeight", importationFolder.getPrintingSettings().getPdfHeaderHeight())
        .addParam("FooterHeight", importationFolder.getPrintingSettings().getPdfFooterHeight())
        .addFormat(formatPdf);
  }

  protected String getImportationFolderName(boolean plural, String formatPdf) {
    return I18n.get(plural ? "Importation folders" : "Importation folder")
        + " - "
        + Beans.get(AppBaseService.class).getTodayDate().format(DateTimeFormatter.BASIC_ISO_DATE)
        + "."
        + formatPdf;
  }

  public String getFileName(ImportationFolder importationFolder) {
    return I18n.get("Importation folder") + " " + importationFolder.getName();
  }
}
