/*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.search = (function() {
    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var

        stateMap = {},
        configModule,
        onSearch,
        initModule;
    //----------------- END MODULE SCOPE VARIABLES ---------------

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

    //------------------- BEGIN EVENT HANDLERS -------------------
    //ONSEARCH
    //
    onSearch = function(event) {
        event.preventDefault();
        $('#app-error-message').text('');

        //get form data
        var query = $('#query').val();

        app.api.search(query, function(res) {
            if (res.error) {
                $('#app-error-message').text(res.msg);
                return false;
            }

            console.log('search result:')
            console.log(res);
            var $appList = $('#app-list');
            $appList.empty();
            $.each(res, function(i, obj) {
                var editUrl = '';
                switch (obj.obj) {
                    case 'customer':
                        editUrl = 'customer_edit/' + obj._id;
                        break;
                    case 'site':
                        editUrl = 'customer_site_edit/' + obj._id + '/' + obj.customer_id;
                        break;
                    case 'incident':
                        editUrl = 'incident_edit/' + obj._id + '/' + obj.site_id;
                        break;
                    case 'trial':
                        editUrl = 'trial_edit/' + obj._id + '/' + obj.site_id;
                        break;
                    case 'purchase':
                        editUrl = 'purchase_edit/' + obj._id + '/' + obj.site_id;
                        break;
                    default:
                        alert("UNKNOWN SEARCH TYPE OBJECT RESULT: " + obj.obj + " WHUPS!")
                }

                $appList.append("<li><a href=\"#!/" + editUrl + "\">" +
                    app.util_b.genListColumn(obj.name + ' ' + obj.obj) +
                    "</a></li>");

                //cache it
                app.shell.stateMap.search_cache.has_cache = true;
                app.shell.stateMap.search_cache.html = $("#app-list").html();
                app.shell.stateMap.search_cache.query = query;


            });

        });

        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.search']({}));

        // bind actions      
        $('#btn-search').bind('click', onSearch);

        //reconstitute from last search cache
        if (app.shell.stateMap.search_cache.has_cache == true) {
            $("#app-list").html(app.shell.stateMap.search_cache.html);
            $('#query').val(app.shell.stateMap.search_cache.query);
        }


    };

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