
require([
  "dojo/cookie",
  "dojo/dom",
  "dojo/dom-class",
  "dojo/dom-style",
  "dojo/ready",
  "dojo/request/xhr",
  "dijit/registry"
], function(
  cookie,
  dom,
  domClass,
  domStyle,
  ready,
  xhr,
  registry
) {

    checkFailoverStatus = function () {

        var refreshFailover = function() {
            refreshTree();
            // Refresh storage tab, if opened
            var p = registry.byId("content");
            var c = p.getChildren();
            for(var i=0; i<c.length; i++){
                if(c[i].tab == 'storage'){
                    c[i].refresh();
                } else if(c[i].tab == 'services'){
                    c[i].refresh();
                } else if(c[i].tab == 'shares'){
                    c[i].refresh();
                }
            }
        }

        xhr.get('/failover/status/', {
            preventCache: true
        }).then(function(data) {
            var menuitem = dom.byId("failover_menuitem");
            var statusitem = dom.byId("failover_status");
            var statusname = dom.byId("failover_status_name");
            domStyle.set(menuitem, "display", "inherit");
            statusname.status = data;
            if(data == 'MASTER') {
                statusname.innerHTML = 'Active';
                if(
                  domClass.contains(statusitem, "failover_electing") ||
                  domClass.contains(statusitem, "failover_importing") ||
                  domClass.contains(statusitem, "failover_passive") ||
                  domClass.contains(statusitem, "failover_error")
                  ) {
                    domClass.remove(statusitem, ["failover_electing", "failover_importing", "failover_passive", "failover_error"]);
                    domClass.add(statusitem, "failover_ok");
                    refreshFailover();
                }
            } else if (data == 'ELECTING') {
                statusname.innerHTML = 'Electing';
                if(
                  domClass.contains(statusitem, "failover_ok") ||
                  domClass.contains(statusitem, "failover_importing") ||
                  domClass.contains(statusitem, "failover_passive") ||
                  domClass.contains(statusitem, "failover_error")
                  ) {
                    domClass.remove(statusitem, ["failover_ok", "failover_importing", "failover_passive", "failover_error"]);
                    domClass.add(statusitem, "failover_electing");
                    refreshFailover();
                }
            } else if (data == 'IMPORTING') {
                statusname.innerHTML = 'Importing';
                if(
                  domClass.contains(statusitem, "failover_ok") ||
                  domClass.contains(statusitem, "failover_electing") ||
                  domClass.contains(statusitem, "failover_passive") ||
                  domClass.contains(statusitem, "failover_error")
                  ) {
                    domClass.remove(statusitem, ["failover_ok", "failover_electing", "failover_passive", "failover_error"]);
                    domClass.add(statusitem, "failover_importing");
                    refreshFailover();
                }
            } else if (data == 'ERROR') {
                statusname.innerHTML = 'Error';
                if(
                  domClass.contains(statusitem, "failover_ok") ||
                  domClass.contains(statusitem, "failover_electing") ||
                  domClass.contains(statusitem, "failover_importing") ||
                  domClass.contains(statusitem, "failover_passive")
                  ) {
                    domClass.remove(statusitem, ["failover_ok", "failover_electing", "failover_importing", "failover_passive"]);
                    domClass.add(statusitem, "failover_error");
                    refreshFailover();
                }
            } else {
                statusname.innerHTML = 'Passive';
                if(
                  domClass.contains(statusitem, "failover_ok") ||
                  domClass.contains(statusitem, "failover_electing") ||
                  domClass.contains(statusitem, "failover_importing") ||
                  domClass.contains(statusitem, "failover_error")
                  ) {
                    domClass.remove(statusitem, ["failover_ok", "failover_electing", "failover_importing", "failover_error"]);
                    domClass.add(statusitem, "failover_passive");
                    refreshFailover();
                }
            }
            setTimeout(checkFailoverStatus, 7000);
        }, function(data, req, err) {
            setTimeout(checkFailoverStatus, 10000);
        });

    }

    sync_to_peer = function(id) {

        xhr.post('/failover/sync_to_peer/' + id + '/', {
            headers: {"X-CSRFToken": cookie('csrftoken')},
            handleAs: "json"
        }).then(function(data) {
            if(data.error == true) {
                setMessage(gettext('An error occurred while syncing to peer!'), "error");
            }
        });

    }

    failoverForceMaster = function(btn, url) {
      btn.set('disabled', true);
      form = getForm(btn);
      require([
        "dojo/request/xhr"
      ], function(xhr) {
        xhr.post(url, {
          handleAs: 'json',
          headers: {"X-CSRFToken": CSRFToken}
        }).then(function(data) {
          btn.set('disabled', false);
          if(!data.error) {
            setMessage(data.message);
          } else {
            setMessage(data.message, "error");
          }
        });

      });
    }

    carpCritical = function(val) {

console.log("here");
      var crit = registry.byId("id_carp_critical");
      var number = registry.byId("id_carp_number");
      var group = registry.byId("id_carp_group");

      if(number.get('value') == '1' || number.get('value') == '2') {
          crit.set('value', false);
          crit.set('disabled', true);
      } else {
          crit.set('disabled', false);
      }
console.log(crit.get('value'));
      if(crit.get('value') == 'on') {
        group.set('disabled', false);
      } else {
        group.set('disabled', true);
      }

    };

    ready(function() {
      checkFailoverStatus();
    });

});
