import sha256 from "./sha256.min.js";

function showMenu() {
  var x = document.getElementById("navMob");
  if (x.className.indexOf("w3-show") == -1) {
    x.className += " w3-show";
  } else {
    x.className = x.className.replace(" w3-show", "");
  }

}



function hideMenus() {

  let navBar = document.getElementById("navMob");
  navBar.className = navBar.className.replace(" w3-show", "");
}

function logout() {
  let id = sha256("verified");
  window.sessionStorage.setItem(id, "");
  document.cookie = id + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
  window.location.reload(); 
}

function modalNotifier(msg = '', timeout = 5000, closeable = true) {
  let modalElement = document.getElementById("modalNotifier");
  let msgElement = document.getElementById("modalMsg");
  let closeElement = document.getElementById("modalClose");

  if (msg == '!done') {
    document.getElementById("modalNotifier").style.display = "none";
    return
  }

  modalElement.style.display = "block";
  msgElement.innerHTML = msg;

  if (!closeable) {
    closeElement.style.display = "none"
  } else {
    closeElement.style.display = "block"
  }

  if (timeout >= 1) {
    const run = setTimeout(
      () => {
        modalElement.style.display = "none"
        closeElement.style.display = "block"
      },
      timeout * 1000
    );
  }
}

function moveProgressBar(intervall) {
  var elem = document.getElementById("loadingNewArticlesProgress");  
  var width = 0;
  var id = setInterval(() => {
    if (width >= 100) {
      elem.innerHTML = 'waiting...';
      clearInterval(id);
    } else {
      width++; 
      elem.style.width = width + '%'; 
      elem.innerHTML = width + '%'; 
    }
  }, intervall)
}

export {
  hideMenus,
  showMenu,
  logout,
  modalNotifier,
  moveProgressBar
};
