const MAX_PROMPT_ITEMS = 20;

function undisplay(element) {
  element.style.display="none";
}

function add_message(severity, message) {
  message = `<div class="message ${severity}" onclick="undisplay(this)">
<span class="hide-button" onclick="undisplay(this.parentElement)">&times;</span>
${message}
</div>`;
  const message_area = document.getElementById('message-area');
  message_area.insertAdjacentHTML('beforeend', message);
}

/*********************************************************
 * Handling server-sent event stream                     *
 *********************************************************/

const events = new EventSource("events");

events.onerror = function(event) {
  if (event.target.readyState === EventSource.CLOSED) {
    setTimeout(() => {
      add_message("danger", "Connection to server lost.");
    }, 1000);
  }
};

events.addEventListener("game-info", event => {
  const info = JSON.parse(event.data);

  window.game.set_game_info(info);
});

events.addEventListener("player-info", event => {
  const info = JSON.parse(event.data);

  window.game.set_player_info(info);
});

events.addEventListener("player-enter", event => {
  const info = JSON.parse(event.data);

  window.game.set_other_player_info(info);
});

events.addEventListener("player-exit", event => {
  const info = JSON.parse(event.data);

  window.game.disable_player(info);
});

events.addEventListener("player-update", event => {
  const info = JSON.parse(event.data);

  if (info.id === window.game.state.player_info.id)
    window.game.set_player_info(info);
  else
    window.game.set_other_player_info(info);
});

events.addEventListener("game-state", event => {
  const state = JSON.parse(event.data);

  window.game.reset_game_state();

  window.game.set_prompts(state.prompts);

  window.game.set_active_prompt(state.active_prompt);

  window.game.set_players_answered(state.players_answered);

  window.game.set_players_answering(state.players_answering);

  window.game.set_answering_idle(state.answering_idle);

  window.game.set_end_answers(state.end_answers);

  window.game.set_ambiguities(state.ambiguities);

  window.game.set_players_judged(state.players_judged);

  window.game.set_players_judging(state.players_judging);

  window.game.set_judging_idle(state.judging_idle);

  window.game.set_end_judging(state.end_judging);

  window.game.set_scores(state.scores);

  window.game.set_new_game_votes(state.new_game_votes);

  window.game.state_ready();
});

events.addEventListener("prompt", event => {
  const prompt = JSON.parse(event.data);

  window.game.add_or_update_prompt(prompt);
});

events.addEventListener("start", event => {
  const prompt = JSON.parse(event.data);

  window.game.set_active_prompt(prompt);
});

events.addEventListener("player-answered", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_answered(player);
});

events.addEventListener("player-answering", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_answering(player);
});

events.addEventListener("answering-idle", event => {
  const value = JSON.parse(event.data);

  window.game.set_answering_idle(value);
});

events.addEventListener("vote-end-answers", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_vote_end_answers(player);
});

events.addEventListener("unvote-end-answers", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_unvote_end_answers(player);
});

events.addEventListener("ambiguities", event => {
  const ambiguities = JSON.parse(event.data);

  window.game.set_ambiguities(ambiguities);
});

events.addEventListener("player-judged", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_judged(player);
});

events.addEventListener("player-judging", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_judging(player);
});

events.addEventListener("judging-idle", event => {
  const value = JSON.parse(event.data);

  window.game.set_judging_idle(value);
});

events.addEventListener("vote-end-judging", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_vote_end_judging(player);
});

events.addEventListener("unvote-end-judging", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_unvote_end_judging(player);
});

events.addEventListener("scores", event => {
  const scores = JSON.parse(event.data);

  window.game.set_scores(scores);
});

events.addEventListener("vote-new-game", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_vote_new_game(player);
});

events.addEventListener("unvote-new-game", event => {
  const player = JSON.parse(event.data);

  window.game.set_player_unvote_new_game(player);
});

/*********************************************************
 * Game and supporting classes                           *
 *********************************************************/

