import { storeData, clearData, retrieveData } from "./storage.js";

function parseFormOnSubmit(returnJSON = false, parseOnly = false) {
    //event.preventDefault;
  
    let dataArray = [];
  
    if (parseOnly) {
      let lT = activeState.loadedTemplate;
      dataArray = retrieveData("userInputForce", lT);
      if (dataArray != "") {
        try {
          dataArray = JSON.parse(dataArray);
        } catch (e) {
          alert("Decryption failed; are you authenticated?");
          window.location.reload();
          return;
        }
      }
      if (dataArray == null) {
        let wrapper = document.createElement("div");
        wrapper.innerHTML = mainFormPlaceholder();
        let div = wrapper.firstChild;
        return div;
      }

      if (dataArray.length <= 0) {
        let wrapper = document.createElement("div");
        wrapper.innerHTML = mainFormPlaceholder();
        let div = wrapper.firstChild;
        return div;
      }

    } else {
      let x = document.getElementById("mainFormObj");
      
      if (x != null) {
        for (let i = 0; i < x.length; i++) {
          dataArray.push({
            value: x.elements[i].value,
            name: x.elements[i].name,
          });
        }
      }

      //set filename to active state according to userFileName field from loadTemplate
      let userFileNameField = document.getElementById("userFileName");
      let userFileName = userFileNameField.value;
      let userFileNamePH = userFileNameField.getAttribute("placeholder");
      if (userFileName.length != 0) {
        activeState.fileName = userFileName;
      } else if (userFileNamePH != null) {
        activeState.fileName = userFileNamePH;
      } 
    }

  
    //get original objects from sessionstorage gen from loadTemplate
    let objects = JSON.parse(window.sessionStorage.getItem("templateObjects"));

    //get the complete unparsed template string from sessionstorage from loadTemplate
    let fullString = window.sessionStorage.getItem("fullString");
  
    let b = "";
  
    if (objects == null) {
      return;
    }


    //iterate through templateObjects and look for according formdata
    for (let obj of objects) {
      for (let data of dataArray) {
        //convert conList Master name to default name as set flag for appending connected list fields cl-name
        let conListFlag = false;
        if (data.name.split("-")[0] == "clM") {
          if (data.name.substring(4) === obj.word.replace(/ /g, "_") ) {
            conListFlag = true;
            data.name = data.name.substring(4);
            if (data.value == "!none") {
              obj.result = "";
              continue;
            }
            if (data.value == "!selected") {
              data.value = obj[0];
              obj.result = "";
            }
          }
        }
        if (obj.word.replace(/ /g, "_") === data.name) {
          let value = parseDataForResult(obj, data.value);
          obj.result = value;

        }
        if (conListFlag) {
          if (obj.type == "conList") {

            //check for button if only one item exists and search conlist item
            if (obj.listCount == 0) {
              for (let d of dataArray) {
                if (d.name.split(":!")[1] !== undefined) d.placeholder = "!"+d.name.split(":!")[1];
                d.name = d.name.split(":!")[0];
                if ("cl-"+obj[0].replace(/ /g, "_") == d.name && d.value != "") {
                  //console.log(d, obj[0], data);
                  if (data.value.replace(/ /g, "_")  == d.name.substring(3)) {
                    if (d.hasOwnProperty("placeholder")) d.value = d.placeholder + "\n" + d.value;
                    let value = parseDataForResult(obj, d.value);
                    obj.result = obj.result + "\n" + value;
                  }
                }
              }             
            } else {
              //loop through dataArray and look for coresponding conlist items
              for (let i = 0; i <= obj.listCount; i++) {
                for (let d of dataArray) {
                  if (d.name.split(":!")[1] !== undefined) d.placeholder = "!"+d.name.split(":!")[1];
                  d.name = d.name.split(":!")[0];
                  if ("cl-"+obj[i].replace(/ /g, "_") == d.name && d.value != "") {
                    if (data.value.replace(/ /g, "_")  == d.name.substring(3)) {
                      if (d.hasOwnProperty("placeholder")) d.value = d.placeholder + "\n" + d.value;
                      let value = parseDataForResult(obj, d.value);
                      obj.result = obj.result + "\n" + value;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  
    //console.log(this, dataArray);

    b = fullString.substring(0, objects[0].spos);
    for (let i = 0; i < objects.length; i++) {
      let j = i + 1;
      if (objects[j] === undefined) {
        b +=
          objects[i].result +
          fullString.substring(objects[i].epos, fullString.length);
      } else {
        b +=
          objects[i].result +
          fullString.substring(objects[i].epos, objects[j].spos);
      }
    }
  
    let bHtml = b.replace(/(?:\r\n|\r|\n)/g, "<br>");
    bHtml = bHtml.replace(/!l /g, " •   ");
    bHtml = bHtml.replace(/!ls /g, "     ○   ");
    bHtml = bHtml.replace(/ /g, "&nbsp;");
  
    bHtml =
      "<div style='font-family: Arial, Helvetica, sans-serif;'>" +
      bHtml +
      "</div>";
  
    let div = document.createElement("div");
    div.classList.add("w3-code", "notranslate", "w3-border-white");
    div.id = "fileDisplay";
    div.innerHTML = bHtml;
  
    if (parseOnly) {
      return div;
    }
  
    let p = document.createElement("p");
    p.innerHTML = "Copied to Clipboard:";
    document.getElementById("output").innerHTML = "";
    document.getElementById("output").appendChild(p);
    document.getElementById("output").appendChild(div);
  
    storeData("userInput", JSON.stringify(dataArray));
    clearData("userInput");
  
    if (returnJSON) {
      return dataArray;
    } else {
      copyToClipBoard(bHtml);
    }
  }

  function parseDataForResult(obj, value) {
    //handle placeholders like title, link, italic
    if (obj.hasOwnProperty("placeholder") && value !== "") {
      //console.log(obj.placeholder);
      //check for markups
      if (activeState.markups.includes(obj.placeholder)) {
        value = parseMarkupmarkups(value, obj.placeholder);
      } else {
        value = obj.placeholder + "\n" + value;
      }
    }
    

    //Plugin TextBlock Insertion according to file _textblocks.txt
    value = parseTextBlocks(value);


    //handle placeholders like !l or !n and set it to final interpreted string for object
    value = parseTextMarkups(value);

    return value;
  }

  export function parseTextMarkups(data) {
    let dataArray = data.split("\n");
    let listFlag = false;
    let listSubFlag = false;
    let boldFlag = false;
    let listNumberFlag = false;
    let listNumberFlagNum = 1;
    for (let i = 0; i < dataArray.length; i++) {
      switch (dataArray[i]) {
        case "!l":
          listFlag = true;
          dataArray.splice(i, 1);
          i = i - 1;
          break;
        case "!ls":
          listSubFlag = true;
          dataArray.splice(i, 1);
          i = i - 1;
          break;
        case "!n":
          listNumberFlag = true;
          dataArray.splice(i, 1);
          i = i - 1;
          break;
        case "!b":
          boldFlag = true;
          dataArray.splice(i, 1);
          i = i - 1;
          break;
        case "!e":
          listFlag = false;
          listNumberFlag = false;
          dataArray.splice(i, 1);
          i = i - 1;
          break;
        case "!es":
          listSubFlag = false;
          dataArray.splice(i, 1);
          i = i - 1;
          break;
        default:
          if (boldFlag) {
            dataArray[i] = "<b>" + dataArray[i] + "</b>";
            boldFlag = false;
          }
          if (listSubFlag) {
            dataArray[i] = "     ○   " + dataArray[i];
            continue;
          }
          if (listFlag) {
            dataArray[i] = " •   " + dataArray[i];
          }
          if (listNumberFlag) {
            dataArray[i] = "  " + listNumberFlagNum + ".  " + dataArray[i];
            listNumberFlagNum++;
          }
      }
    }
    return dataArray.join("\n");
  }
  
  function parseMarkupmarkups(value, markup) {
    switch (markup) {
      case "title":
        return "<b>" + value + "</b>";
        break;
      case "italic":
        return "<i>" + value + "</i>";
        break;
    }
  }
  
  function parseTextBlocks(data) {
    let textBlocks = loadTextBlocks();
    let textBlockIds = Object.keys(textBlocks);
    for (let i = 0; i < textBlockIds.length; i++) {
      let id = textBlockIds[i];
      if (data.indexOf("!" + id) !== -1) {
        //console.log("found: "+id);
        let sPos = data.indexOf("!" + id);
        let ePos = sPos + id.length + 1;
        data =
          data.substring(0, sPos) +
          textBlocks[id] +
          data.substring(ePos, data.length);
      }
    }
    return data;
  }


function loadTextBlocks() {
    let textBlocks = document.getElementById("textBlocks").innerText;
    let textBlocksObject = {};
    if (textBlocks.indexOf("\r\n") !== -1) {
      var wordArrayByLine = textBlocks.split("\r\n");
    } else {
      var wordArrayByLine = textBlocks.split("\n");
    }
  
    for (let i = 0; i < wordArrayByLine.length; i++) {
      let textBlockId = wordArrayByLine[i].split(":")[0];
      let textBlockText = wordArrayByLine[i].substring(
        textBlockId.length + 2,
        wordArrayByLine[i].length
      );
      if (textBlockId.length < 1) {
        continue;
      }
      textBlocksObject[textBlockId.replace(/\s/g, "")] = textBlockText;
    }
    return textBlocksObject;
  }

  function copyToClipBoard(html) {
    // Create an iframe (isolated container) for the HTML
    var container = document.createElement("div");
    container.innerHTML = html;
  
    // Hide element
    container.style.position = "fixed";
    container.style.pointerEvents = "none";
    container.style.opacity = 0;
  
    // Detect all style sheets of the page
    var activeSheets = Array.prototype.slice
      .call(document.styleSheets)
      .filter(function (sheet) {
        return !sheet.disabled;
      });
  
    // Mount the iframe to the DOM to make `contentWindow` available
    document.body.appendChild(container);
  
    // Copy to clipboard
    window.getSelection().removeAllRanges();
  
    var range = document.createRange();
    range.selectNode(container);
    window.getSelection().addRange(range);
  
    document.execCommand("copy");
    for (var i = 0; i < activeSheets.length; i++) activeSheets[i].disabled = true;
    document.execCommand("copy");
    for (var i = 0; i < activeSheets.length; i++)
      activeSheets[i].disabled = false;
  
    // Remove the iframe
    document.body.removeChild(container);
  }

  function mainFormPlaceholder() {
    return "<div class='w3-row-padding w3-padding-24 w3-container w3-flat-clouds'><div class='w3-code notranslate w3-border-white' style='font-family: Arial, Helvetica, sans-serif;'><p>Select a file</p><br><br><br><br><br><br><br><br><br><br><br></div></div>";
  }

  export default parseFormOnSubmit;