<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Creating Session - Web Platform Tests</title>
    <link rel="stylesheet" href="css/bulma-0.7.5/bulma.min.css" />
    <link rel="stylesheet" href="css/style.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script src="lib/wave-service.js"></script>
    <style>
      #console {
        font-family: monospace;
      }
      .spinner {
        height: 0.7em;
        width: 0.7em;
      }
      .fa-pulse {
        -webkit-animation: fa-spin 1s steps(8) infinite;
        animation: fa-spin 1s steps(8) infinite;
      }
      @-webkit-keyframes fa-spin {
        0% {
          -webkit-transform: rotate(0deg);
          transform: rotate(0deg);
        }
        to {
          -webkit-transform: rotate(1turn);
          transform: rotate(1turn);
        }
      }
      @keyframes fa-spin {
        0% {
          -webkit-transform: rotate(0deg);
          transform: rotate(0deg);
        }
        to {
          -webkit-transform: rotate(1turn);
          transform: rotate(1turn);
        }
      }
    </style>
  </head>
  <body>
    <section class="section">
      <div class="container">
        <img src="res/wavelogo_2016.jpg" alt="WAVE Logo" class="site-logo" />

        <h1 class="title is-spaced">
          <span>
            <img src="res/spinner-solid.svg" class="spinner fa-pulse" />
          </span>
          Creating Session
        </h1>
        <div id="content">
          <div id="details-wrapper">
            <div class="detail">
              <div>Reference Tokens:</div>
              <div id="reference-tokens"></div>
            </div>
            <div class="detail">
              <div>Test Paths:</div>
              <div id="test-path"></div>
            </div>
          </div>

          <div id="console"></div>
        </div>
      </div>
    </section>
    <script>
      var screenConsole = document.getElementById("console");
      var log = function() {
        var text = "";
        for (var i = 0; i < arguments.length; i++) {
          text += arguments[i] + " ";
        }
        text = text.replace(/ /gm, "&nbsp;");
        text = text.replace(/\n/gm, "<br/>");
        screenConsole.innerHTML += "<br/>" + text;
      };

      window.onerror = function(error) {
        log(error);
      };

      var HOSTNAME = location.hostname;
      var PORT = location.port;
      var PROTOCOL = location.protocol.replace(/:/, "");
      var QUERY = decodeURIComponent(location.search.replace(/\?/, ""));
      var match = QUERY.match(/token=([^&]+)/);
      var TOKEN = match ? match[1] : null;
      QUERY += /[\?&]path=/.test(location.search) ? "" : "&resume=1";

      var parsedQuery = {};

      var parts = QUERY.split("&");
      for (var i = 0; i < parts.length; i++) {
        var part = parts[i];
        var key = part.split("=")[0];
        var value = part.split("=")[1];
        parsedQuery[key] = value;
      }

      var includedTests = [];
      var paths = parsedQuery["path"].split(",");
      for (var i = 0; i < paths.length; i++) {
        var path = paths[i];
        includedTests.push(path.trim());
      }

      var excludedTests = [
        "/html/semantics/scripting-1/the-script-element/module/dynamic-import/no-active-script-manual-classic.html",
        "/html/semantics/scripting-1/the-script-element/module/dynamic-import/no-active-script-manual-module.html"
      ];

      var referenceTokens = [];
      if (parsedQuery["reftoken"]) {
        var paths = parsedQuery["reftoken"].split(",");
        for (var i = 0; i < paths.length; i++) {
          var path = paths[i];
          referenceTokens.push(path.trim());
        }
      }

      var testPath = document.getElementById("test-path");
      var paths = includedTests;
      for (var i = 0; i < paths.length; i++) {
        var path = paths[i];
        testPath.innerText += path + "\n";
      }
      var referenceTokensElement = document.getElementById("reference-tokens");
      if (referenceTokens.length === 0) {
        referenceTokensElement.innerText = "none";
      } else {
        for (var i = 0; i < referenceTokens.length; i++) {
          var token = referenceTokens[i];
          referenceTokensElement.innerText += token + "\n";
        }
      }
      log("Please wait ...");
      WaveService.createSession(
        {
          tests: { include: includedTests, exclude: excludedTests },
          referenceTokens: referenceTokens
        },
        function(token) {
          log("Session created successfully! Token: " + token);
          log("Redirecting ...");
          location.href = "/newsession.html?token=" + token;
        }
      );
    </script>
  </body>
</html>
