showDisconnectedWindow = function () {
  $('#connectWindow_block').show();
}

hideDisconnectedWindow = function () {
  $('#connectWindow_block').hide();
}

//buttons for reconnect window
$(function (){
  $('.connectWindow_content_back_btn').click(function () {
    //gwd.actions.gwdPagedeck.goToPage('pagedeck', 'page1', 'fade', 500, 'linear', 'top');
    hideDisconnectedWindow();
    $('#reconnect-button').show();
  })

  $('.connectWindow_content_retry_btn').click(function () {
    requireWebsocket.connect();
    responseWebsocket.connect();
    hideDisconnectedWindow();
    $('#reconnect-button').hide();
  })
})

function showBlocklyTextWindow(text, imagePath) {
  $('#blocklyTextWindow_block').show();
  var textElement = document.querySelector('.blocklyTextWindow_content_text');
  const maxLength = 100;
  if (text.length > maxLength) {
    textElement.textContent = text.substr(0, maxLength) + '...';
  } else {
    textElement.textContent = text;
  }

  if (imagePath) {
    var imageElement = $('<img>').attr('src', imagePath).addClass('blocklyTextWindow_content_img');
    $('.blocklyTextWindow_content_text').after(imageElement);
  }
}

function hideBlocklyTextWindow() {
  $('#blocklyTextWindow_block').hide();
  $('.blocklyTextWindow_content_img').remove();
}

$(function (){
  $('.blocklyTextWindow_content_ok_btn').click(function () {
    hideBlocklyTextWindow();
  })
})

// Set up a function to check the connection status
function checkConnection() {
  if (!navigator.onLine) {
    // Connection lost, show alert window
    window.alert('Connection lost!');
  }
}
// Check the connection status every 5 seconds
var intervalID = window.setInterval(checkConnection, 5000);

//function that calls showBlocklyTextWindow if the specified blocks are not present in the workspace
function mandatoryBlocksMissing(workspace) {
  var blockTypes = ['stop_movement'];
  var missingBlocks = [];

  for (var i = 0; i < blockTypes.length; i++) {
    var blockType = blockTypes[i];
    var blocks = workspace.getBlocksByType(blockType);
    if (blocks.length === 0) {
      missingBlocks.push(blockType);
    }
  }

  if (missingBlocks.length > 0) {
    var message = 'The following blocks are missing: ' + missingBlocks.join(', ');
    showBlocklyTextWindow(message, 'stop_block.PNG');
    return true;
  }else return false;
}

