// to tell whether bootstrap's modal had Submit button pressed :(
var nd_save_ok = false;

// user clicked or asked for port changes to be submitted via ajax
function port_control (e) {
  var td = $(e).closest('td'),
      reason = $('#nd_portlog-reason').val(),
      logmessage = $('#nd_portlog-log').val();
  $('#nd_portlog-log').val('');

  if (nd_save_ok == false) {
    td.find('.nd_editable-cell-content').text(td.data('default'));
    td.blur();
    return;
  }
  nd_save_ok = false;

  $.ajax({
    type: 'POST'
    ,url: uri_base + '/ajax/portcontrol'
    ,data: {
      device:  td.data('for-device')
      ,port:   td.data('for-port')
      ,field:  td.data('field')
      ,action: ($(e).data('action') || td.data('action'))
      ,value:  td.text().trim()
      ,reason: reason
      ,log:    logmessage
    }
    ,success: function() {
      toastr.info('Submitted change request');

      // update all the screen furniture unless bouncing
      if (! $(e).hasClass('fa-bullseye')) {
        if ($.trim(td.data('action')) == 'down') {
          td.prev('td').html('<i class="fas fa-xmark"></i>');
          $(e).toggleClass('fa-hand-point-down');
          $(e).toggleClass('fa-hand-point-up');
          $(e).siblings('.fa-bullseye').hide();
          retitleTooltip($(e), 'Enable Port');
          td.data('action', 'up');
        }
        else if ($.trim(td.data('action')) == 'up') {
          td.prev('td').html('<i class="fas fa-arrows-rotate fa-spin"></i>');
          $(e).toggleClass('fa-hand-point-up');
          $(e).toggleClass('fa-hand-point-down');
          $(e).siblings('.fa-bullseye').show();
          retitleTooltip($(e), 'Disable Port');
          td.data('action', 'down');
        }
        else if ($.trim(td.data('action')) == 'false') {
          $(e).next('span').text('');
          $(e).toggleClass('nd_power-on');
          retitleTooltip($(e), 'Enable Power');
          td.data('action', 'true');
        }
        else if ($.trim(td.data('action')) == 'true') {
          $(e).toggleClass('nd_power-on');
          retitleTooltip($(e), 'Disable Power');
          td.data('action', 'false');
        }
      }
    }
    ,error: function() {
      toastr.error('Failed to submit change request');
      td.find('.nd_editable-cell-content').text(td.data('default'));
      td.blur();
    }
  });
}

// on load, establish global delegations for now and future
$(document).ready(function() {
  // for growl-like functionality, check for notifications periodically
  if (nd_check_userlog) {
    (function worker() {
      $.ajax({
        url: uri_base + '/ajax/userlog'
        ,success: function(data) {
          for (var i = 0; i < data['error'].length; i++) {
            toastr.error(data['error'][i], 'Failed Job:');
          }
          for (var i = 0; i < data['done'].length; i++) {
            toastr.success(data['done'][i], 'Successful Job:');
          }
          // Schedule next request when the current one's complete
          setTimeout(worker, 5000);
        }
        ,error: function() {
          // after failure, try less often
          setTimeout(worker, 60000);
        }
      });
    })();
  }

  // toggle visibility of port up/down and edit controls
  $('.tab-content').on('mouseenter', '.nd_editable-cell', function() {
    $(this).children('.nd_hand-icon').show();
    if (! $(this).is(':focus')) {
      $(this).children('.nd_edit-icon').show(); // ports
      $(this).siblings('td').find('.nd_device-details-edit').show(); // details
    }
  });
  $('.tab-content').on('mouseleave', '.nd_editable-cell', function() {
    $(this).children('.nd_hand-icon').hide();
    if (! $(this).is(':focus')) {
      $(this).children('.nd_edit-icon').hide(); // ports
      $(this).siblings('td').find('.nd_device-details-edit').hide(); // details
    }
  });
  $('.tab-content').on('focus', '[contenteditable=true]', function() {
      $(this).children('.nd_edit-icon').hide(); // ports
      $(this).siblings('td').find('.nd_device-details-edit').hide(); // details
  });

  // to tell whether bootstrap's modal had Submit button pressed :(
  $('#ports_pane').on('click', '#nd_portlog-submit', function() {
    nd_save_ok = true;
  });

  // activity for port up/down control, power enable/disable control
  $('#ports_pane').on('click', '.fa-hand-point-up,.fa-hand-point-down,.nd_power-icon,.fa-bullseye', function() {
    var clicked = this; // create a closure
    $('#nd_portlog').one('hidden.bs.modal', function() {
      port_control(clicked); // save
    });
    if ($(this).hasClass('fa-hand-point-up')) {
      $('#nd_portlog-reason').val('resolved');
    }
    else {
      $('#nd_portlog-reason').val('other');
    }
    $('#nd_portlog').modal('show');
  });

  // has cell content changed?
  var dirty = false;

  // activity for contenteditable control
  $('.tab-content').on('keydown', '[contenteditable=true]', function(event) {
    var cell = this,
        td = $(cell).closest('td'),
        esc = event.which == 27,
        nl  = event.which == 13;

    if (esc) {
      $(cell).blur();
      $(this).val("");
    }
    else if (nl) {
      event.preventDefault();

      if (td.data('field') == 'c_pvid') {
        $('#nd_portlog').one('hidden.bs.modal', function() {
          port_control(cell); // save
        });
        $('#nd_portlog').modal('show');
      }
      else if (td.data('field') == 'nd_left-acl-rule-field') {
        if ($(this).val().length == 0) { return }
        td.append('<input class="nd_left-acl-rule-field" data-form="update" name="left_rule" type="hidden" value="'+
          Math.floor( Date.now() / 1000 ) + '.' + window.btoa($(this).val()) +'">')
        $(this).closest('tr').find('button.nd_adminbutton[name="update"]').click();
      }
      else if (td.data('field') == 'nd_right-acl-rule-field') {
        if ($(this).val().length == 0) { return }
        td.append('<input class="nd_right-acl-rule-field" data-form="update" name="right_rule" type="hidden" value="'+
          Math.floor( Date.now() / 1000 ) + '.' + window.btoa($(this).val()) +'">')
        $(this).closest('tr').find('button.nd_adminbutton[name="update"]').click();
      }
      else {
        // no confirm for port descr change
        nd_save_ok = true;
        port_control(cell); // save
      }

      dirty = false;
      $(cell).blur();
    }
    else {
      dirty = true;
    }
  });

  // activity for contenteditable control
  $('.tab-content').on('blur', '[contenteditable=true]', function(event) {
    if (dirty) {
      document.execCommand('undo');
      dirty = false;
      $(this).blur();
    }
  });

});
