/*
 * @(#)InvalidEmailException.java
 *
 * Copyright (c) JSCAPE LLC.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 */

package com.jscape.inspect;

import com.jscape.inet.email.EmailAddress;

/**
 * InvalidEmailException is thrown when an email address is found to be invalid.  At times, network related issues
 * such as the inability to connect to a DNS or SMTP server during validation will cause an InvalidEmailException
 * to be thrown.
 */

public class InvalidEmailException
        extends Exception {

    /**
     * Indicates I/O error communicating with DNS Nameserver or SMTP server.
     */
    public static final int IO_ERROR = 0;

    /**
     * Indicates syntax error in email address.
     */
    public static final int INVALID_SYNTAX_ERROR = 100;

    /**
     * Indicates email address contains a denied domain as defined in EmailInspector instance.
     */
    public static final int DENIED_DOMAIN_ERROR = 200;

    /**
     * Indicates email address contains an invalid domain name.
     */
    public static final int INVALID_DOMAIN_ERROR = 300;

    /**
     * Indicates error locating SMTP server for email address.
     */
    public static final int INVALID_MX_ERROR = 400;

    /**
     * Indicates invalid user portion of email address.
     */
    public static final int INVALID_USER_ERROR = 500;


    private EmailAddress email;
    private int errorCode = 0;

    /**
     * Creates exception with specified message string.
     *
     * @param message the error message
     * @param email   the email address
     */
    public InvalidEmailException(String message, String email) {
        super(message);
        this.email = new EmailAddress(email);
    }


    /**
     * Creates exception with specified message String, email and base exception.
     *
     * @param message   the error message
     * @param email     the email address
     * @param cause the exception which generated this exception
     */
    public InvalidEmailException(String message, String email, Throwable cause) {
        super(message, cause);
        this.email = new EmailAddress(email);
    }

    /**
     * Exception thrown to handle an invalid email.
     *
     * @param email     the email
     * @param errorCode the type of error encountered
     */
    public InvalidEmailException(String email, int errorCode) {
        this.email = new EmailAddress(email);
        this.errorCode = errorCode;
    }

    /**
     * Gets the EmailAddress associated with this exception.
     *
     * @return the email address
     */
    public String getEmail() {
        return email.toString();
    }

    /**
     * Gets error code associated with error.
     *
     * @return error code
     *
     * @see #IO_ERROR
     * @see #INVALID_SYNTAX_ERROR
     * @see #INVALID_DOMAIN_ERROR
     * @see #DENIED_DOMAIN_ERROR
     * @see #INVALID_MX_ERROR
     * @see #INVALID_USER_ERROR
     */
    public int getErrorCode() {
        return errorCode;
    }

    /**
     * Gets error message for this exception.
     *
     * @return error message
     */
    public String getMessage() {
        if (getCause() != null) {
            return getCause().getMessage();
        } else {
            String message = "Invalid error code returned";
            switch (errorCode) {
                case INVALID_SYNTAX_ERROR:
                    message = "Sorry, the syntax of email " + email.getEmail() + " is invalid.";
                    break;
                case DENIED_DOMAIN_ERROR:
                    message = "Sorry, email is denied for domain " + email.getDomain();
                    break;
                case INVALID_DOMAIN_ERROR:
                    message = "Sorry, I couldn't find any host named " + email.getDomain();
                    break;
                case INVALID_MX_ERROR:
                    message = "Sorry, I couldn't find any MX records for host named " + email.getDomain();
                    break;
                case INVALID_USER_ERROR:
                    message = "Sorry, the email " + email.getEmail() + " is not valid on this domain.";
                    break;
                default:
                    break;
            }
            return message;
        }
    }

}