function copy_to_clipboard(id)
{
  const tmp = document.createElement("input");
  tmp.setAttribute("value", document.getElementById(id).innerHTML);
  document.body.appendChild(tmp);
  tmp.select();
  document.execCommand("copy");
  document.body.removeChild(tmp);
}

const GameInfo = React.memo(props => {
  if (! props.id)
    return null;

  return (
    <div className="game-info">
      <span className="game-id">{props.id}</span>
      {" "}
      Share this link to invite friends:{" "}
      <span id="game-share-url">{props.url}</span>
      {" "}
      <button
        className="inline"
        onClick={() => copy_to_clipboard('game-share-url')}
      >Copy Link</button>
    </div>
  );
});

const PlayerInfo = React.memo(props => {
  if (! props.player.id)
    return null;

  const all_players = [{...props.player, active:true}, ...props.other_players];

  const sorted_players = all_players.sort((a,b) => {
    return b.score - a.score;
  });

  /* Return a new array with the separator interspersed between
   * each element of the array passed in as the argument.
   */
  function intersperse(arr, sep) {
    return arr.reduce((acc, val) => [...acc, sep, val], []).slice(1);
  }

  let names_and_scores = sorted_players.map(player => {
    if (player.score) {
      return (
        <span
          key={player.name}
          className={player.active ? "player-active" : "player-idle"}
        >
        {player.name} ({player.score})
        </span>
      );
    } else {
      if (player.active)
        return player.name;
      else
        return null;
    }
  }).filter(component => component != null);

  names_and_scores = intersperse(names_and_scores, ", ");

  return (
    <div className="player-info">
      <span className="players-header">Players: </span>
      {names_and_scores}
    </div>
  );
});

function fetch_method_json(method, api = '', data = {}) {
  const response = fetch(api, {
    method: method,
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  });
  return response;
}

function fetch_post_json(api = '', data = {}) {
  return fetch_method_json('POST', api, data);
}

async function fetch_put_json(api = '', data = {}) {
  return fetch_method_json('PUT', api, data);
}

class CategoryRequest extends React.PureComponent {
  constructor(props) {
    super(props);
    this.category = React.createRef();

    this.handle_change = this.handle_change.bind(this);
    this.handle_submit = this.handle_submit.bind(this);
  }

  handle_change(event) {
    const category_input = this.category.current;
    const category = category_input.value;

    const match = category.match(/[0-9]+/);
    if (match) {
      const num_items = parseInt(match[0], 10);
      if (num_items > 0 && num_items <= MAX_PROMPT_ITEMS)
        category_input.setCustomValidity("");
    }
  }

  async handle_submit(event) {
    const form = event.currentTarget;
    const category_input = this.category.current;
    const category = category_input.value;

    /* Prevent the default page-changing form-submission behavior. */
    event.preventDefault();

    const match = category.match(/[0-9]+/);
    if (match === null) {
      category_input.setCustomValidity("Category must include a number");
      form.reportValidity();
      return;
    }

    const num_items = parseInt(match[0], 10);

    if (num_items > MAX_PROMPT_ITEMS) {
      category_input.setCustomValidity(`Maximum number of items is ${MAX_PROMPT_ITEMS}`);
      form.reportValidity();
      return;
    }

    if (num_items < 1) {
      category_input.setCustomValidity("Category must require at least one item.");
      form.reportValidity();
      return;
    }

    const response = await fetch_post_json("prompts", {
      items: num_items,
      prompt: category
    });

    if (response.status === 200) {
      const result = await response.json();
      if (! result.valid) {
        add_message("danger", result.message);
        return;
      }
    } else {
      add_message("danger", "An error occurred submitting your category");
    }

    form.reset();
  }

