/*jslint         browser : true, continue : true,
  devel  : true, indent  : 2,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : true,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/

/*global $, app */

app.authenticate = (function() {
    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var
        stateMap = {},
        onSubmit,
        configModule,
        initModule;
    //----------------- END MODULE SCOPE VARIABLES ---------------

    //------------------- BEGIN UTILITY METHODS ------------------
    //-------------------- END UTILITY METHODS -------------------

    //------------------- BEGIN EVENT HANDLERS -------------------
    onSubmit = function(event) {
        event.preventDefault();

        //get creds
        var login = $('#login').val();
        var password = $('#password').val();


        $.ajax({
            method: "post",
            dataType: "json",
            url: app.shell.stateMap.apiUrl.replace('/api/', '/authenticate'),          
            data: {
                login: login,
                password: password
            },
            success: function(data, textStatus, jqXHR) {
                if (data.ok == 1) {
                    app.shell.stateMap.user.authenticated = true;
                    app.shell.stateMap.user.token = data.token;
                    app.shell.stateMap.user.name = data.name;                    
                    app.shell.stateMap.user.id=data.id;
                    //token expiry date                   
                    app.shell.stateMap.user.expires = data.expires;

                    //tell the shell we've logged in successfully
                    $.gevent.publish('app-login', {
                        name: login
                    });
                } else {
                    if (data.error) {
                        $.gevent.publish('app-show-error',data.error);                           
                    }
                    app.shell.stateMap.user.authenticated = false;
                    app.shell.stateMap.user.token = '';
                    app.shell.stateMap.user.name = 'please sign in';
                    $.gevent.publish('app-logout');
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                app.shell.stateMap.user.authenticated = false;
                app.shell.stateMap.user.token = '';
                app.shell.stateMap.user.name = 'please sign in';
                $.gevent.publish('app-logout');
                $.gevent.publish('app-show-error',textStatus + " " + errorThrown); 
            }
        });
        return false; //prevent default?
    };

    //-------------------- END EVENT HANDLERS --------------------

    //------------------- BEGIN PUBLIC METHODS -------------------
    //CONFIGMODULE
    //
    configModule = function(context) {
        stateMap.context = context.context;
        if (stateMap.context.params.id) {
            stateMap.id = stateMap.context.params.id;
        }
    };

    //INITMODULE
    //
    initModule = function($container) {
        if (typeof $container === 'undefined') {
            $container = $('#app-shell-main-content');
        }
        $container.html(Handlebars.templates['app.authenticate']({}));
        $('#btnSubmit').bind('click', onSubmit);
    };


    // return public methods
    return {
        configModule: configModule,
        initModule: initModule
    };
    //------------------- END PUBLIC METHODS ---------------------
}());