/*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.ops = (function() {
  "use strict";
  //---------------- BEGIN MODULE SCOPE VARIABLES --------------
  var stateMap = {},
    configModule,
    checkService,
    initModule;
  //----------------- END MODULE SCOPE VARIABLES ---------------

  //------------------- BEGIN UTILITY METHODS ------------------
  //////////////////
  //Generate list item
  //
  checkService = function(service) {
    
    app.api.get("ops/status/" + service, function(res) {
      if (res.error) {
        $.gevent.publish("app-show-error", res.msg);
      } else {
        if (res.serviceCheckError) {
          var errorText = $("#rf-ops-error").text();
          errorText += "\r\n";
          errorText += res.serviceCheckError;
          $("#rf-ops-error").text(errorText);
        }

        var serviceElement = $("#" + service);
        serviceElement.removeClass("mdi-help-circle-outline text-warning");
        if (res.status == true) {
          serviceElement.addClass("mdi-check-circle text-success");
        } else {
          serviceElement.addClass("mdi-alert text-danger");
        }
      }
    });

  };

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

  //------------------- BEGIN EVENT HANDLERS -------------------

  //-------------------- 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.ops"]({}));
    $("#rf-ops-error").text("");

    //Context menu
    app.nav.contextClear();

    checkService("MailMirror");
    checkService("AyaNovaSite");
    checkService("Backup");
    checkService("ContactForm");
    checkService("RequestForm");
    checkService("RequestLiteForm");
    checkService("Forum");
    checkService("Subversion");
    checkService("DevOps");
    checkService("APISite");
  };

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