  render() {
    return (
      <div className="category-request">
        <h2>Submit a Category</h2>
        <p>
          Suggest a category to play. Don't forget to include the
          number of items for each person to submit.
        </p>

        <form onSubmit={this.handle_submit} >
          <div className="form-field large">
            <input
              type="text"
              id="category"
              placeholder="6 things at the beach"
              required
              autoComplete="off"
              onChange={this.handle_change}
              ref={this.category}
            />
          </div>

          <div className="form-field large">
            <button type="submit">
              Send
            </button>
          </div>

        </form>
      </div>
    );
  }
}

const PromptOption = React.memo(props => {

  const prompt = props.prompt;

  if (prompt.votes_against.find(v => v === props.player.name))
    return false;

  return (
    <button
      className="vote-button"
      key={prompt.id}
      onClick={() => fetch_post_json(`vote/${prompt.id}`) }
    >
      <span
        className="hide-button"
        onClick={(event) => {
          event.stopPropagation();
          fetch_post_json(`vote_against/${prompt.id}`);
        }}
      >
        &times;
      </span>
      {prompt.prompt}
      <div className="vote-choices">
        {prompt.votes.map(v => {
          return (
            <div
              key={v}
              className="vote-choice"
            >
              {v}
            </div>
          );
        })}
      </div>
    </button>
  );
});

const PromptOptions = React.memo(props => {

  if (props.prompts.length === 0)
    return null;

  return (
    <div className="prompt-options">
      <h2>Vote on Categories</h2>
      <p>
        Select any categories below that you'd like to play.
        You can choose as many as you'd like.
      </p>
      {props.prompts.map(
        prompt => <PromptOption
                    key={prompt.id}
                    prompt={prompt}
                    player={props.player}
                  />
      )}
    </div>
  );
});

const LetsPlay = React.memo(props => {

  const quorum = Math.max(0, props.num_players - props.prompts.length);
  const max_votes = props.prompts.reduce(
    (max_so_far, v) => Math.max(max_so_far, v.votes.length), 0);

  if (max_votes < quorum) {
    let text = `Before we play, we should collect a bit
                more information about what category would
                be interesting for this group. So, either
                type a new category option above, or else`;
    if (props.prompts.length) {
      if (props.prompts.length > 1)
        text += " vote on some of the categories below.";
      else
        text += " vote on the category below.";
    } else {
      text += " wait for others to submit, and then vote on them below.";
    }

    return (
      <div className="before-we-play">
        <p>
          {text}
        </p>
      </div>
    );
  }

  const candidates = props.prompts.filter(p => p.votes.length >= max_votes);
  const index = Math.floor(Math.random() * candidates.length);
  const winner = candidates[index];

  return (
    <div className="lets-play">
      <h2>Let's Play</h2>
      <p>
        That should be enough voting. If you're not waiting for any
        other players to join, then let's start.
      </p>
      <button
        className="lets-play"
        onClick={() => fetch_post_json(`start/${winner.id}`) }
      >
        Start Game
      </button>
    </div>
  );
});

class Ambiguities extends React.PureComponent {

  constructor(props) {
    super(props);

    function canonize(word) {
      return word.replace(/((a|an|the) )?(.*?)s?$/i, '$3');
    }

    const word_sets = [];

    for (let word of props.words) {
      const word_canon = canonize(word);
      let found_match = false;
      for (let set of word_sets) {
        const set_canon = canonize(set.values().next().value);
        if (word_canon === set_canon) {
          set.add(word);
          found_match = true;;
          break;
        }
      }
      if (! found_match) {
        const set = new Set();
        set.add(word);
        word_sets.push(set);
      }
    }

    this.state = {
      word_sets: word_sets,
      selected: null,
      starred: null
    };

    this.submitted = false;
    this.judging_sent_recently = false;
  }

  async handle_submit() {

    /* Don't submit a second time. */
    if (this.submitted)
      return;

    const response = await fetch_post_json(
      `judged/${this.props.prompt.id}`,{
        word_groups: this.state.word_sets.map(
          set => ({
            words: Array.from(set),
            kudos: this.state.starred === set ? true : false
          }))
      }
    );

    if (response.status === 200) {
      const result = await response.json();
      if (! result.valid) {
        add_message("danger", result.message);
        return;
      }
    } else {
      add_message("danger", "An error occurred submitting the results of your judging");
      return;
    }

    this.submitted = true;
  }

