document.addEventListener("DOMContentLoaded", function () {
  const svgFile = document.getElementById("fileInput");
  // Event listener for file selection
  svgFile.addEventListener("change", function (event) {
    const file = event.target.files[0];
    if (file) {
      handleDotFileUpload(file);
    }
  });

  const textFile = document.getElementById("prismModelInput");

  textFile.addEventListener("change", function (event) {
    const file = event.target.files[0];
    if (file) {
      handleCodeFileUpload(file);
    }
  });
  const specs = document.getElementById("specificationsInput");

  specs.addEventListener("change", function (event) {
    const file = event.target.files[0];
    if (file) {
      handleSpecsFileUpload(file);
    }
  });

  // Function to handle file upload for editor
  function handleCodeFileUpload(file) {
    const reader = new FileReader();

    reader.onload = function (event) {
      const fileContents = event.target.result;
      editor.setValue(fileContents);
    };

    reader.onerror = function (event) {
      console.error("Error reading file:", event);
    };

    // Read the file as text
    reader.readAsText(file);
  }
  // Function to handle file upload for specs
  function handleSpecsFileUpload(file) {
    const reader = new FileReader();

    reader.onload = function (event) {
      const fileContents = event.target.result;
      editor2.setValue(fileContents);
    };

    reader.onerror = function (event) {
      console.error("Error reading file:", event);
    };

    // Read the file as text
    reader.readAsText(file);
  }

  // Function to handle file upload for SVG
  function handleDotFileUpload(file) {
    const reader = new FileReader();

    reader.onload = function (event) {
      const fileContents = event.target.result;
      Viz.instance().then(function (viz) {
        var svg = viz.renderSVGElement(fileContents);
        graphFunction(svg);
        updateTextarea(getActiveTabContent());
      });
    };

    reader.onerror = function (event) {
      console.error("Error reading file:", event);
    };

    // Read the file as text
    reader.readAsText(file);
  }

  // Function for converting input to text format and automatically downloading the content
  (function () {
    // Declare a variable for the text file URL
    var textFile = null;

    // Function to create the text file and return its URL
    var makeTextFile = function (text) {
      // Create a Blob from the text
      var data = new Blob([text], { type: "text/plain" });

      // Revoke any previous object URL to avoid memory leaks
      if (textFile !== null) {
        window.URL.revokeObjectURL(textFile);
      }

      // Create a new object URL for the Blob and return it
      textFile = window.URL.createObjectURL(data);
      return textFile;
    };

    var download = document.getElementById("downloadFirstEditor");
    var download2 = document.getElementById("downloadSpecifications");

    var input1 = editor;
    var input2 = editor2;

    // Add an event listener to the button for the click event
    download.addEventListener("click", function () {
      // Check if the editor is empty
      if (!editor.getValue()) {
        Toastify({
          text: "Editor field empty",
          duration: 3000,
          close: true,
          gravity: "top",
        }).showToast();
      } else {
        // Create the text file URL from the input editor value
        var fileURL = makeTextFile(input1.getValue());

        // Create a temporary link element
        var tempLink = document.createElement("a");

        tempLink.setAttribute("download", "prism.txt");

        // Set the link's href attribute to the file URL
        tempLink.href = fileURL;

        // Append the link to the document body (required for the click event to work in some browsers)
        document.body.appendChild(tempLink);

        tempLink.click();

        // Remove the link from the document body after the download
        document.body.removeChild(tempLink);
      }
    });

    download2.addEventListener("click", function () {
      if (!editor2.getValue()) {
        Toastify({
          text: "Specifications field empty",
          duration: 3000,
          close: true,
          gravity: "top",
        }).showToast();
      } else {
        var fileURL = makeTextFile(input2.getValue());

        var tempLink = document.createElement("a");

        tempLink.setAttribute("download", "specifications.txt");

        tempLink.href = fileURL;

        document.body.appendChild(tempLink);

        tempLink.click();

        document.body.removeChild(tempLink);
      }
    });
  })();
});