package com.capgemini.reports.common;

import com.capgemini.framework.teamcenter.exceptions.KurtException;
import com.capgemini.framework.teamcenter.exceptions.NoTeamcenterConnectionException;
import com.capgemini.framework.teamcenter.exceptions.runtime.ConnectionLostException;
import com.capgemini.reports.exceptions.InvalidInputException;
import com.capgemini.reports.exceptions.InvalidUserInputException;
import com.capgemini.reports.exceptions.UserInputErrorEnum;
import org.json.JSONObject;
import org.quartz.SchedulerException;

import javax.jms.JMSException;
import javax.naming.NamingException;

/** Helper class that provides several error and success messages with standardized KEYS and TOKENS that are understood by the HMI. */
public final class ServletMessageHelper {

    private ServletMessageHelper() {

    }

    /**
     * TOKEN to obtain request TcReleaseStatus from the JSON response.
     */
    public static final String TOKEN_STATUS = "status";
    /** TOKEN to obtain error explanation from the JSON response when STATUS is FAILED. */
    public static final String TOKEN_ERROR = "error";
    /** TOKEN to obtain success message from the JSON response. */
    public static final String TOKEN_SUCCESS_MSG = "success";

    /** STATUS value for the JSON response when posting is successful. */
    public static final String STATUS_SUCCESS = TOKEN_SUCCESS_MSG;
    /** STATUS value for the JSON response when posting has failed. */
    public static final String STATUS_FAILED = "failed";
    /** STATUS value for the JSON response when user should be prompted before actually launching the report. */
    public static final String STATUS_PROMPT = "prompt";

    protected static final String POSTING_ERROR_MESSAGE_EN = "Error caught while trying to submit the report into the Queue";
    protected static final String POSTING_ERROR_MESSAGE_FR =
            new InvalidUserInputException(UserInputErrorEnum.INTERNAL_ERROR).getErrorMessageForUser();
    private static final String DELAY_ERROR_MESSAGE_FR = "Impossible de planifier une ex&eacute;cution";
    private static final String TEAMCENTER_UNAVAILABLE_EXCEPTION_MSG_FR = "Impossible de communiquer avec le serveur Teamcenter";
    private static final String TEAMCENTER_CONNECTION_LOST_EXCEPTION_MSG_FR =
            "Le serveur Teamcenter a cess&eacute; de r&eacute;pondre, r&eacute;essayez ult&eacute;rieurement. Si le probl&egrave;me persiste, contactez l'administrateur.";

    /**
     * Helper function to return standardized success messages to the client.
     * 
     * @param msg the message
     * @return the {@link JSONObject}
     */
    public static JSONObject successMessage(final String msg) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_SUCCESS).put(TOKEN_SUCCESS_MSG, msg);
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a fault is found.
     * 
     * @param msg the message
     * @return the {@link JSONObject}
     */
    public static JSONObject errorMessage(final String msg) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, msg);
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a InvalidUserInputException is triggered by a user.
     * 
     * @param exception the {@link InvalidUserInputException} that caused the failure and contains a nice error message for the user
     * @return the {@link JSONObject} containing the status and a nice error message for the user
     */
    public static JSONObject errorMessage(final InvalidUserInputException exception) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, exception.getErrorMessageForUser());
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a InvalidInputException occurred.
     * 
     * @param exception the {@link InvalidInputException} that caused the failure and contains a nice error message for the user
     * @return the {@link JSONObject} containing the status and a nice error message for the user
     */
    public static JSONObject errorMessage(final InvalidInputException exception) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, exception.getErrorMessageForUser());
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a fault is found.
     * 
     * @param exception the generic {@link KurtException} that caused the failure
     * @return the {@link JSONObject} containing the status and a generic error message
     */
    public static JSONObject errorMessage(final KurtException exception) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, exception.getMessage());
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a NamingException occurred.
     * 
     * @param exception the {@link NamingException} that caused the failure
     * @return the {@link JSONObject} containing the status and an error message
     */
    public static JSONObject errorMessage(final NamingException exception) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, ServletMessageHelper.POSTING_ERROR_MESSAGE_FR);
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a JMSException occurred.
     * 
     * @param exception the {@link JMSException} that caused the failure
     * @return the {@link JSONObject} containing the status and an error message
     */
    public static JSONObject errorMessage(final JMSException exception) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, ServletMessageHelper.POSTING_ERROR_MESSAGE_FR);
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a SchedulerException occurred.
     * 
     * @param exception the {@link SchedulerException} that caused the failure
     * @return the {@link JSONObject} containing the status and an error message
     */
    public static JSONObject errorMessage(final SchedulerException exception) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, ServletMessageHelper.DELAY_ERROR_MESSAGE_FR);
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a NoTeamcenterConnectionException occurred.
     * 
     * @param exception the {@link NoTeamcenterConnectionException} that caused the failure
     * @return the {@link JSONObject} containing the status and an error message
     */
    public static JSONObject errorMessage(final NoTeamcenterConnectionException exception) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, ServletMessageHelper.TEAMCENTER_UNAVAILABLE_EXCEPTION_MSG_FR);
    }

    /**
     * Helper function for producing standardized JSON error messages to be sent to server in case a ConnectionLostException occurred.
     * 
     * @param exception the {@link ConnectionLostException} that caused the failure
     * @return the {@link JSONObject} containing the status and an error message
     */
    public static JSONObject errorMessage(final ConnectionLostException exception) {
        return new JSONObject().put(TOKEN_STATUS, STATUS_FAILED).put(TOKEN_ERROR, ServletMessageHelper.TEAMCENTER_CONNECTION_LOST_EXCEPTION_MSG_FR);
    }

}