  handle_click(word) {

    /* Let the server know we are doing some judging, (but rate limit
     * this so we don't send a "judging" notification more frquently
     * than necessary.
     */
    if (! this.judging_sent_recently) {
      fetch_post_json(`judging/${this.props.prompt.id}`);
      this.judging_sent_recently = true;
      setTimeout(() => { this.judging_sent_recently = false; }, 1000);
    }

    if (this.state.selected == word) {
      /* Second click on same word removes the word from the group. */
      const idx = this.state.word_sets.findIndex(s => s.has(word));
      const set = this.state.word_sets[idx];
      if (set.size === 1) {
        /* When the word is already alone, there's nothing to do but
         * to un-select it. */
        this.setState({
          selected: null
        });
        return;
      }

      const new_set = new Set([...set].filter(w => w !== word));
      this.setState({
        selected: null,
        word_sets: [...this.state.word_sets.slice(0, idx),
                    new_set,
                    new Set().add(word),
                    ...this.state.word_sets.slice(idx+1)]
      });
    } else if (this.state.selected) {
      /* Click of a second word groups the two together. */
      const idx1 = this.state.word_sets.findIndex(s => s.has(this.state.selected));
      const idx2 = this.state.word_sets.findIndex(s => s.has(word));
      const set1 = this.state.word_sets[idx1];
      const set2 = this.state.word_sets[idx2];
      const new_set = new Set([...set2, ...set1]);
      if (idx1 < idx2) {
        this.setState({
          selected: null,
          word_sets: [...this.state.word_sets.slice(0, idx1),
                      ...this.state.word_sets.slice(idx1 + 1, idx2),
                      new_set,
                      ...this.state.word_sets.slice(idx2 + 1)]
        });
      } else {
        this.setState({
          selected: null,
          word_sets: [...this.state.word_sets.slice(0, idx2),
                      new_set,
                      ...this.state.word_sets.slice(idx2 + 1, idx1),
                      ...this.state.word_sets.slice(idx1 + 1)]
        });
      }
    } else {
      /* First click of a word selects it. */
      this.setState({
        selected: word
      });
    }
  }

