// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
//
// Client-side logic for the referee interface.

var websocket;
var foulTeamButton;
var foulRuleButton;

// Handles a click on a team button.
var setFoulTeam = function(teamButton) {
  if (foulTeamButton) {
    foulTeamButton.attr("data-selected", false);
  }
  foulTeamButton = $(teamButton);
  foulTeamButton.attr("data-selected", true);

  $("#commit").prop("disabled", !(foulTeamButton && foulRuleButton));
};

// Handles a click on a rule button.
var setFoulRule = function(ruleButton) {
  if (foulRuleButton) {
    foulRuleButton.attr("data-selected", false);
  }
  foulRuleButton = $(ruleButton);
  foulRuleButton.attr("data-selected", true);

  $("#commit").prop("disabled", !(foulTeamButton && foulRuleButton));
};

// Sets button styles to match the selection cached in the global variables.
var setSelections = function() {
  $("[data-team]").each(function(i, teamButton) {
    $(teamButton).attr("data-selected", $(teamButton).attr("data-team") == foulTeam);
  });

  $("[data-rule]").each(function(i, ruleButton) {
    $(ruleButton).attr("data-selected", $(ruleButton).attr("data-rule") == foulRule);
  });
};

// Resets the buttons to their default selections.
var clearFoul = function() {
  if (foulTeamButton) {
    foulTeamButton.attr("data-selected", false);
    foulTeamButton = null;
  }
  if (foulRuleButton) {
    foulRuleButton.attr("data-selected", false);
    foulRuleButton = null;
  }
  $("#commit").prop("disabled", true);
};

// Sends the foul to the server to add it to the list.
var commitFoul = function() {
  websocket.send("addFoul", {Alliance: foulTeamButton.attr("data-alliance"),
      TeamId: parseInt(foulTeamButton.attr("data-team")), Rule: foulRuleButton.attr("data-rule"),
      IsTechnical: foulRuleButton.attr("data-is-technical") == "true"});
};

// Removes the foul with the given parameters from the list.
var deleteFoul = function(alliance, team, rule, isTechnical, timeSec) {
  websocket.send("deleteFoul", {Alliance: alliance, TeamId: parseInt(team), Rule: rule,
      IsTechnical: isTechnical, TimeInMatchSec: timeSec});
};

// Cycles through no card, yellow card, and red card.
var cycleCard = function(cardButton) {
  var newCard = "";
  if ($(cardButton).attr("data-card") == "") {
    newCard = "yellow";
  } else if ($(cardButton).attr("data-card") == "yellow") {
    newCard = "red";
  }
  websocket.send("card", {Alliance: $(cardButton).attr("data-alliance"),
      TeamId: parseInt($(cardButton).attr("data-card-team")), Card: newCard});
  $(cardButton).attr("data-card", newCard);
};

// Signals to the volunteers that they may enter the field.
var signalVolunteers = function() {
  websocket.send("signalVolunteers");
};

// Signals to the teams that they may enter the field.
var signalReset = function() {
  websocket.send("signalReset");
};

// Signals the scorekeeper that foul entry is complete for this match.
var commitMatch = function() {
  websocket.send("commitMatch");
};

$(function() {
  // Set up the websocket back to the server.
  websocket = new CheesyWebsocket("/displays/referee/websocket", {
  });

  clearFoul();
});
