<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Amaranth:ital,wght@0,400;0,700;1,400;1,700&family=Kanit:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Oswald:wght@200..700&display=swap"
      rel="stylesheet"
    />

    <!-- for favicon -->
    <link
      rel="apple-touch-icon"
      sizes="180x180"
      href="favicon/apple-touch-icon.png"
    />
    <link
      rel="icon"
      type="image/png"
      sizes="32x32"
      href="favicon/favicon-32x32.png"
    />
    <link
      rel="icon"
      type="image/png"
      sizes="16x16"
      href="favicon/favicon-16x16.png"
    />
    <link rel="manifest" href="/site.webmanifest" />
    <meta name="msapplication-TileColor" content="#da532c" />
    <meta name="theme-color" content="#ffffff" />

    <!--  -->
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Chatters:Text-Chat</title>

    <!-- Stylesheets -->

    <link rel="stylesheet" href="css/main.css" />
    <link rel="stylesheet" href="css/responsive.css" />
    <link rel="stylesheet" href="css/after.css" />
    <link rel="stylesheet" href="css/hover.css" />
    <link rel="stylesheet" href="css/textChat.css" />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
      integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    />
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
    />
    <link
      rel="stylesheet"
      href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0"
    />
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body style="background-color: #101010">
    <!-- header section -->

    <header>
      <!-- nav section -->
      <nav class="navbar navbar-expand-lg">
        <div class="container-fluid">
          <a class="navbar-brand" href="/"
            ><img
              class="navChatterLogo"
              src="https://chatters-lhfj.onrender.com/res/imgs/logo2.svg"
              alt="banner"
          /></a>
          <button
            class="navbar-toggler"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#navbarNavAltMarkup"
            aria-controls="navbarNavAltMarkup"
            aria-expanded="false"
            aria-label="Toggle navigation"
          >
            <span class="material-symbols-outlined navbar-toggler-icon">
              density_medium
            </span>
          </button>
          <div
            class="routesDiv collapse navbar-collapse"
            id="navbarNavAltMarkup"
          >
            <div class="routes navbar-nav">
              <a
                class="green green-hover-underline-animation nav-link"
                aria-current="page"
                href="/"
                >Home</a
              >
              <a
                class="neonBlue neonBlue-hover-underline-animation nav-link"
                href="/video-chat"
                >Video</a
              >
              <!-- <a
                                class="orange orange-hover-underline-animation nav-link"
                                href="">Feedback</a> -->

              <div class="loginButtonDiv">
                <form method="get" action="/login">
                  <button type="submit" class="login">Login</button>
                </form>
              </div>
            </div>
          </div>
        </div>
      </nav>
    </header>

    <!-- main section -->
    <div class="messageBox div">
      <span class="usersCountSpan"
        ><p style="color: red" class="usersCount">0</p>
        live</span
      >
      <span class="commentsSpan">
        <p style="color: red" class="comments"></p>
        Disconnected</span
      >
    </div>

    <footer>
      <div class="chatSection">
        <button class="new-skipButton new">New</button
        ><input
          readonly
          disabled
          type="text"
          name
          placeholder="Message"
          class="inputBox"
          id="message"
        /><button disabled id="sendBtn">send</button>
      </div>
    </footer>

    <!-- Javascript Code -->
    <script src="/js/textChatScript.js"></script>
    <script src="/js/onClick.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      //             document.addEventListener("contextmenu",(e)=>{
      // e.preventDefault();
      //             })

      // new socket
      const socket = io();
      socket.disconnect();

      // section
      const new_skipButton = document.querySelector(".new-skipButton");
      const chatDiv = document.querySelector(".div");
      const sendButton = document.getElementById("sendBtn");
      const inputBox = document.getElementById("message");
      const usersCount = document.querySelector(".usersCount");
      const commentsPara = document.querySelector(".comments");
      const commentsSpan = document.querySelector(".commentsSpan");

      // console.log(commentsPara);
      // console.log("Hello",usersCount);

      // function :message send to server
      function sendMessage(e) {
        const message = inputBox.value;
        if (message !== "") {
          // clearing the current value
          inputBox.value = "";
          console.log(message);
          // sending message to server
          socket.emit("user-message", message);
          // new div creation
          const newDiv = document.createElement("div");
          newDiv.innerText = "You:" + message;
          chatDiv.appendChild(newDiv);
          inputBox.focus();
        }
      }

      // skip or new button javascript
      new_skipButton.addEventListener("click", () => {
        // console.log(
        //     new_skipButton.classList
        // );
        if (new_skipButton.classList[1] === "new") {
          new_skipButton.innerText = "Skip";
          new_skipButton.classList.remove("new");
          new_skipButton.classList.add("skip");

          socket.connect();
          inputBox.placeholder = "Waiting For stranger";
          inputBox.disabled = false;
          commentsSpan.style.display = "none";
          commentsPara.innerText = "";
        } else {
          if (new_skipButton.classList[1] === "skip") {
            sendButton.disabled = true;
            inputBox.disabled = true;
            socket.disconnect();
            commentsSpan.style.display = "flex";
            commentsPara.innerText = "You";
            new_skipButton.innerText = "New";
            new_skipButton.classList.remove("skip");
            new_skipButton.classList.add("new");
          }
        }
      });

      // Messege send to server by clicking sendButton
      sendButton.addEventListener("click", (e) => sendMessage(e));

      // sending message to server by ENTER
      inputBox.addEventListener("keypress", (e) => {
        if (e.key === "Enter") {
          sendMessage();
        }
      });

      // send button enable/stranger connected
      socket.on("strangerConnection", (condition) => {
        console.log("i'm condition ", condition);
        if (condition) {
          inputBox.readOnly = false;
          inputBox.placeholder = "Message";

          sendButton.disabled = !condition;
          socket.emit("connectionReq", true);
        }
      });

      // message recieve from the sever
      socket.on("message", (msage) => {
        console.log("Message from server", msage);

        const newDiv = document.createElement("div");
        newDiv.innerText = "Stranger:" + msage;
        chatDiv.appendChild(newDiv);
      });

      // users count
      socket.on("ttl", (count) => {
        usersCount.innerText = count;
        console.log(count);
      });

      //what if stranger disconnects
      socket.on("disconnectedUser", (condition) => {
        // console.log("Someone Disconnected");
        if (condition) {
          inputBox.readOnly = true;
          sendButton.disabled = true;
          inputBox.disabled = true;
          socket.disconnect();
          new_skipButton.innerText = "New";
          new_skipButton.classList.remove("skip");
          new_skipButton.classList.add("new");

          socket.disconnect();
          commentsSpan.style.display = "flex";
          commentsPara.innerText = "Stranger";
        }
      });
    </script>
  </body>
</html>