  render() {
    let move_on_button = null;

    if (this.props.idle) {
      move_on_button = (
        <button
          className="vote-button"
          onClick={() => fetch_post_json(`end-judging/${this.props.prompt.id}`) }
        >
          Move On Without Their Input
          <div className="vote-choices">
            {[...this.props.votes].map(v => {
              return (
                <div
                  key={v}
                  className="vote-choice"
                >
                  {v}
                  </div>
              );
            })}
          </div>
        </button>
      );
    }

    let still_waiting = null;
    const judging_players = Object.keys(this.props.players_judging);
    if (judging_players.length) {
      still_waiting = (
        <div>
          <p>
            Still waiting for the following player
            {judging_players.length > 1 ? 's' : '' }
            :
          </p>
          <ul>
            {judging_players.map(player => {
              return (
                <li
                  key={player}
                >
                  {player}{' '}
                  <span className=
                  {this.props.players_judging[player].active ?
                   "typing active"
                   :
                   "typing idle"}>
                    <span>{'.'}</span><span>{'.'}</span><span>{'.'}</span>
                  </span>
                </li>
              );
            })}
          </ul>
        </div>
      );
    }

    if (this.props.players_judged.has(this.props.player.name)) {
      return (
        <div className="please-wait">
          <h2>Submission received</h2>
          <p>
            The following players have completed judging:{' '}
            {[...this.props.players_judged].join(', ')}
          </p>
          {still_waiting}
          {move_on_button}

        </div>
      );
    }

    const btn_class = "ambiguity-button";
    const btn_selected_class = btn_class + " selected";

    return (
      <div className="ambiguities">
        <h2>Judging Answers</h2>
        <p>
          Click/tap on each pair of answers that should be scored as equivalent,
          (or click a word twice to split it out from a group). Remember,
          what goes around comes around, so it's best to be generous when
          judging.
        </p>
        <p>
          Also, for an especially fun or witty answer, you can give kudos
          by clicking the star on the right. You may only do this for one
          word/group.
        </p>
        <h2>{this.props.prompt.prompt}</h2>
        {this.state.word_sets.map(set => {
          return (
            <div
              className="ambiguity-group"
              key={Array.from(set)[0]}
            >
              {Array.from(set).map(word => {
                return (
                  <button
                    className={this.state.selected === word ?
                               btn_selected_class : btn_class }
                    key={word}
                    onClick={() => this.handle_click(word)}
                  >
                    {word}
                  </button>
                );
              })}
              <span
                className={this.state.starred === set ?
                           "star-button selected" : "star-button"
                          }
                onClick={(event) => {
                  event.stopPropagation();
                  if (this.state.starred === set) {
                    this.setState({
                      starred: null
                    });
                  } else {
                    this.setState({
                      starred: set
                    });
                  }
                }}
              >
              {this.state.starred === set ?
               '★' : '☆'
              }
              </span>
            </div>
          );
        })}
        <p>
          Click here when done judging:<br/>
          <button
            onClick={() => this.handle_submit()}
          >
            Send
          </button>
        </p>
      </div>
    );
  }
}

class ActivePrompt extends React.PureComponent {

  constructor(props) {
    super(props);
    const items = props.prompt.items;

    this.submitted = false;

    this.answers = [...Array(items)].map(() => React.createRef());
    this.answering_sent_recently = false;

    this.handle_submit = this.handle_submit.bind(this);
    this.handle_change = this.handle_change.bind(this);
  }

  handle_change(event) {
    /* We don't care (or even look) at what the player is typing at
     * this point. We simply want to be informed that the player _is_
     * typing so that we can tell the server (which will tell other
     * players) that there is activity here.
     */

    /* Rate limit so that we don't send an "answering" notification
     * more frequently than necessary.
     */
    if (! this.answering_sent_recently) {
      fetch_post_json(`answering/${this.props.prompt.id}`);
      this.answering_sent_recently = true;
      setTimeout(() => { this.answering_sent_recently = false; }, 1000);
    }
  }

  async handle_submit(event) {
    const form = event.currentTarget;

    /* Prevent the default page-changing form-submission behavior. */
    event.preventDefault();

    /* And don't submit a second time. */
    if (this.submitted)
      return;

    const response = await fetch_post_json(`answer/${this.props.prompt.id}`, {
      answers: this.answers.map(r => r.current.value)
    });
    if (response.status === 200) {
      const result = await response.json();
      if (! result.valid) {
        add_message("danger", result.message);
        return;
      }
    } else {
      add_message("danger", "An error occurred submitting your answers");
      return;
    }

    /* Everything worked. Server is happy with our answers. */
    form.reset();
    this.submitted = true;
  }

