/**
 * User: Brian Roy
 * Date: 6/6/11
 * Time: 2:36 PM
 *
 */

function mvcErrorHandler() {
    // Public constants
    this.SEVERE  = 'SEVERE';
    this.INFO    = 'INFO';
    this.WARNING = 'WARNING';
    this.DEBUG   = 'DEBUG';

    this.IGNORE     = 'ignore';
    this.MESSAGEBOX = 'msgbox';

    var self = this;

    // private members
    var objName      = '';
    var methodName   = '';
    var errSeverity  = '';
    var errMessage   = '';
    var restResource = 'http://' + window.location.host + '/api/clogger/v1/';

    var arySev = Array();
    arySev[0] = 'DEBUG';
    arySev[1] = 'INFO';
    arySev[2] = 'WARNING';
    arySev[3] = 'SEVERE';

    var minCLogSeverity = this.WARNING;

    /**
     * Returns the msgBox dialog type derived from the severity.
     * @param sev
     * @return string The msgBox type.
     */
    this.getMsgBoxType = function(sev) {
        switch(sev) {
            case self.SEVERE:
                return "error"
                break;
            case self.WARNING:
                return "warning"
                break;
            case self.INFO:
                return "info";
                break;
            case self.DEBUG:
                return "info";
                break;

        }
        return false;
    }

    /**
     * Determines if the error severity causes logging based on the
     * minimum severity supported.
     * @param sev
     * @return boolean
     */
    this.isLoggable = function(sev) {
        var min = -1;
        var thisSev = -1;
        for(var i in arySev) {
            if(arySev[i] == minCLogSeverity) min = i;
            if(arySev[i] == sev) thisSev = i;
        }
        if(thisSev >= min) return true;
        return false;
    }

    /**
     * Logs the error using an async ajax request.
     *
     */
    this.logError = function() {
        if(typeof(isAsync) == 'undefined') isAsync = true;
        var errType = "error";
        if(errSeverity == this.DEBUG || errSeverity == this.INFO)
            errType = "message";

        var clientTS = new Date();
        clientTS = clientTS.toLocaleString();
        var msg = errSeverity + ": " + clientTS + " - " + this.errMessage + " in Object: " + this.objName + " Method: " + this.methodName;

        var reqData = "msg=" + encodeURIComponent(msg) + "&type=" + errType;

        /*
         * Set default values if optional are empty.
         */
        DataType = typeof(DataType) != 'undefined' ? DataType : "json";
        Method   = typeof(Method) != 'undefined' ? Method : 'POST';
        var URL = restResource;

        $.ajax({
                url: URL,
                type: Method,
                dataType: DataType,
                data: reqData,
                async: isAsync,
                success: function(json) {
                    // These are fire and forget... if it work, great...
                },
                error: function(a,b,c) {
                   // If it doesn't... we really don't care.
                }

        });
    }


}
/**
 *
 * @param string oName The name of the calling object.
 */
mvcErrorHandler.prototype.setObject = function(oName)
{
    this.objName = oName;
}

/**
 *
 * @parameter string mName The name of the method where the error occured.
 */
mvcErrorHandler.prototype.setMethod = function(mName)
{
    this.methodName = mName;
}

/**
 *
 */
mvcErrorHandler.prototype.throwError = function(msg, sev, action, userMessage)
{
    if ((typeof(sev) == 'undefined')) sev = this.INFO;
    this.errMessage = msg;

    if(this.isLoggable(sev)) this.logError();



    if(action == this.MESSAGEBOX) {
        var msgBoxType = this.getMsgBoxType(sev);
        if(msgBoxType) msgBox(msgBoxType, userMessage);
    }

}



