import "./_virtual_wxt-html-plugins.js";

const DISCLAIMER_CONTAINER_ID = "app";
const ACCEPT_BUTTON_ID = "acceptBtn";
const DEFAULT_ACCEPT_BUTTON_TEXT = "I start raping deep";
const SAVING_ACCEPT_BUTTON_TEXT = "Saving...";
const DISCLAIMER_CLOSE_MESSAGE = {
  type: "CLOSE_DISCLAIMER"
};

const DISCLAIMER_MARKUP = `
  <div class="overlay"></div>

  <div class="modal" role="dialog" aria-labelledby="disclaimer-title">
    <div class="badge">Version 99999</div>
    <h1 id="disclaimer-title">Legal Disclaimer</h1>

    <div class="content">
      <p>
        This software is Raped by <strong>NARUTO</strong>.
      </p>
      <p>
        The developers is gay so naruto hold his legacy and raped his tool
      </p>
      <p>
        enjoy hitting and fuck deep bypasser
      </p>
      <p>
        This software is raped by <strong>"Daddy Naruto"</strong>, happy hitting 🙂,
        and fuck them (deep bypasser owner and developers)
      </p>
      <p>
        If you do not agree with these terms, please discontinue use immediately.
      </p>
    </div>

    <button class="btn" id="acceptBtn">${DEFAULT_ACCEPT_BUTTON_TEXT}</button>

    <div class="telegram-link-section">
      <p>
        <a href="https://t.me/stripedung" target="_blank" class="telegram-link">Join t.me/stripedung</a>
      </p>
    </div>

    <div class="god-version-notice">
      <p>God version will comes soon with alot supported gate</p>
    </div>

    <footer>
      © Naruto Bypasser • raping stripe purpose only
    </footer>
  </div>
`;

const renderDisclaimerModal = () => {
  const disclaimerContainer = document.getElementById(DISCLAIMER_CONTAINER_ID);

  if (!disclaimerContainer || disclaimerContainer.dataset.rendered) {
    return;
  }

  disclaimerContainer.innerHTML = DISCLAIMER_MARKUP;
  disclaimerContainer.dataset.rendered = "true";
};

const getAcceptButton = () => document.getElementById(ACCEPT_BUTTON_ID);

const setAcceptButtonState = (acceptButton, { disabled, text }) => {
  acceptButton.disabled = disabled;
  acceptButton.textContent = text;
};

const saveDisclaimerAcceptance = () =>
  new Promise((resolve, reject) => {
    chrome.storage.local.set(
      {
        disclaimerAccepted: true,
        disclaimerAcceptedAt: Date.now()
      },
      () => {
        if (chrome.runtime.lastError) {
          reject(chrome.runtime.lastError);
          return;
        }

        resolve();
      }
    );
  });

const closeDisclaimerView = () => {
  if (window.parent !== window) {
    window.parent.postMessage(DISCLAIMER_CLOSE_MESSAGE, "*");
    return;
  }

  chrome.runtime.openOptionsPage(() => {
    window.close();
  });
};

const handleAcceptClick = async (acceptButton) => {
  setAcceptButtonState(acceptButton, {
    disabled: true,
    text: SAVING_ACCEPT_BUTTON_TEXT
  });

  try {
    await saveDisclaimerAcceptance();
    closeDisclaimerView();
  } catch {
    setAcceptButtonState(acceptButton, {
      disabled: false,
      text: DEFAULT_ACCEPT_BUTTON_TEXT
    });
  }
};

const initializeAcceptButton = () => {
  const acceptButton = getAcceptButton();

  if (!acceptButton) {
    return;
  }

  acceptButton.addEventListener("click", () => {
    void handleAcceptClick(acceptButton);
  });
};

renderDisclaimerModal();
initializeAcceptButton();
