package com.capgemini.reports.definition.r03report;

import com.capgemini.framework.common.SettingsHelper;
import com.capgemini.framework.common.TeamcenterDateFormatter;
import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
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.bomlineholder.BOMLineHolder;
import com.capgemini.reports.builder.R03Builder;
import com.capgemini.reports.builder.ReportBuilder;
import com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum;
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.Printer.PrintFormat;
import com.capgemini.reports.reports.parameters.IChecker;
import com.capgemini.reports.reports.parameters.checker.ObjectTypeChecker;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * This class controls the execution of the R03 report.
 */
@Getter
@EqualsAndHashCode(callSuper = true)
public class ExecutionR03 extends AbstractExecutionReport<ExecutionR03, BOMLineHolder> {
    private static final long serialVersionUID = -1050126556474290283L;

    // Policies parameter
    private static final PolicyLocation R03_POLICY_XML_FULL = PolicyLocation.make("R03_policy_full", "/xml/policies/r03report/R03_policy_full.xml");
    private static final PolicyLocation R03_POLICY_XML_LIGHT = PolicyLocation.make("R03_policy_light", "/xml/policies/r03report/R03_policy_light.xml");

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

    // Revision rule parameter
    private static final String REPORT_REVISION_RULE = SettingsHelper.getPropertyWithDefaultValue("common.bom.revisionRule.REPORT_R03", DEFAULT_REVISION_RULE);

    // Launch type parameter
    private static final ReportLaunchTypeEnum REPORT_LAUNCH_TYPE = ReportLaunchTypeEnum.getReportLaunchType("common.launch.launchType.REPORT_R03");

    // Configuration
    private static final String FILE_SEPARATOR = "_";
    private static final String PATTERN_DATE = "dd-MM-yyyy";

    // Internal working data
    private final String partID;
    private final String partRevisionID;

    /**
     * Executes the Report R03.
     *
     * @param partId         {@link String} - the part Id
     * @param partRevisionID {@link String} - the part revision Id
     */
    public ExecutionR03(final String partId, final String partRevisionID) {
        super(REPORT_PRIORITY, REPORT_REVISION_RULE, REPORT_LAUNCH_TYPE);
        this.partID = partId;
        this.partRevisionID = partRevisionID;
    }

    @Override
    public List<CriteriaValue> getLaunchData() {
        List<CriteriaValue> theCriteria = new ArrayList<>();
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.PART_ID, partID));
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.PART_REVISION, partRevisionID));
        return theCriteria;
    }

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

    @Override
    public List<IChecker> getQuickChecks() {
        List<IChecker> quickChecks = new ArrayList<>();
        quickChecks.add(new ObjectTypeChecker(partID, SnecmaObjectTypes.ALTERNATE, SnecmaObjectTypes.DET, SnecmaObjectTypes.PART_AERO,
                SnecmaObjectTypes.PART_ASSEMB, SnecmaObjectTypes.PART_MAT, SnecmaObjectTypes.PART_NO_AERO,
                SnecmaObjectTypes.STANDARD));
        return quickChecks;
    }

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

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

    @Override
    public List<IChecker> getLongChecks() {
        // Empty lists - not used for this report
        return new ArrayList<>();
    }

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

    @Override
    public String getSpecifiedFileName() {
        // Format file expected : BOMParDATE_ "RéférenceArticle"_"RévisionArticle"_"date_système".pdf
        String dateSystem = TeamcenterDateFormatter.formatCalendarToPattern(Calendar.getInstance(), PATTERN_DATE);
        return "R03-BOMParDATE_" + partID + FILE_SEPARATOR + partRevisionID + FILE_SEPARATOR + dateSystem;
    }

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

    @Override
    public IDataReportPreparator<ExecutionR03> getDataPreparator() {
        return new DataReportPreparatorR03();
    }

    @Override
    public AbstractExecutionReport<?, ?> cloneTemplateToExecution() {
        return new ExecutionR03(partID, partRevisionID);
    }

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