<!DOCTYPE html>
<html lang="en">
<head>
  <title>Test Client</title>
  <script src="pop2.js"></script>
  <script>
      const ssoService = "http://localhost:9090";

      POP2.init(ssoService + "/authorize", "f4cdb93d-fe28-427b-b037-f03f44c86a16", "openid profile age email", 500, 600);

      window.addEventListener("load", function () {
          doThisThing(false);
      })

      function updateTokenInfo(data) {
          document.getElementById("someTextArea").textContent = JSON.stringify(data, null, 2);
          POP2.getToken(function (x) {
              document.getElementById("tokenValues").textContent = JSON.stringify(parseJwt(x), null, 2);
          });
      }

      function parseJwt(token) {
          const base64Url = token.split('.')[1];
          const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
          const jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function (c) {
              return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
          }).join(''));
          return JSON.parse(jsonPayload);
      }

      function doThisThing(popup) {
          POP2.clientRequest(ssoService + "/userinfo", {}, popup).then(function (x) {
              return x.json();
          }).then(function (x) {
              updateTokenInfo(x);
          }).catch(function (x) {
              console.error(x);
          });
      }
  </script>
  <style>
      :root {
          color-scheme: light dark;
      }

      #someTextArea {
          width: 400px;
          height: 400px;
      }

      #tokenValues {
          width: 400px;
          height: 400px;
      }
  </style>
</head>
<body>
<header>
  <h1>Test Client</h1>
</header>
<main>
  <div>
    <button onclick="doThisThing(true);">Login</button>
  </div>
  <div style="display:flex; gap: 2em;">
    <div>
      <div>
        <label for="someTextArea"></label><textarea id="someTextArea"></textarea>
      </div>
      <div>
        <label for="tokenValues"></label><textarea id="tokenValues"></textarea>
      </div>
    </div>
    <div>
      <p>Permissions:</p>
      <ul id="somePerms"></ul>
    </div>
  </div>
</main>
</body>
</html>