package com.capgemini.reports.definition.r28report;

import com.capgemini.framework.common.ReportFolderOutputHelper;
import com.capgemini.framework.common.SettingsHelper;
import com.capgemini.framework.common.TeamcenterDateFormatter;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.capgemini.reports.JasperTemplate;
import com.capgemini.reports.ReportEnum;
import com.capgemini.reports.ReportLaunchTypeEnum;
import com.capgemini.reports.builder.R28Builder;
import com.capgemini.reports.builder.ReportBuilder;
import com.capgemini.reports.database.manager.MngCriteriaVal.CriteriaValue;
import com.capgemini.reports.execution.AbstractExecutionReport;
import com.capgemini.reports.execution.IDataReportPreparator;
import com.capgemini.reports.printing.PrintParameters;
import com.capgemini.reports.printing.Printer;
import com.capgemini.reports.printing.Printer.PrintFormat;
import com.capgemini.reports.reports.parameters.IChecker;
import com.teamcenter.soa.client.model.strong.Item;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * Obtain data for report R28.<br>
 * Contain {@link IChecker} for a report's construction
 */
@Getter
@EqualsAndHashCode(callSuper = true)
public class ExecutionR28 extends AbstractExecutionReport<ExecutionR28, ExecutionR28.ReportDataR28> {
  /**
   * Policy for inspecting Alternates and DETs more closely (i.e. looking into their pseudo-folders for alternative parts).
   */
  public static final PolicyLocation R28_POLICY_XML_ALT = PolicyLocation.make("R28_policy_alt", "/xml/policies/r28report/R28_policy_alt.xml");
  private static final long serialVersionUID = -5027358935089122516L;
  private static final IDataReportPreparator DATA_REPORT_PREPARATOR_R28 = new DataReportPreparatorR28();
  // Policies parameters
  private static final PolicyLocation R28_POLICY_XML_LIGHT = PolicyLocation.make("R28_policy_light", "/xml/policies/r28report/R28_policy_light.xml");
  private static final PolicyLocation R28_POLICY_XML_FULL = PolicyLocation.make("R28_policy_full", "/xml/policies/r28report/R28_policy_full.xml");
  // Launch type parameter
  private static final ReportLaunchTypeEnum REPORT_LAUNCH_TYPE = ReportLaunchTypeEnum.getReportLaunchType("common.launch.launchType.REPORT_R28");

  // Priority parameter
  private static final int REPORT_PRIORITY = SettingsHelper.getPriority(ReportEnum.REPORT_R28);

  private static final String FILE_SEPARATOR = "_";
  private static final String TITLE = "Export_Repere_fonctionnel";
  private static final String PATTERN_DATE = "dd-MM-yyyy";

  /**
   * Constructor.
   */
  public ExecutionR28(List<Item> queryObjects) {
    super(REPORT_PRIORITY, REPORT_LAUNCH_TYPE);
  }


  /**
   * Empty constructor.
   */
  public ExecutionR28() {
    super(REPORT_PRIORITY, REPORT_LAUNCH_TYPE);
  }


  @Override
  public String getSpecifiedFileName() {
    StringBuilder builder = new StringBuilder("R28").append(FILE_SEPARATOR).append(TITLE).append(FILE_SEPARATOR);
    builder.append(TeamcenterDateFormatter.formatCalendarToPattern(Calendar.getInstance(), PATTERN_DATE));
    return builder.toString();
  }

  @Override
  public PrintFormat getPrintingFormat() {
    return PrintFormat.CSV;
  }

  @Override
  public List<CriteriaValue> getLaunchData() {
    return Collections.emptyList();
  }

  @Override
  public ReportEnum getReportIdentifier() {
    return ReportEnum.REPORT_R28;
  }


  @Override
  public PolicyLocation getPropertyPolicyForLongChecks() {
    return R28_POLICY_XML_LIGHT;
  }

  @Override
  public PolicyLocation getFullPolicy() {
    return R28_POLICY_XML_FULL;
  }

  @Override
  public List<IChecker> getLongChecks() {
    return Collections.emptyList();
  }

  @Override
  public String getTemplateLocation() {
    return JasperTemplate.TEMPLATE_R28.getFilepath();
  }

  @Override
  public IDataReportPreparator<ExecutionR28> getDataPreparator() {
    return DATA_REPORT_PREPARATOR_R28;
  }

  @Override
  public List<IChecker> getQuickChecks() {
    return Collections.emptyList();
  }

  @Override
  public Class<? extends ReportBuilder> getReportBuilderClass() {
    return R28Builder.class;
  }

  @Override
  public AbstractExecutionReport<?, ?> cloneTemplateToExecution() {
    return null;
  }

  public PrintParameters reportParameters() {
    return new PrintParameters(PrintFormat.CSV, getFileNameOnDisc(), ReportFolderOutputHelper.getStorageFolderLocation(getLaunchType()));
  }

  /**
   * POJO container of all data required for this report.
   */
  static class ReportDataR28 {
    private final List<Item> parts;

    public ReportDataR28(List<Item> parts) {
      this.parts = parts;
    }

    public List<Item> getParts() {
      return parts;
    }
  }

}
