/**
 * Created by Benny on 31.03.2016.
 */

"use strict";

var app = app || {};
app.core = app.core || {};

app.core.Rest = {

    url : null,
    auth : null,
    sessionId : null,
    cfg :
    {
        dataType : 'json'
    },

    /**
     * Init
     *
     * @param url
     */
    init: function ( url, sessionId )
    {
        this.url = url;
        this.sessionId = sessionId || null;
    },

    /**
     * Set auth
     *
     * @param user
     * @param pass
     */
    setAuth : function( user, pass )
    {
        this.auth = btoa( user + ":" + pass );
    }

    ,

    setSessionId : function( sessionId )
    {

    }

    ,

    /**
     * Get request
     *
     * @param endpoint
     * @param data
     * @param cbSuccess
     * @param cbError
     */
    get : function( endpoint, cbSuccess, cbError )
    {
        this.call( {
            method : 'GET',
            endpoint : endpoint,
            cbSuccess : cbSuccess,
            cbError : cbError || null
        });
    },

    /**
     * Post request
     *
     * @param endpoint
     * @param data
     * @param cbSuccess
     * @param cbError
     */
    post : function( endpoint, data, cbSuccess, cbError )
    {
        this.call( {
            method : 'POST',
            endpoint : endpoint,
            data : _.isObject( data ) ? JSON.stringify( data ) : data,
            cbSuccess : cbSuccess,
            cbError : cbError || null
        });
    },

    delete : function( endpoint, cbSuccess, cbError )
    {
        this.call( {
            method : 'DELETE',
            endpoint : endpoint,
            cbSuccess : cbSuccess,
            cbError : cbError || null
        });
    },

    /**
     * Generic call
     *
     * @param params
     */
    call : function( params )
    {
        console.log( params );

        var method = params.method || 'GET',
            data = params.data || null,
            endpoint = params.endpoint || '',
            successCb = params.cbSuccess || null,
            errorCb = params.cbError || null;

        $.ajax({
            url : encodeURI( this.url + endpoint ),
            type : method,
            data : data,
            beforeSend : function( xhr )
            {
                xhr.setRequestHeader("Content-Type","application/json");
                if ( this.auth )
                {
                    xhr.setRequestHeader(
                        'Authorization',
                        "Basic " + this.auth
                    );
                }
                if ( this.sessionId )
                {
                    xhr.setRequestHeader(
                        'x-sessionid',
                        this.sessionId
                    );
                }
            },
            success : function( res )
            {
                var json;
                if ( _.isObject( res ) )
                {
                    json = res;
                }
                else
                {
                    json = JSON.parse( res );
                }
                successCb && successCb( json );
            },
            error : function( xhr, res )
            {
                if ( _.isFunction( errorCb ) )
                {
                    errorCb( xhr, res );
                }
                else
                {
                    switch( xhr.status )
                    {
                        // Bad request
                        case 400:
                            break;

                        // Unauthorized
                        case 401:
                            break;

                        // Not found
                        case 404:
                            break;

                        // Unknown error
                        default:

                    }
                }
            }
        });
    }
};