  render() {

    let still_waiting = null;
    const answering_players = Object.keys(this.props.players_answering);;
    if (answering_players.length) {
      still_waiting = (
        <div>
          <p>
            Still waiting for the following player
            {answering_players.length > 1 ? 's' : ''}
            :
          </p>
          <ul>
            {answering_players.map(player => {
              return (
                <li
                  key={player}
                >
                  {player}{' '}
                  <span className=
                  {this.props.players_answering[player].active ?
                   "typing active"
                   :
                   "typing idle"}>
                    <span>{'.'}</span><span>{'.'}</span><span>{'.'}</span>
                  </span>
                </li>
              );
            })}
          </ul>
        </div>
      );
    }

    let move_on_button = null;
    if (this.props.idle) {
      move_on_button =(
        <button
          className="vote-button"
          onClick={() => fetch_post_json(`end-answers/${this.props.prompt.id}`) }
        >
          {answering_players.length ?
           "Move On Without Their Answers" :
           "Move On Without Anyone Else"}
          <div className="vote-choices">
            {[...this.props.votes].map(v => {
              return (
                <div
                  key={v}
                  className="vote-choice"
                >
                  {v}
                </div>
              );
            })}
          </div>
        </button>
      );
    }

    if (this.props.players_answered.has(this.props.player.name)) {
      return (
        <div className="please-wait">
          <h2>Submission received</h2>
          <p>
            The following players have submitted their answers:{' '}
            {[...this.props.players_answered].join(', ')}
          </p>
          {still_waiting}
          {move_on_button}

        </div>
      );
    }

    return (
      <div className="active-prompt">
        <h2>The Game of Empathy</h2>
        <p>
          Remember, you're trying to match your answers with
          what the other players submit.
          Give {this.props.prompt.items} answer
          {this.props.prompt.items > 1 ? 's' : ''} for the following prompt:
        </p>
        <h2>{this.props.prompt.prompt}</h2>
        <form onSubmit={this.handle_submit}>
          {[...Array(this.props.prompt.items)].map((whocares,i) => {
            return (
              <div
                key={i}
                className="form-field large">
                <input
                  type="text"
                  name={`answer_${i}`}
                  required
                  autoComplete="off"
                  onChange={this.handle_change}
                  ref={this.answers[i]}
                />
              </div>
            );
          })}

          <div
            key="submit-button"
            className="form-field large">
            <button type="submit">
              Send
            </button>
          </div>

        </form>
      </div>
    );
  }
}

