package com.axelor.apps.stock.service.stockmove.print;

import com.axelor.apps.ReportFactory;
import com.axelor.apps.base.service.app.AppBaseService;
import com.axelor.apps.report.engine.ReportSettings;
import com.axelor.apps.stock.db.StockProductionRequest;
import com.axelor.apps.stock.exception.IExceptionMessage;
import com.axelor.apps.stock.report.IReport;
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 StockProductionRequestPrintService {

  public String printStockProductionRequest(
      StockProductionRequest purchaseRequest, String formatPdf) throws AxelorException {

    String fileName = getPurchaseRequestFilesName(false, formatPdf);

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

  public String printStockProductionRequests(List<Long> ids) throws IOException {
    List<File> printedPurchaseRequests = new ArrayList<>();
    ModelTool.apply(
        StockProductionRequest.class,
        ids,
        new ThrowConsumer<StockProductionRequest>() {

          public void accept(StockProductionRequest purchaseRequest) throws Exception {
            printedPurchaseRequests.add(print(purchaseRequest, ReportSettings.FORMAT_PDF));
          }
        });
    String fileName = getPurchaseRequestFilesName(true, ReportSettings.FORMAT_PDF);
    return PdfTool.mergePdfToFileLink(printedPurchaseRequests, fileName);
  }

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

  public ReportSettings prepareReportSettings(
      StockProductionRequest stockProductionRequest, String formatPdf) throws AxelorException {
    if (stockProductionRequest.getPrintingSettings() == null) {
      throw new AxelorException(
          TraceBackRepository.CATEGORY_MISSING_FIELD,
          String.format(
              I18n.get(IExceptionMessage.STOCK_MOVE_MISSING_TEMPLATE),
              stockProductionRequest.getStockProductionRequestSeq()),
          stockProductionRequest);
    }
    String locale = ReportSettings.getPrintingLocale(null);
    String title = getFileName(stockProductionRequest);
    ReportSettings reportSetting =
        ReportFactory.createReport(IReport.STOCK_PRODUCTION_REQUEST, title + " - ${date}");

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

  protected String getPurchaseRequestFilesName(boolean plural, String formatPdf) {
    return I18n.get(plural ? "Stock production requests" : "Stock production request")
        + " - "
        + Beans.get(AppBaseService.class).getTodayDate().format(DateTimeFormatter.BASIC_ISO_DATE)
        + "."
        + formatPdf;
  }

  public String getFileName(StockProductionRequest StockProductionRequest) {
    return I18n.get("Stock production request")
        + " "
        + StockProductionRequest.getStockProductionRequestSeq();
  }
}
