<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <title>2-player reaction game</title>
    <style>

      /* General styles */

      html {
        background-color: white;
        height: 100%;
        font-family: sans-serif;
      }

      body {
        height: inherit;
        background-color: red;
        margin: 0;
      }

      * {
        box-sizing: border-box;
      }

      /* UI Layout */

      section {
        width: 100%;
        height: inherit;
        padding: 30px;
      }

      .topbar {
        height: 50%;
        display: flex;
        justify-content: space-between;
      }

      .topbar p, button {
        margin: 0;
        font-size: 1.5rem;
        border: 5px solid;
        border-radius: 20px;
        padding: 10px 20px;
      }

      .p1, .p2 {
        align-self: flex-start;
      }

      .topbar .p1 {
        order: 0;
        border-color: yellow;
        color: yellow;
      }

      .topbar .p2 {
        order: 2;
        border-color: cyan;
        color: cyan;
      }

      .topbar .middlebar {
        order: 1;
      }

      .middlebar {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
      }

      /* Button-specific styling */

      button {
        border: 0;
        padding: 12.75px 20px;
        background-color: #ddd;
        cursor: pointer;
      }

      button:hover, button:focus {
        background-color: #eee;
      }

      button:active {
        background-color: #fff;
      }

      /* spinner-specific styling */

      .spinner {
        position: absolute;
        z-index: 1;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
      }

      .spinner div {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .spinner p {
        margin: 0;
        font-size: 10rem;
      }
    </style>
  </head>
  <body>

    <div class="spinner"><div><p>↻</p></div></div>

    <section class="ui">
      <div class="topbar">
        <p class="p1">Player 1: "A"</p>
        <p class="p2">Player 2: "L"</p>
        <div class="middlebar">
          <button>Start game</button>
          <p class="result"></p>
        </div>
      </div>
    </section>
    <script>

      // Store reference to the spinner and spinner container, create a rotate counter and null startTime
      // and create an uninitialized variable to store a requestAnimationFrame() call in,
      const spinner = document.querySelector('.spinner p');
      const spinnerContainer = document.querySelector('.spinner');
      let rotateCount = 0;
      let startTime = null;
      let rAF;

      // Store references to the start button and the result paragraph
      const btn = document.querySelector('button');
      const result = document.querySelector('.result');

      // function to generate random number
      function random(min,max) {
        var num = Math.floor(Math.random()*(max-min)) + min;
        return num;
      }

      // Create a draw() function
      function draw(timestamp) {
        if(!startTime) {
         startTime = timestamp;
        }

        rotateCount = (timestamp - startTime) / 3;
        
        // If rotateCount gets over 359, set it to 'remainder of dividing by 360'
        if(rotateCount > 359) {
          rotateCount %= 360;
        }

        // Set the rotation of the div to be equal to rotateCount degrees
        spinner.style.transform = 'rotate(' + rotateCount + 'deg)';

        // Call the next frame in the animation
        rAF = requestAnimationFrame(draw);
      }

      // Initially hide the spinner and results
      result.style.display = 'none';
      spinnerContainer.style.display = 'none';

      // Reset the game to its initial state on restart
      function reset() {
        btn.style.display = 'block';
        result.textContent = '';
        result.style.display = 'none';
      }

      // Start the game when the button is pressed

      btn.addEventListener('click', start);

      function start() {
        // Start the spinner spinning
        draw();
        // Show the spinner and hide the button
        spinnerContainer.style.display = 'block';
        btn.style.display = 'none';
        // run the setEndgame() function after a random number of seconds between 5 and 10
        setTimeout(setEndgame, random(5000,10000));
      }

      // Function to allow players to take their turn when the time is right
      function setEndgame() {
        cancelAnimationFrame(rAF);
        spinnerContainer.style.display = 'none';
        result.style.display = 'block';
        result.textContent = 'PLAYERS GO!!';

        document.addEventListener('keydown', keyHandler);

        function keyHandler(e) {
          console.log(e.key);
          if(e.key === "a") {
            result.textContent = 'Player 1 won!!';
          } else if(e.key === "l") {
            result.textContent = 'Player 2 won!!';
          }

          document.removeEventListener('keydown', keyHandler);
          setTimeout(reset, 5000);
        };
      }
    </script>
  </body>
</html>
