{{ 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;
  }

  .form-container {
    display: flex;
    flex-direction: column; /* this is for displaying the text under each other, instead of next to each other */
    align-items: center;
    justify-content: flex-start;
    height: 100vh;
    /* margin-top: 0rem; this doesn't seem to do anything */
  }

  /* CSS styles for the table */
  table {
    border-collapse: collapse;
  }

  td {
    border: 1px solid black;
    padding: 0;
    width: 30px;
    height: 30px;
    text-align: center;
    cursor: pointer; /* Add this to show the pointer cursor when hovering over cells */
  }

  /* Add a class to style the selected cell (customize as you like) */
  .selected {
    background-color: lightseagreen;
    border: 1px solid black;
  }

  .ObsB_choice_hidden {
    cursor: pointer;
  }

  .ObsB_both_selected {
    border: 5px solid mediumpurple;
  }

  .ObsB_selected {
    background-color: mediumpurple;
    border: 1px solid black;
  }

  /* Beautfiul. This way I can use the pre-build function in python and now show how much time is left on the participant screen  */
  .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%;
  }

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

</style>

<!-- JS --------------------------------------------------------------------------------------------------------------->
<script>

  var formSubmitted = false;
  var ObsA_show_form_field = {{ ObsA_show_form_field }};
  var ObsB_show_form_field = {{ ObsB_show_form_field }};
  var player_id = {{player_id}};

  function send_predicted_choice_JS() {
    var predicted_choice_variable_for_JS = document.getElementById("predicted_choice").value.trim();
    var predicted_choice_RT_for_JS = document.getElementById("RT_predicted_choice").value.trim();
      liveSend({"predicted_choice": predicted_choice_variable_for_JS, "RT_predicted_choice": predicted_choice_RT_for_JS})
  }

