/**
 * Created by Benny on 12.10.2014.
 */

"use strict";

var TB = TB || {};

TB.Service = {

    // Response codes
    CODE_OK : 200,
    CODE_NOT_LOGGED_IN : 401,
    CODE_INVALID_CALL : 404,
    CODE_INVALID_DATA : 409,
    CODE_GENERAL_ERROR : 500,
    CODE_OUTDATED_VERSION : 899,

    url : null,
    auth : { token: null, account_id: null, user: null, pass: null, hashed: true, version: '0.0' },

    init : function( url ) {
        this.url = url;
        if ( 'http://service.tb.com' == this.url ) {
            this.url += '?XDEBUG_SESSION_START=PHPSTORM';
        }
        this.auth.version = TB.Client.VERSION;
    },

    setAuth : function( authData ) {
        this.auth.account_id = authData.account_id || null;
        this.auth.token = authData.token || null;
    },

    request : function( ctrl, action, params, callback, $trigger, errCallback ) {
        var data = params, cb = callback, $triggerButton = $trigger, ladaButton = null, errCb = errCallback || null;
        data[ 'c' ] = ctrl;
        data[ 'a' ] = action,
        data[ 'auth' ] = TB.Service.auth;

        // Disable trigger button

        if ( $triggerButton ) {
            $triggerButton.prop( 'disabled', true );
            $triggerButton.blur();
            //ladaButton = Ladda.create( $triggerButton.get(0) );
            //ladaButton.start();
        }

        $.ajax( this.url, {
            // Always
            'complete' : function() {
                // Re-enable trigger button
                if ( $triggerButton ) $triggerButton.prop( 'disabled', false );
            },
            // In case of success
            // @param res JSON Service response { code: (int), data : (mixed), notes : (mixed) }
            'success' : function( res ) {

                // Resolve data
                var data = ( res && res.data ) ? res.data : null,
                    code = ( res && res.code ) ? res.code : null;

                // Perform actions by result code
                switch ( code ) {
                    case TB.Service.CODE_OK:
                        // If custom callback is set and should be triggered
                        cb && cb( data );
                    break;

                    case TB.Service.CODE_NOT_LOGGED_IN:
                        TB.Client.logout();
                        TB.Worker.stop();
                        TB.Controller.do( 'Auth', 'login' );
                        TB.Ext.Alert.show( 'Sitzung abgelaufen.', 'Bitte erneut einloggen.' );
                    break;

                    case TB.Service.CODE_INVALID_CALL:
                        TB.Ext.Notification.show( 'Fehlerhafter Aufruf.', TB.Ext.Notification.TYPE_WARNING );
                    break;

                    case TB.Service.CODE_INVALID_DATA:
                        TB.Ext.Notification.show( 'Aktion nicht erfolgreich. Die übergebenen Daten sind falsch..', TB.Ext.Notification.TYPE_WARNING );
                    break;

                    case TB.Service.CODE_GENERAL_ERROR:
                        if (errCallback && typeof errCallback === "function" ) {
                            errCallback( data );
                        } else {
                            TB.Ext.Notification.show( 'Aktion nicht erfolgreich. Gegebenenfalls später noch einmal versuchen.', TB.Ext.Notification.TYPE_DANGER );
                        }
                    break;

                    case TB.Service.CODE_OUTDATED_VERSION:
                        TB.Worker.stop();
                        TB.Ext.Alert.modal( {
                           'title' : 'Server Synchronisation',
                            'text' : 'Drücke OK um Serverdaten zu aktualisieren.',
                            'type' : 'info',
                            'showCancelButton' : false
                        }, function() {
                            location.reload( true );
                        });
                    break;
                }
            },
            // In case of an error
            'error' : function( xhr, txt, info ) {
                if ( errCb ) {
                    errCb( xhr, txt, info );
                } else {
                    TB.Ext.Notification.show(
                        "Server Fehler. Bitte versuche es später nochmal.",
                        TB.Ext.Notification.TYPE_DANGER
                    );
                }
            },
            'data' : data,
            'cache' : false,
            'type' : 'POST',
            'dataType' : 'json',
            'crossDomain' : true
        }).always( function(){
            ladaButton && ladaButton.stop();
        });
    }
};