package com.capgemini.reports.definition.r19report;

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.R19Builder;
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.r19report.ExecutionR19.ReportDataR19;
import com.capgemini.reports.documentationobjects.AbstractDoc;
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.ModelObjectChecker;
import com.capgemini.reports.snecmaobjects.AbstractSnecmaObject;
import lombok.AllArgsConstructor;
import lombok.Getter;

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

/**
 * This class controls the execution of an R19 report.
 */
@Getter
public class ExecutionR19 extends AbstractExecutionReport<ExecutionR19, ReportDataR19> {

    private static final long serialVersionUID = 4050093665599652815L;

    // Policies parameters
    private static final PolicyLocation R19_POLICY_XML_FULL = PolicyLocation.make("R19_policy_full", "/xml/policies/r19report/R19_policy_full.xml");
    private static final PolicyLocation R19_POLICY_XML_LIGHT = PolicyLocation.make("R19_policy_light", "/xml/policies/r19report/R19_policy_light.xml");

    // Configuration
    private static final String FILE_SEPARATOR = "_";
    // Launch type parameter
    private static final ReportLaunchTypeEnum REPORT_LAUNCH_TYPE = ReportLaunchTypeEnum.getReportLaunchType("common.launch.launchType.REPORT_R19");
    // Revision rule parameter
    private static final String REPORT_REVISION_RULE = SettingsHelper.getPropertyWithDefaultValue("common.bom.revisionRule.REPORT_R19", DEFAULT_REVISION_RULE);
    // Priority parameter
    private static final int REPORT_PRIORITY = SettingsHelper.getPriority(ReportEnum.REPORT_R19);
    private final String documentId;
    private final String documentRev;
    private final String documentType;
    private final String documentUID;

    /**
     * Executes the Report R19.
     *
     * @param documentId   the ID of the document
     * @param documentRev  the revision of the Document
     * @param documentType the Type of the document
     * @param documentUID  the UID of the document
     */
    public ExecutionR19(final String documentId, final String documentRev, final String documentType, final String documentUID) {
        super(REPORT_PRIORITY, REPORT_REVISION_RULE, REPORT_LAUNCH_TYPE);
        this.documentId = documentId;
        this.documentRev = documentRev;
        this.documentType = documentType;
        this.documentUID = documentUID;
    }


    @Override
    public List<IChecker> getQuickChecks() {
        List<IChecker> quickChecks = new ArrayList<>();
        quickChecks.add(new ModelObjectChecker(documentUID, SnecmaObjectTypes.DOC_NORM, SnecmaObjectTypes.DOC_NORM_STANDARD,
                SnecmaObjectTypes.DOC_DRAWING_REVISION, SnecmaObjectTypes.DOC_TECHNIC_REVISION));
        return quickChecks;
    }

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

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

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

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

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


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

    @Override
    public List<CriteriaValue> getLaunchData() {
        List<CriteriaValue> theCriteria = new ArrayList<>();
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.DOCUMENT_ID, documentId));
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.DOCUMENT_REVISION, documentRev));
        theCriteria.add(new CriteriaValue(ReportCriteriaEnum.TYPE_OBJET, documentType));
        return theCriteria;
    }


    @Override
    public String getSpecifiedFileName() {
        if (documentRev.isEmpty()) {
            return "R19_" + documentId;
        } else {
            return "R19_" + documentId + FILE_SEPARATOR + documentRev;
        }
    }

    @Override
    public IDataReportPreparator<ExecutionR19> getDataPreparator() {
        return new DataReportPreparatorR19();
    }

    @Override
    public AbstractExecutionReport<?, ?> cloneTemplateToExecution() {
        return new ExecutionR19(documentId, documentRev, documentType, documentUID);
    }

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

    /**
     * POJO container of all data required for this report.
     */
    @AllArgsConstructor
    @Getter
    static class ReportDataR19 {
        private final AbstractDoc document;
        private final List<AbstractSnecmaObject> objectsWhereReferenced;
    }
}