<!-- JavaScript code to handle cell selection and form submission -->

  document.addEventListener("DOMContentLoaded", function() {
    if (ObsA_show_form_field === 1) {
      const cells = document.querySelectorAll(".cell");
      let selectedCell = null;
      let startTime = Date.now();

      cells.forEach(cell => {
        cell.addEventListener("click", function () {

          if (formSubmitted) {
            return; // Ignore click events if the form has been submitted
          }

          if (selectedCell !== null) {
            // Deselect the previously selected cell
            selectedCell.classList.remove("selected");
          }

          // Get the coordinates of the selected cell
          const row = this.parentElement.rowIndex;
          const col = this.cellIndex;

          // Check if the clicked cell is already selected
          if (selectedCell === this) {
            // If the cell is already selected, set it to null (deselect it)
            selectedCell = null;
            // Clear the value of the hidden input field
            document.getElementById('predicted_choice').value = '';
            // Disable the submit button
            document.getElementById('submit_button').disabled = true;
          } else {
            // Select the clicked cell
            this.classList.add("selected");
            // Set the selectedCell variable to the current cell
            selectedCell = this;
            // Prepare the data to be sent to the server
            const data = {
              row: row,
              col: col,
            };

            // Set the value of the hidden input field to the selected cell coordinates
            document.getElementById('predicted_choice').value = [row, col];

            // Set the value of the hidden input field to the reaction time, which is time now - time when page was first displayed
            document.getElementById('RT_predicted_choice').value = Date.now() - startTime;
            // Enable the submit button
            // document.getElementById('submit_button').disabled = false;
            // Enable the submit button
            const submitButton = document.getElementById('submit_button');
            submitButton.disabled = false;


            // Attach click event listener to the submit button when it's enabled
            submitButton.addEventListener("click", function () {
              send_predicted_choice_JS();
              this.disabled = true;
              formSubmitted = true;
            });
          }
          console.log(row, col);
        });
      });
    }
    else {
      document.getElementById('predicted_choice').value = ['NA', 'NA'];
      document.getElementById('RT_predicted_choice').value = '';
      send_predicted_choice_JS();
      //setTimeout(function () {
      //  send_predicted_choice_JS();
      //}, ObsB_time_to_decide);
    }
  });


    function highlightCell(rowIndex, columnIndex, bothSelected) {
      setTimeout(function() {
          var table = document.querySelector('table');
          var row = table.rows[rowIndex]; // Get the specific row
          if (row) {
            var cell = row.cells[columnIndex]; // Get the specific cell
            if (cell) {
                if (bothSelected) {
                    cell.classList.add('ObsB_both_selected');
                }
                else {
                    cell.classList.add('ObsB_selected');
                }
            }
          }
      }, 10)
    }

  function liveRecv(data) {
    //console.log('live receive')
    var bothSelected = false;

    if (ObsA_show_form_field && ObsB_show_form_field) {
        if (data['predicted_choice_row_player1'] !== undefined) {
            predictedChoiceRowPlayer1 = data['predicted_choice_row_player1'];
            predictedChoiceColumnPlayer1 = data['predicted_choice_column_player1'];
        }

        if (data['predicted_choice_row_player2'] !== undefined) {
            predictedChoiceRowPlayer2 = data['predicted_choice_row_player2'];
            predictedChoiceColumnPlayer2 = data['predicted_choice_column_player2'];
        }
        if (data['all_submitted']) {
            if (predictedChoiceRowPlayer1 === predictedChoiceRowPlayer2 && predictedChoiceColumnPlayer1 === predictedChoiceColumnPlayer2) {
              bothSelected = true;
            }
            if (ObsA_show_form_field === 1) {
                document.getElementById("show_ObsA_guess_text").style.display = "block";
            }
            if (ObsB_show_form_field === 1) {
                document.getElementById("show_ObsB_guess_text").style.display = "block";
            }

            if (player_id === 1 ) {
                highlightCell(predictedChoiceRowPlayer2, predictedChoiceColumnPlayer2, bothSelected)
            }
            if (player_id === 2) {
                highlightCell(predictedChoiceRowPlayer1, predictedChoiceColumnPlayer1, bothSelected)
            }

            // case that both have query --> need B in addition to A
            // Delayed submission after 2.5 seconds
            setTimeout(function () {
                // Submit the form
                form.submit(); // Ensure 'form' is defined and refers to your form element
            }, 6000);
        }
    }
    else {

        predictedChoiceRowPlayer1 = data['predicted_choice_row_player1'];
        predictedChoiceColumnPlayer1 = data['predicted_choice_column_player1'];

        predictedChoiceRowPlayer2 = data['predicted_choice_row_player2'];
        predictedChoiceColumnPlayer2 = data['predicted_choice_column_player2'];

        if (ObsA_show_form_field === 1) {
            document.getElementById("show_ObsA_guess_text").style.display = "block";
        }
        if (ObsB_show_form_field === 1) {
            document.getElementById("show_ObsB_guess_text").style.display = "block";
        }

        if (player_id === 1 && ObsA_show_form_field === 0) {
            highlightCell(predictedChoiceRowPlayer2, predictedChoiceColumnPlayer2, bothSelected)
        }
        if (player_id === 2 && ObsA_show_form_field === 0) {
            highlightCell(predictedChoiceRowPlayer1, predictedChoiceColumnPlayer1, bothSelected)
        }

        // case that both have query --> need B in addition to A
        // Delayed submission after 2.5 seconds
        setTimeout(function () {
            // Submit the form
            form.submit(); // Ensure 'form' is defined and refers to your form element
        }, 6000);
    }
  }

</script>



<!-- Display the form and input field -->
<!-- This way the table container is the parent and the form container the child -->
<div class="table-container">

  <div class="left-content">

    <!-- Left content with grey box -->
    <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>

      <!-- Show submit button and save if participant is asked query -->
      {% if ObsA_show_form_field %}
        <p><button type="button" id="submit_button" disabled>Submit</button></p>
      {% endif %}
      <input type="hidden" name="predicted_choice" id="predicted_choice" />
      <input type="hidden" name="RT_predicted_choice" id="RT_predicted_choice" />

      <!-- Show text if Obs B is asked query -->
      <div id="show_ObsB_guess_text" style="display: none" class="result-text">
        {% if ObsB_show_form_field %}
          <p>{{ ObsB_show_guess_text }} </p>
        {% endif %}
      </div>
    </div>
  </div>

  <!-- Grid -->
  <table id="table">
    {% for row in grid_data %}
      <tr>
        {% for value in row %}
            <td class="cell"></td>
        {% endfor %}
      </tr>
    {% endfor %}
  </table>



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

    <!-- Text centered under the right box -->
    <div class="left-text">
      <!-- Show query text if Obs B is asked query -->
      <p>{{ ObsB_query_row1 }}</p>
      <p>{{ ObsB_query_row2 }}</p>

      <!-- Show query text if Obs A was asked query -->
      <div id="show_ObsA_guess_text" style="display: none" class="result-text">
        {% if ObsA_show_form_field %}
          Observer A's answer is highlighted in green.
        {% endif %}
      </div>
    </div>
  </div>
</div>

{% endblock %}