package com.capgemini.reports.exceptions;

import lombok.Getter;

/**
 * KURT functional input value errors ("not a number" etc), specifically coming from the user.
 * <p>
 * Carries a message explaining to the user why the input is wrong.<br>
 * Also carries contextual information, such as which report has caused this error.
 */
@Getter
public class InvalidUserInputException extends InvalidInputException {

    private static final long serialVersionUID = -8816332546502998913L;

    protected String userInputDynamicError = "";

    /**
     * Builds an <b>Internal Error</b> Exception, with custom message for debugging.
     * 
     * @param anError Error description for devs
     */
    public InvalidUserInputException(final String anError) {
        super(anError);
    }

    /**
     * Builds an <b>Internal Error</b> Exception, with custom message for debugging. Includes the Throwable that caused the error.
     * 
     * @param anError Error description for devs
     * @param originalError the Original Error that caused all the drama
     */
    public InvalidUserInputException(final String anError, final Throwable originalError) {
        super(anError, originalError);
    }

    /**
     * Builds an exception which explains what happened to the user, and to the developer.
     * 
     * @param anError an {@link com.capgemini.reports.exceptions.UserInputErrorEnum} that explains what happened for the user, and for the developper.
     */
    public InvalidUserInputException(final UserInputErrorEnum anError) {
        super(anError.getMsgEn(), anError);
    }

    /**
     * Builds an exception which explains what happened to the user, and to the developer.
     * 
     * @param anError an {@link com.capgemini.reports.exceptions.UserInputErrorEnum} that explains what happened for the user, and for the developper.
     * @param originalError the original {@link Throwable} Exception that caused the failure.
     */
    public InvalidUserInputException(final UserInputErrorEnum anError, final Throwable originalError) {
        super(anError, originalError);
    }


    /**
     * Builds dynamic exception which explains what happened to the user, and to the developer.
     * 
     * @param anError an {@link com.capgemini.reports.exceptions.UserInputErrorEnum} that explains what happened for the user, and for the developper.
     * @param tags String the dynamic parameters to add to the error.
     */
    public InvalidUserInputException(final UserInputErrorEnum anError, final String... tags) {
        super(replaceTags(anError.getMsgEn(), tags));
        this.userInputDynamicError = replaceTags(anError.getMsgFr(), tags);
    }

    private static String replaceTags(final String message, final String[] tags) {
        String msg = message;
        for (String tag : tags) {
            msg = msg.replaceFirst("\\{\\}", tag);
        }
        return msg;
    }

    /**
     * Returns the message to be sent to the user.
     * 
     * @return the message
     */
    @Override
    public String getErrorMessageForUser() {
        if (!userInputDynamicError.isEmpty()) {
            return userInputDynamicError;
        }
        else {
            return userInputError.msgFr;
        }
    }
}
