package com.capgemini.reports.checkdata;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.InvalidConfigurationException;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.capgemini.reports.database.DataBaseReportHelper;
import com.capgemini.reports.database.javabean.enumeration.ReportErrorTypeEnum;
import com.capgemini.reports.exceptions.FileLoadingException;
import com.capgemini.reports.exceptions.InvalidUserInputException;
import com.capgemini.reports.exceptions.UserInputErrorEnum;
import com.capgemini.reports.execution.AbstractExecutionReport;
import com.capgemini.reports.reports.parameters.IChecker;
import lombok.extern.slf4j.Slf4j;

/** This class allow to make a control on the user input parameters. */
@Slf4j
public final class CheckingDataHelper {
    private static final String COULD_NOT_LOAD_PROPERTY_POLICIES = "Could not load property policies";

    private CheckingDataHelper() {
        // nothing to do...
    }

    /**
     * Perform a quick check on each of the user input parameters.<br>
     * This method must return quickly, so the check may not be thorough.
     * 
     * @param session the teamcenter session to use to check the parameters.
     * @param abstractExecutionReport the report used to check parameters
     * @throws FileLoadingException if unable to read policies
     * @throws InvalidUserInputException if anything goes wrong with the checks
     */
    public static void doQuickCheck(final TcSession session, final AbstractExecutionReport<?, ?> abstractExecutionReport)
            throws InvalidUserInputException, FileLoadingException {
        PolicyLocation propertyPolicyForChecks = abstractExecutionReport.getPropertyPolicyForLongChecks();

        try {
            session.setObjectPropPolicy(propertyPolicyForChecks);
        } catch (InvalidConfigurationException e) {
            throw new InvalidUserInputException(UserInputErrorEnum.INTERNAL_ERROR, e);
        }

        // Perform each quick check
        for (IChecker parameter : abstractExecutionReport.getQuickChecks()) {
            parameter.check(session);
        }
    }

    /**
     * Perform some in-depth checks of each of the user input parameters.<br>
     * 
     * @param session the teamcenter session to use to check the parameters.
     * @param aAbstractExecutionReport the report on which the tests should be made
     * @throws InvalidUserInputException if the checks failed (wrong parameters, or >maybe< the tc servers were busy)
     */
    public static void doLongCheck(final TcSession session,
                                   final AbstractExecutionReport<?, ?> aAbstractExecutionReport) throws InvalidUserInputException {
        try {
            session.setObjectPropPolicy(aAbstractExecutionReport.getPropertyPolicyForLongChecks());
        } catch (InvalidConfigurationException e) {
            log.error(COULD_NOT_LOAD_PROPERTY_POLICIES);
            throw new InvalidUserInputException(UserInputErrorEnum.INTERNAL_ERROR, e);
        }
        try {
            /* Execute each long check */
            for (IChecker parameter : aAbstractExecutionReport.getLongChecks()) {
                parameter.check(session);
            }
        } catch (InvalidUserInputException e) {
            log.error("Some Long check highlighted anF error in the user-provided parameters", e);
            DataBaseReportHelper.registerError(e.getErrorMessageForUser(), ReportErrorTypeEnum.FUNCTIONAL,
                    aAbstractExecutionReport.getReportTechnicalUid());
            throw e;
        }
    }

}
