package com.capgemini.reports.definition.r15r16r17reports.r16report;

import com.capgemini.framework.common.SettingsHelper;
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.builder.R16Builder;
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.definition.r15r16r17reports.AbstractExecutionR15R16R17;
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.ProductUnit;
import com.capgemini.reports.reports.parameters.checker.ObjectTypeChecker;
import com.capgemini.reports.reports.parameters.checker.ProductUnitGenericChecker;
import com.capgemini.reports.snecmaobjects.admin.RFAdminRevision;
import com.capgemini.reports.snecmaobjects.generic.AbstractGenericRevision;
import com.capgemini.reports.snecmaobjects.part.PartAeroRevision;
import com.capgemini.reports.snecmaobjects.rf.RFGenericRevision;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

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

/**
 * This class controls the execution of a R16 report.
 */
@Getter
@Setter(value = AccessLevel.PROTECTED)
@EqualsAndHashCode(callSuper = true)
public class ExecutionR16 extends AbstractExecutionR15R16R17<ExecutionR16> {
    private static final long serialVersionUID = -3158586550831992551L;

    // Policies
    private static final PolicyLocation R16_POLICY_XML_FULL = PolicyLocation.make("R16_policy_full", "/xml/policies/r16report/R16_policy_full.xml");
    private static final PolicyLocation R16_POLICY_XML_LIGHT = PolicyLocation.make("R16_policy_light", "/xml/policies/r16report/R16_policy_light.xml");
    // Priority parameter
    private static final int REPORT_PRIORITY = SettingsHelper.getPriority(ReportEnum.REPORT_R16);
    // Revision rule parameter
    private static final String REPORT_REVISION_RULE = SettingsHelper.getPropertyWithDefaultValue("common.bom.revisionRule.REPORT_R16", DEFAULT_REVISION_RULE);
    // Launch type parameter
    private static final ReportLaunchTypeEnum REPORT_LAUNCH_TYPE = ReportLaunchTypeEnum.getReportLaunchType("common.launch.launchType.REPORT_R16");
    // Raw input parameters
    protected final ProductUnit theProduitUnitObject;
    // Internal working data
    @Getter(value = AccessLevel.NONE)
    protected final String typeImpression;
    // Data gathered
    protected final boolean isDefCertifiee;
    @Getter(value = AccessLevel.NONE)
    protected RFGenericRevision rfGenericRev;
    protected String articleId;
    protected AbstractGenericRevision genericRev;
    protected List<PartAeroRevision> partNumberRevList = new ArrayList<>();
    protected RFAdminRevision rfAdminRev;
    protected String frontiereAvion = "";
    protected String frontiereCooperant = "";
    @Getter(value = AccessLevel.NONE)
    private String articleRevision;

    /**
     * Constructor.
     *
     * @param produitUnit    {@link ProductUnit} - the Product id and the Unit range.
     * @param idTopGeneric   {@link String} - the id of the top Generic.
     * @param typeImpression {@link String} - the type of impression of the report
     * @param langage        {@link String} - the language selected by the user
     */
    public ExecutionR16(final ProductUnit produitUnit, final String idTopGeneric, final String typeImpression, final String langage) {
        super(idTopGeneric, langage, REPORT_PRIORITY, REPORT_REVISION_RULE, REPORT_LAUNCH_TYPE);
        this.theProduitUnitObject = produitUnit;
        this.isDefCertifiee = "Def_certifiee".equals(typeImpression);
        this.typeImpression = typeImpression;
    }

    @Override
    public List<CriteriaValue> getLaunchData() {
        List<CriteriaValue> theCriteria = new ArrayList<>();
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.PART_ID, itemId));
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.PRODUCT, theProduitUnitObject.getProductID()));
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.UNIT, theProduitUnitObject.getUnitStr()));
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.TYPE_IMPRESSION, typeImpression));
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.LANGUAGE, frenchInput));
        return theCriteria;
    }

    @Override
    public List<IChecker> getQuickChecks() {
        ArrayList<IChecker> quickChecks = new ArrayList<>();
        quickChecks.add(new ProductUnitGenericChecker(itemId, theProduitUnitObject));
        quickChecks.add(new ObjectTypeChecker(itemId, SnecmaObjectTypes.GENERIC));
        return quickChecks;
    }

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

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

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

    @Override
    public List<IChecker> getLongChecks() {
        return new ArrayList<>();
    }

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

    @Override
    public String getSpecifiedFileName() {
        String separator = "_";
        StringBuilder filename = new StringBuilder(articleId).append(separator).append(articleRevision).append(separator).append("FA_");

        // Type impression
        if (isDefCertifiee) {
            filename.append("Certif");
        } else {
            filename.append("Real");
        }

        // Language
        if (!isFrench()) {
            filename.append("_ENG");
        }
        return filename.toString();
    }

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

    @Override
    public IDataReportPreparator<ExecutionR16> getDataPreparator() {
        return new DataReportPreparatorR16();
    }

    @Override
    public AbstractExecutionReport<?, ?> cloneTemplateToExecution() {
        return new ExecutionR16(theProduitUnitObject, articleId, typeImpression, frenchInput);
    }

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