package org.waveprotocol.box.webclient.client.extended.ui;


import org.waveprotocol.wave.client.extended.WaveContentChat;
import org.waveprotocol.wave.model.extended.type.chat.ChatMessage;
import org.waveprotocol.wave.model.extended.type.chat.ChatPresenceStatus;
import org.waveprotocol.wave.model.extended.type.chat.ObservableChat;
import org.waveprotocol.wave.model.wave.InvalidParticipantAddress;
import org.waveprotocol.wave.model.wave.ParticipantId;

import java.util.Set;


public class SimpleChatPresenter implements ObservableChat.Listener, SimpleChatView.Listener {


  private final SimpleChatView view;
  private final ParticipantId participant;
  private WaveContentChat waveContent;

  public static SimpleChatPresenter create(SimpleChatView view, ParticipantId participant) {
    SimpleChatPresenter listPresenter = new SimpleChatPresenter(view, participant);
    listPresenter.init();
    return listPresenter;
  }

  protected SimpleChatPresenter(SimpleChatView view, ParticipantId participant) {
    this.view = view;
    this.participant = participant;
  }


  protected void init() {
    this.view.setListener(this);
  }

  public void bind(WaveContentChat chatContent) {
    this.waveContent = chatContent;
    this.waveContent.getChat().addListener(this);

    Set<ParticipantId> participants = this.waveContent.getParticipants();

    // Show participants
    this.view.setParticipants(participants);

    // Update participant status
    for (ParticipantId participant : participants)
      this.view.setParticipantStatus(participant,
          this.waveContent.getChat().getParticipantStatus(participant));


    // Show some previous messages
    int totalMessages = waveContent.getChat().numMessages();
    int numShowMessages = totalMessages > 10 ? 10 : totalMessages;
    /*
     * for (int i = totalMessages - numShowMessages; i < totalMessages; i++)
     * this.view.addChatLine(waveContent.getChat().getMessage(i));
     */

    // Show all messages
    for (int i = 0; i < totalMessages; i++)
      this.view.addChatLine(waveContent.getChat().getMessage(i));

  }

  //
  // Listen to the View
  //

  @Override
  public void onNewMessage(String msg) {
    waveContent.getChat().addMessage(new ChatMessage("", msg, System.currentTimeMillis(), participant));
  }


  @Override
  public void onAddParticipant(String address) {

    ParticipantId newParticipantId = null;

    try {
      newParticipantId = ParticipantId.of(address);
      waveContent.addParticipant(newParticipantId);
      waveContent.getChat().addMessage(
          new ChatMessage("", address + " has joined to this conversation.", System
              .currentTimeMillis(), participant));
    } catch (InvalidParticipantAddress e) {
      // TODO - bad participant address
    }
  }

  //
  // Chat's remote events Listener
  //

  @Override
  public void onMessageAdded(ChatMessage message) {
    this.view.addChatLine(message);
  }

  @Override
  public void onParticipantAdded(ParticipantId participant) {
    this.view.setParticipants(waveContent.getParticipants());
  }

  @Override
  public void onParticipantRemoved(ParticipantId participant) {
    this.view.setParticipants(waveContent.getParticipants());
  }

  @Override
  public void startWriting() {
    this.waveContent.getChat().setParticipantStatus(participant,
        ChatPresenceStatus.createWritingStatus());
  }

  @Override
  public void stopWriting() {
    this.waveContent.getChat().setParticipantStatus(participant,
        ChatPresenceStatus.createOnlineStatus());
  }

  @Override
  public void onParticipantStatusChanged(ParticipantId participant, ChatPresenceStatus status) {
    this.view.setParticipantStatus(participant, status);
  }




}

@@ -3253,32 +3253,42 @@
 tWriting() %7B%0A   
+ /*%0A     *
  this.waveConten
@@ -3330,34 +3330,33 @@
 rticipant,%0A     
-  
+*
  ChatPresenceSta
@@ -3375,32 +3375,40 @@
 itingStatus());%0A
+     */%0A
   %7D%0A%0A  @Override
@@ -3437,24 +3437,34 @@
 ting() %7B%0A   
+ /*%0A     *
  this.waveCo
@@ -3518,18 +3518,17 @@
 t,%0A     
-  
+*
  ChatPre
@@ -3558,24 +3558,32 @@
 eStatus());%0A
+     */%0A
   %7D%0A%0A  @Over

Disable chat presence example