{{ block title }}
    <center> Player's selection </center>
{{ endblock }}

{{ block content }}

<!-- CSS ----------------------------------------------------------------------------------------------------------- -->
<style>

  .table-container {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    height: 100%;
    position: relative;
  }

  table {
    border-collapse: collapse;
  }

  td.cell {
    border: 1px solid black;
    padding: 0;
    width: 30px;
    height: 30px;
    text-align: center;
    cursor: pointer;
      color: white;
    background-color: white;
  }

  /* Ensure each cell is a square */
  td.cell {
    width: 30px; /* Set width */
  }

  td.cell:before {
    content: "";
    display: block;
    padding-top: 100%; /* Maintain aspect ratio (1:1 square) */
    color: white;
    background-color: white;
  }

  .selected {
    background-color: deepskyblue;
    border: 1px solid black;
  }

  .otree-timer {
    display: none;
  }

  .left-content {
    flex: 1;
    max-width: 50%; /* Limit width */
    overflow-wrap: break-word;
    padding: 0 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .left-box {
    background-color: lightseagreen;
    padding: 10px;
    border-radius: 5px;
    font-weight: bold;
    text-align: center;
    margin-bottom: 10px;
    align-self: flex-start;
    width: 100%;
  }

  .left-text {
    text-align: center;
  }

  .result-text {
    text-align: center;
    font-weight: bold;
    bottom: 0;
  }

  .right-content {
    flex: 1;
    max-width: 50%; /* Limit width */
    overflow-wrap: break-word;
    padding: 0 10px;
  }

  .right-box {
    background-color: mediumpurple;
    padding: 10px;
    border-radius: 5px;
    font-weight: bold;
    text-align: center;
    margin-bottom: 10px;
    max-width: 100%;
  }

  .current-cell {
    border: 3px solid yellow;  /* Add a gray border */
  }

  .highlighted-cell {
    background-color: yellow;  /* Set background color to light red */
    border: 1px solid black;
  }

  .current-highlighted-cell {
    background-color: lightpink;  /* Set background color to light red */
    border: 3px solid yellow;  /* Add a gray border */
  }

  .form-section button {
    width: 100px;
    height: 40px;
    /* Additional styles for the button (e.g., background-color, font-size, etc.) */
  }

  .background-box-ObsA {
    padding: 10px;
    border-radius: 5px;
    margin-bottom: 10px;
    align-self: flex-start;
    width: 69%; /* 100% spans the entire  */
    height: 110%;
    background-color: rgba(255, 237, 189, 0.5);
    position: absolute;
    top: -5%;
    left: -1%;
    z-index: -1; /* Place it behind other content */
  }

  .background-box-ObsB {
    padding: 10px;
    border-radius: 5px;
    margin-bottom: 10px;
    align-self: flex-start;
    width: 68%; /* 100% spans the entire  */
    height: 110%;
    background-color: rgba(255, 237, 189, 0.5); /* Semi-transparent black */
    position: absolute;
    top: -5%;
    right: 0%;
    z-index: -1; /* Place it behind other content */
  }

</style>

<!-- JS ----------------------------------------------------------------------------------------------------------- -->
<script>
  var ObsA_show_form_field = {{ ObsA_show_form_field }};
  var ObsB_show_form_field = {{ ObsB_show_form_field }};
  var player = {{player_id}};

  // This is to measure reaction time
  var startTime
  window.onload = function() {
    startTime = Date.now();
  }

    // Check if the input field is empty and enable/disable the button
  function checkInput() {
    var input = document.getElementById('predicted_reward_JS').value;
    document.getElementById('manualSubmitButton').disabled = false;
  }

  // Function to calculate time and save it to the hidden field
  function saveTimes() {
    var reaction_time = Date.now() - startTime;
    // Save the number in the hidden field
    // document.getElementById('page_end_time').value = Date.now();
    if (ObsA_show_form_field === 1) {
      document.getElementById('RT_predicted_reward').value = reaction_time;
    }
   //else {
   //   document.getElementById('RT_predicted_reward').value = NaN;
   //}
    // Attach the doTimeStuff function to the form's submit event
    document.querySelector('form').addEventListener('submit', saveTimes);
  }

  // Function to handle user input and trigger liveSend
  var formSubmitted = false;
  function send_predicted_reward_JS() {
    // Check if the form has already been submitted
    if (formSubmitted) {
      return;  // Do not process if the form has already been submitted
    }

    var predicted_reward_variable_for_JS
    if (ObsA_show_form_field === 1) {
      predicted_reward_variable_for_JS = parseFloat(document.getElementById("predicted_reward_JS").value.trim())
    }
    else {
      predicted_reward_variable_for_JS = document.getElementById("predicted_reward_JS_else")
    }
    saveTimes();
    liveSend({"predicted_reward": predicted_reward_variable_for_JS})
    formSubmitted = true;
  }

  function executeLiveRecv() {
    // Reminder: Block means a style in which the element is displayed (not that it is blocked)
    if (ObsA_show_form_field === 1) {
        document.getElementById("ObsA_show_guess").style.display = "block";
    }
    if (ObsB_show_form_field === 1) {
        document.getElementById("ObsB_show_guess").style.display = "block";
    }

    if (ObsA_show_form_field === 1) {
      document.getElementById("manualSubmitButton").disabled = true;
    }

    // Hide elements after 2.5 seconds. not sure which one delays it but seems to work
    setTimeout(function () {
      form.submit();
    }, 6000);
  }

  // This is also for Live Pages
  // the name data['predicted_reward_variable_for_JS'] is what comes from python
  function liveRecv(data) {
      result_predicted_reward_player1 = data['predicted_reward_player1'];
      result_predicted_reward_player2 = data['predicted_reward_player2'];
      //console.log('result_predicted_reward_player1', result_predicted_reward_player1)
      //console.log('result_predicted_reward_player2', result_predicted_reward_player2)


      if (data['predicted_reward_player1'] !== undefined) {
          document.getElementById("predicted_reward_player1_display").innerHTML = data['predicted_reward_player1'];
      }
      if (data['predicted_reward_player2'] !== undefined) {
          document.getElementById("predicted_reward_player2_display").innerHTML = data['predicted_reward_player2'];
      }


      if (ObsA_show_form_field === 1 && ObsB_show_form_field ===1) {
          if (data['all_submitted']) {
              console.log('all submitted = true')
              console.log('live receive worked',result_predicted_reward_player1, result_predicted_reward_player2 )
              executeLiveRecv();
          }
      }
      else {
          executeLiveRecv()
      }

  }

</script>


<!-- HTML --------------------------------------------------------------------------------------------------------- -->

<div class="table-container">

  {% if ObsA_sees %}
    <div class="background-box-ObsA"></div>
  {% endif %}

  {% if ObsB_sees %}
    <div class="background-box-ObsB"></div>
  {% endif %}

  <div class="left-content">
    <!-- Left box Obs A-->
    <div class="left-box">
      You (Observer A)
    </div>

    <!-- Text centered under the left box -->
    <div class="left-text">
      <p>{{ ObsA_query_row1 }}</p>
      <p>{{ ObsA_query_row2 }}</p>

      <!-- This is a form field RT. It's hidden and needs to get send anyways -->
      {% if ObsA_show_form_field %}
        <!-- name is for .py in otree, id is for JS-->
        <p> <input name=predicted_reward id="predicted_reward_JS" oninput="checkInput()"></p>
        <!-- on click activates JS function, > Submit is the text shown on the button-->
        <p> <button type="button" onclick="send_predicted_reward_JS()" id="manualSubmitButton" disabled>Submit </button> </p>
        <input type="hidden" name="RT_predicted_reward" id="RT_predicted_reward"/>
      {% endif %}

      <!-- Only show if B was asked the query>-->
      <div id="ObsB_show_guess" style="display: none" class="result-text">
          {% if player_id == 1 %}
            <p>{{ ObsB_show_guess }} <span id="predicted_reward_player2_display"></span></p>
          {% elif player_id == 2 %}
            <p>{{ ObsB_show_guess }} <span id="predicted_reward_player1_display"></span></p>
          {% endif %}
      </div>
    </div>
  </div>

  <!-- Table -->

  <table>
    {% for row in grid_data %}
      <tr>
        {% for cell in row %}
          {% if cell.row_index == current_highlight_index_row and cell.column_index == current_highlight_index_column and ObsA_show_form_field == True %}
            <td class="highlighted-cell"> </td>
          {% elif cell.row_index == current_highlight_index_row and cell.column_index == current_highlight_index_column and ObsA_show_form_field != True %}
            <td class="highlighted-cell"> </td>
          {% else %}
            <td class="cell"></td>
          {% endif %}
        {% endfor %}
      </tr>
    {% endfor %}
  </table>

 <div class="right-content">
    <!-- Right box Obs B -->
    <div class="right-box">
      Observer B
    </div>

    <div class="left-text">
      <p>{{ ObsB_query_row1 }}</p>
      <p>{{ ObsB_query_row2 }}</p>


      <!-- Only show if B was asked the query>-->
      <div id="ObsA_show_guess" style="display: none" class="result-text">
          {% if player_id == 1 %}
            <p>{{ ObsA_show_guess }} <span id="predicted_reward_player1_display"></span></p>
          {% elif player_id == 2 %}
            <p>{{ ObsA_show_guess }} <span id="predicted_reward_player2_display"></span></p>
          {% endif %}
      </div>

    </div>
  </div>
</div>


{% endblock %}