<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Video Requester</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        margin: 20px;
      }
      #urlDisplay {
        margin-top: 20px;
        padding: 10px;
        border: 1px solid #ccc;
        background-color: #f9f9f9;
        white-space: pre-wrap; /* Preserve formatting */
      }
      input {
        padding: 10px;
        margin-top: 10px;
        width: 300px; /* Set a width for input fields */
      }
      button {
        padding: 10px 15px;
        font-size: 16px;
        cursor: pointer;
        margin-top: 10px; /* Space above the button */
        margin-right: 10px; /* Space between buttons */
      }
      .bottom-text {
        width: 100%;
        color: rgb(205, 205, 205);
      }
    </style>
  </head>
  <body>
    <h1>Video Requester</h1>

    <label for="dayInput">Enter Day:</label>
    <input type="text" id="dayInput" placeholder="e.g., 15" />
    <br />
    <label for="monthInput">Enter Month:</label>
    <input type="text" id="monthInput" placeholder="e.g., 12" />
    <br />
    <button id="setYesterday">Set Yesterday</button>
    <button id="setToday">Set Today</button>
    <br />
    <label for="hashInput">Enter Hash:</label>
    <input
      type="text"
      id="hashInput"
      placeholder="e.g., 50a815921ac8992ad5389729970d4820"
    />

    <div>
      <button id="actionRemake">Action = Remake</button>
      <button id="actionList">Action = List</button>
    </div>

    <div id="urlDisplay">URL will appear here...</div>
    <button id="openLink">Open Link in New Tab</button>
    <p>0 done, -1 queue, 1 in progress</p>

    <div class="bottom-text">
      <p>Made by Gromov with ❤️</p>
      <p>2024</p>
      <p>v1.1</p>
    </div>

    <script>
      // Base URL without parameters
      const baseUrl =
        "https://pruffmelab.com/engine/admin/scripts/converter.php?action=";
      let currentAction = "remake"; // Default action

      // Function to clean hash from input or URL
      function cleanHash(input) {
        input = input.trim();
        // Check if input is a Pruffme URL
        if (input.includes("pruffme.com/webinar/")) {
          // Extract hash from URL
          const match = input.match(/[?&]id=([a-fA-F0-9]{32})/);
          return match ? match[1] : input;
        }
        // Remove any non-hexadecimal characters
        return input.replace(/[^a-fA-F0-9]/g, "");
      }

      // Function to update the displayed URL
      function updateUrl() {
        const day = document.getElementById("dayInput").value.trim();
        const month = document.getElementById("monthInput").value.trim();
        const rawHash = document.getElementById("hashInput").value;
        const hash = cleanHash(rawHash);

        // Update hash input field with cleaned hash
        if (hash !== rawHash) {
          document.getElementById("hashInput").value = hash;
        }

        if (day && month && hash) {
          const fullUrl = `${baseUrl}${currentAction}&date=${day}-${month}&hash=${hash}`;
          document.getElementById("urlDisplay").innerText = fullUrl;
          return fullUrl; // Return the constructed URL
        } else {
          document.getElementById("urlDisplay").innerText =
            "Please enter both date and hash.";
          return null; // Return null if inputs are not valid
        }
      }

      // Update URL display when inputs change
      document.getElementById("dayInput").addEventListener("input", updateUrl);
      document
        .getElementById("monthInput")
        .addEventListener("input", updateUrl);
      document.getElementById("hashInput").addEventListener("input", updateUrl);

      // Add paste event listener to handle URL pasting
      document
        .getElementById("hashInput")
        .addEventListener("paste", function (e) {
          // Small delay to let the paste complete
          setTimeout(updateUrl, 0);
        });

      // Set action to list when button is clicked
      document
        .getElementById("actionList")
        .addEventListener("click", function () {
          currentAction = "list";
          updateUrl(); // Update the displayed URL with the new action
        });

      // Set action to remake when button is clicked
      document
        .getElementById("actionRemake")
        .addEventListener("click", function () {
          currentAction = "remake";
          updateUrl(); // Update the displayed URL with the new action
        });

      // Open link in a new tab when the button is clicked
      document
        .getElementById("openLink")
        .addEventListener("click", function () {
          const url = updateUrl(); // Get the updated URL
          if (url) {
            window.open(url, "_blank"); // Open link in a new tab
          }
        });

      // Set today's date on page load
      const today = new Date();
      document.getElementById("dayInput").value = today.getDate();
      document.getElementById("monthInput").value = today.getMonth() + 1;

      document.getElementById("setToday").addEventListener("click", () => {
        const today = new Date();
        document.getElementById("dayInput").value = today.getDate();
        document.getElementById("monthInput").value = today.getMonth() + 1;
        updateUrl(); // Update the URL after setting the date
      });

      document.getElementById("setYesterday").addEventListener("click", () => {
        const yesterday = new Date(today.getTime() - 86400000);
        document.getElementById("dayInput").value = yesterday.getDate();
        document.getElementById("monthInput").value = yesterday.getMonth() + 1;
        updateUrl(); // Update the URL after setting the date
      });

      // Initialize with a default message
      updateUrl();
    </script>
  </body>
</html>
