//---------------------------------------------------------------------------
///In the 'Chat' phase, any Participant can send a chat message that
///will be broadcasted to all group members
void ribi::gtst::ServerStateChat::NotifyChatMessage(
  const boost::shared_ptr<const Participant>& participant,
  const boost::shared_ptr<ChatMessage>& message)
{
  assert(GetServer());

  LogFile * const logfile = GetServer()->GetLog();
  assert(logfile);

  //Append the chat message to the Participant his/her own log
  FindParticipant(participant)->AppendChat(message);

  //Logging this event
  logfile->LogChatMessage(participant,message);

  //Broadcasting chat texts for participants of the same group
  //including the sender itself
  {
    const Group * const group = GetServer()->GetGroups()->FindMyGroup(participant);
    const auto ps = group->CollectParticipants();

    std::for_each(ps.begin(),ps.end(),
      [this,message](const boost::shared_ptr<const Participant>& p)
      {
        this->m_chat_messages[p].push_back(message);
      }
    );
  }

  WtServerPusher::GetInstance()->Post();
}
//---------------------------------------------------------------------------
///ParticipantDialog notifies the Server of the
///Participant his/her vote
void ServerStateVoting::NotifyVote(
  const boost::shared_ptr<const Participant>&  participant,
  const boost::shared_ptr<VotingOption>& vote)
{
  assert(vote);
  assert(participant);
  FindParticipant(participant)->Vote(vote);

  m_has_voted[participant] = true;

  //Log the vote
  m_server->GetLog()->LogVote(participant);
  //WtBroadcastServer::GetInstance()->Post();
}
//---------------------------------------------------------------------------
void ServerStateVoting::Start()
{
  ///Keeps track of Participants having voted
  m_has_voted.clear();
  assert(m_has_voted.empty());

  BOOST_FOREACH(const boost::shared_ptr<const Participant>& p,
    GetServer()->GetGroups()->CollectParticipants(false,false,true,false))
  {
    assert(p);
    m_has_voted[p] = false;
    StateVoting * const state = dynamic_cast<const StateVoting*>(this);
    assert(state);
    FindParticipant(p)->SetState(state);
  }
  assert(!CanGoToNextState());
}
//---------------------------------------------------------------------------
void ribi::gtst::ServerStateChooseAction::Start()
{
  ///Keeps track of Participants having voted
  m_has_chosen_action.clear();
  assert(m_has_chosen_action.empty());

  BOOST_FOREACH(
    const boost::shared_ptr<const Participant>& p,
    GetServer()->GetGroups()->CollectParticipants(false,false,true,false))
  {
    assert(!GetServer()->GetGroups()->CollectParticipants(false,false,true,false).empty());
    assert(p);
    m_has_chosen_action.insert(std::make_pair(p,false));
    StateChooseAction * const state = dynamic_cast<const StateChooseAction*>(this);
    assert(state);
    FindParticipant(p)->SetState(state);
  }
  assert(!CanGoToNextState());
}