/*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();
    $.gevent.publish("app-clear-error");

    //get form data
    var query = $("#searchquery").val();
    //app.api.get('customer/' + stateMap.id, function (res) {
    //app.get('/api/search?query=querytext'
    app.api.get("search?query=" + query, function(res) {
      if (res.error) {
        $.gevent.publish("app-show-error", res.msg);
        return false;
      }

      var $appList = $("#rf-list");
      $appList.empty();
      $.each(res, function(i, obj) {
        var editUrl = "";
        switch (obj.obj) {
          case "customer":
            editUrl = "customerEdit/" + obj.id;
            break;
          case "site":
            editUrl = "customerSiteEdit/" + obj.id + "/" + obj.customerId;
            break;
          case "purchase":
            editUrl = "purchaseEdit/" + obj.id + "/" + obj.site_id;
            break;
          case "license":
            editUrl = "licenseView/" + obj.id;
            break;
          default:
            alert("UNKNOWN SEARCH TYPE OBJECT RESULT: " + obj.obj + " WHUPS!");
        }

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

        //cache it
        app.shell.stateMap.search_cache.has_cache = true;
        app.shell.stateMap.search_cache.html = $("#rf-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
    $("#searchbutton").bind("click", onSearch);

    //reconstitute from last search cache
    if (app.shell.stateMap.search_cache.has_cache == true) {
      $("#rf-list").html(app.shell.stateMap.search_cache.html);
      $("#searchquery").val(app.shell.stateMap.search_cache.query);
    }
    //Context menu
    app.nav.contextClear();
    ////app.nav.setContextTitle("Search");
  };

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