import { hideMenus, showMenu, showSidebar, showTextBlocks, clickSetForm, clickImportFiles } from "./evts.js";
import { buildFile } from "./files.js";
import setPassword, { passwordHash } from "./scripts.js";
import { getUsrId } from "./scripts.js";
import parseFormOnSubmit from "./parseForm.js";
import { createStorageObj } from "./storage.js";
import XORCipher from "./xorc.js";
import sha256 from "./sha256.min.js";
import { loadNavBar, initTextBlocks } from "./web.js";


window.activeState = {
  userId: getUsrId(),
  activePage: "landing",
  loadedTemplate: "",
  fileName: "",
  lastElement: "",
  serverFilesTs: "",
  templates: [],
  templateFieldTypes: [
    "simpleInput",
    "longText",
    "hiddenField",
    "current_time",
    "current_date",
    "markup",
  ],
  markups: [
    "title",
    "link",
    "italic",
    "green_highlighted",
    "highlighted",
  ]
};


function init() {

  //check if user is logged in 
  let verfiedStatus = window.sessionStorage.getItem(sha256("verified"));
  if (verfiedStatus != null) {
    //user logged in

    //write verifiedStatus content into passwordHash for decode check later
    passwordHash.set(XORCipher.decode(sha256("passwordHash"), verfiedStatus));
    document.getElementById("login").style.display = "none";

  } else {
    document.getElementById("login").style.display = "block";
  }

  //load NavigationBar with templates according to server
  loadNavBar();

  //init Textblocks field with entries according to server
  initTextBlocks();

  //add event listeners to document and window
  eventListeners();

  //print current version to footer
  printVersion();

  //adjust title for mobile use
  if (screen.width < 993) {
    document.getElementById("siteTitle").innerHTML = "TG";
  }


  
}

function eventListeners() {

  //add hideMenu to Body
  document
  .getElementsByClassName("w3-main")[0]
  .addEventListener("click", hideMenus);

//add set Password to loginForm
document
  .getElementById("submitPassword")
  .addEventListener("click", setPassword);
//add toggle Navigation Menu
document
  .getElementById("toggleNavigationMenu")
  .addEventListener("click", showMenu);
//add loadTemplateBtn event showMenu
document
  .getElementById("loadTemplateBtn")
  .addEventListener("click", showMenu);  
//add toggle sideBar Menu
document
  .getElementById("toggleSidebarMenu")
  .addEventListener("click", showSidebar);
//add toggle files Menu and sidebar button
document
  .getElementById("toggleFilesMenuSB")
  .addEventListener("click", buildFile);
document
  .getElementById("toggleFilesMenu")
  .addEventListener("click", buildFile);
//add load template sidebar entry
document
  .getElementById("loadTemplateBtn")
  .addEventListener("click", showMenu);  
//add toggle textBLocks Menu
document
  .getElementById("toggleTestBlocksMenu")
  .addEventListener("click", showTextBlocks);

//add setFormBtn for use in form
document
  .getElementById("setFormBtn")
  .addEventListener("click", (e) => clickSetForm(e));

//add saveFiles to server listener on launch page
document
  .getElementById("importFilesSB")
  .addEventListener("click", () => clickImportFiles());

//add key listener for ctrl s in form mode
window.addEventListener("keydown", (e) => {

  if (e.ctrlKey && e.key == "s") { 
    if (activeState.activePage == "form");
    createStorageObj();
    parseFormOnSubmit();
    e.preventDefault(); 
  }
})
}

function printVersion() {
  const scripts = document.getElementsByTagName("script");
  const versionSpan = document.getElementById("currentVersion").lastChild;

  for (var i=0;i<scripts.length;i++) {
    if (scripts[i].src) {
      let source = scripts[i].src;
      // js/version/main.js
      let pathVersion = source.split("/");
      pathVersion = pathVersion[pathVersion.length-2];
      //add it to document footer currentVersion
      versionSpan.textContent = "version: " + pathVersion;
    }
 }
}

export default init;