class Game extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      game_info: {},
      player_info: {},
      other_players: [],
      prompts: [],
      active_prompt: null,
      players_answered: new Set(),
      players_answering: {},
      answering_idle: false,
      end_answers_votes: new Set(),
      ambiguities: null,
      players_judged: new Set(),
      players_judging: {},
      judging_idle: false,
      end_judging_votes: new Set(),
      scores: null,
      new_game_votes: new Set(),
      ready: false
    };
  }

  set_game_info(info) {
    this.setState({
      game_info: info
    });
  }

  set_player_info(info) {
    this.setState({
      player_info: info
    });
  }

  set_other_player_info(info) {
    const other_players_copy = [...this.state.other_players];
    const idx = other_players_copy.findIndex(o => o.id === info.id);
    if (idx >= 0) {
      other_players_copy[idx] = info;
    } else {
      other_players_copy.push(info);
    }
    this.setState({
      other_players: other_players_copy
    });
  }

  disable_player(info) {
    const idx = this.state.other_players.findIndex(o => o.id === info.id);
    if (idx < 0)
      return;

    const other_players_copy = [...this.state.other_players];
    other_players_copy[idx].active = false;

    this.setState({
      other_players: other_players_copy
    });
  }

  reset_game_state() {
    this.setState({
      prompts: [],
      active_prompt: null,
      players_answered: new Set(),
      players_answering: {},
      answering_idle: false,
      end_answers_votes: new Set(),
      ambiguities: null,
      players_judged: new Set(),
      players_judging: {},
      judging_idle: false,
      end_judging_votes: new Set(),
      scores: null,
      new_game_votes: new Set(),
      ready: false
    });
  }

  set_prompts(prompts) {
    this.setState({
      prompts: prompts
    });
  }

  add_or_update_prompt(prompt) {
    const prompts_copy = [...this.state.prompts];
    const idx = prompts_copy.findIndex(p => p.id === prompt.id);
    if (idx >= 0) {
      prompts_copy[idx] = prompt;
    } else {
      prompts_copy.push(prompt);
    }
    this.setState({
      prompts: prompts_copy
    });
  }

  set_active_prompt(prompt) {
    this.setState({
      active_prompt: prompt
    });
  }

  set_players_answered(players) {
    this.setState({
      players_answered: new Set(players)
    });
  }

  set_player_answered(player) {
    const new_players_answering = {...this.state.players_answering};
    delete new_players_answering[player];

    this.setState({
      players_answered: new Set([...this.state.players_answered, player]),
      players_answering: new_players_answering
    });
  }

  set_players_answering(players) {
    const players_answering = {};
    for (let player of players) {
      players_answering[player] = {active: false};
    }
    this.setState({
      players_answering: players_answering
    });
  }

  set_player_answering(player) {
    /* Set the player as actively answering now. */
    this.setState({
      players_answering: {
        ...this.state.players_answering,
        [player]: {active: true}
      }
    });
    /* And arrange to have them marked idle very shortly.
     *
     * Note: This timeout is intentionally very, very short. We only
     * need it long enough that the browser has latched onto the state
     * change to "active" above. We actually use a CSS transition
     * delay to control the user-perceptible length of time after
     * which an active player appears inactive.
     */
    setTimeout(() => {
      this.setState({
        players_answering: {
          ...this.state.players_answering,
          [player]: {active: false}
        }
      });
    }, 100);
  }

  set_answering_idle(value) {
    this.setState({
      answering_idle: value
    });
  }

  set_end_answers(players) {
    this.setState({
      end_answers_votes: new Set(players)
    });
  }

  set_player_vote_end_answers(player) {
    this.setState({
      end_answers_votes: new Set([...this.state.end_answers_votes, player])
    });
  }

  set_player_unvote_end_answers(player) {
    this.setState({
      end_answers_votes: new Set([...this.state.end_answers_votes].filter(p => p !== player))
    });
  }

  set_ambiguities(ambiguities) {
    this.setState({
      ambiguities: ambiguities
    });
  }

  set_players_judged(players) {
    this.setState({
      players_judged: new Set(players)
    });
  }

  set_player_judged(player) {
    const new_players_judging = {...this.state.players_judging};
    delete new_players_judging[player];

    this.setState({
      players_judged: new Set([...this.state.players_judged, player]),
      players_judging: new_players_judging
    });
  }

  set_players_judging(players) {
    const players_judging = {};
    for (let player of players) {
      players_judging[player] = {active: false};
    }
    this.setState({
      players_judging: players_judging
    });
  }

  set_player_judging(player) {
    /* Set the player as actively judging now. */
    this.setState({
      players_judging: {
        ...this.state.players_judging,
        [player]: {active: true}
      }
    });
    /* And arrange to have them marked idle very shortly.
     *
     * Note: This timeout is intentionally very, very short. We only
     * need it long enough that the browser has latched onto the state
     * change to "active" above. We actually use a CSS transition
     * delay to control the user-perceptible length of time after
     * which an active player appears inactive.
     */
    setTimeout(() => {
      this.setState({
        players_judging: {
          ...this.state.players_judging,
          [player]: {active: false}
        }
      });
    }, 100);

  }

  set_judging_idle(value) {
    this.setState({
      judging_idle: value
    });
  }

  set_end_judging(players) {
    this.setState({
      end_judging_votes: new Set(players)
    });
  }

  set_player_vote_end_judging(player) {
    this.setState({
      end_judging_votes: new Set([...this.state.end_judging_votes, player])
    });
  }

  set_player_unvote_end_judging(player) {
    this.setState({
      end_judging_votes: new Set([...this.state.end_judging_votes].filter(p => p !== player))
    });
  }

  set_scores(scores) {
    this.setState({
      scores: scores
    });
  }

  set_new_game_votes(players) {
    this.setState({
      new_game_votes: new Set(players)
    });
  }

  set_player_vote_new_game(player) {
    this.setState({
      new_game_votes: new Set([...this.state.new_game_votes, player])
    });
  }

  set_player_unvote_new_game(player) {
    this.setState({
      new_game_votes: new Set([...this.state.new_game_votes].filter(p => p !== player))
    });
  }

  state_ready() {
    this.setState({
      ready: true
    });
  }

  render() {
    const state = this.state;

    if (state.scores) {

      const players_total = state.players_answered.size;

      let perfect_score = 0;
      for (let i = 0;
           i < state.active_prompt.items &&
           i < state.scores.words.length;
           i++)
      {
        perfect_score += state.scores.words[i].players.length;
      }

      return (
        <div className="scores">
          <h2>{state.active_prompt.prompt}</h2>
          <h2>Scores</h2>
          <ul>
            {state.scores.scores.map(score => {
              let perfect = null;
              if (score.score === perfect_score) {
                perfect = <span className="achievement">Perfect!</span>;
              }
              let quirkster = null;
              if (score.score === state.active_prompt.items) {
                quirkster = <span className="achievement">Quirkster!</span>;
              }
              let kudos_slam = null;
              if (score.kudos > 0 && score.kudos >= players_total - 1) {
                kudos_slam = <span className="achievement">Kudos Slam!</span>;
              }
              return (
                <li key={score.players[0]}>
                  {score.players.join("/")}: {score.score}
                  {score.kudos ? `, ${'★'.repeat(score.kudos)}` : ""}
                  {' '}{perfect} {quirkster} {kudos_slam}
                </li>
              );
            })}
          </ul>
          <h2>Words submitted</h2>
          <ul>
            {state.scores.words.map(word => {
              let great_minds = null;
              if (word.kudos.length && word.players.length > 1) {
                great_minds = <span className="achievement">Great Minds!</span>;
              }
              let kudos_slam = null;
              if (word.kudos.length > 0 && word.kudos.length >= players_total - 1) {
                kudos_slam = <span className="achievement">Kudos Slam!</span>;
              }
              return (
                <li key={word.word}>
                  {word.word} ({word.players.length}
                  {word.kudos.length ? `, ${'★'.repeat(word.kudos.length)}` : ""}
                  ): {word.players.join(', ')}
                  {' '}{great_minds}{kudos_slam}
                </li>
              );
            })}
          </ul>
          <button
            className="vote-button"
            onClick={() => fetch_post_json(`new-game/${state.active_prompt.id}`) }
          >
            New Game
            <div className="vote-choices">
              {[...state.new_game_votes].map(v => {
                return (
                  <div
                    key={v}
                    className="vote-choice"
                  >
                    {v}
                  </div>
                );
              })}
            </div>
          </button>
        </div>
      );
    }

    if (state.ambiguities){
      return <Ambiguities
               prompt={state.active_prompt}
               words={state.ambiguities}
               player={state.player_info}
               players_judged={state.players_judged}
               players_judging={state.players_judging}
               idle={state.judging_idle}
               votes={state.end_judging_votes}
             />;
    }

    if (state.active_prompt) {
      return <ActivePrompt
               prompt={state.active_prompt}
               player={state.player_info}
               players_answered={state.players_answered}
               players_answering={state.players_answering}
               idle={state.answering_idle}
               votes={state.end_answers_votes}
             />;
    }

    if (! state.ready)
      return null;

    return [
      <GameInfo
        key="game-info"
        id={state.game_info.id}
        url={state.game_info.url}
      />,
      <PlayerInfo
        key="player-info"
        game={this}
        player={state.player_info}
        other_players={state.other_players}
      />,
      <p key="spacer"></p>,
      <CategoryRequest
        key="category-request"
      />,
      <LetsPlay
        key="lets-play"
        num_players={1+state.other_players.filter(p => p.active).length}
        prompts={state.prompts}
      />,
      <PromptOptions
        key="prompts"
        prompts={state.prompts}
        player={state.player_info}
      />
    ];
  }
}

ReactDOM.render(<Game
                  ref={(me) => window.game = me}
                />, document.getElementById("empathy"));
