package com.nbusy.app.data;

import com.google.common.collect.ImmutableSet;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * One-to-one chat.
 */
public final class Chat {
    public final String id; // unique chat id
    public final String peerName; // peer name
    public final String lastMessage; // last message in conversation
    public final Date lastMessageSent; // last message sent time
    public final ImmutableSet<Message> messages; // list of messages in this chat

    public Chat(String id, String peerName, String lastMessage, Date lastMessageSent, ImmutableSet<Message> messages) {
        if (id == null || id.isEmpty()) {
            throw new IllegalArgumentException("id cannot be null or empty");
        }
        if (peerName == null || peerName.isEmpty()) {
            throw new IllegalArgumentException("peerName cannot be null or empty");
        }
        if (lastMessage == null || lastMessage.isEmpty()) {
            throw new IllegalArgumentException("lastMessage cannot be null or empty");
        }
        if (lastMessageSent == null) {
            throw new IllegalArgumentException("lastMessageSent cannot be null");
        }
        if (messages == null) {
            throw new IllegalArgumentException("messages cannot be null");
        }

        this.id = id;
        this.peerName = peerName;
        this.lastMessage = lastMessage;
        this.lastMessageSent = lastMessageSent;
        this.messages = messages;
    }

    public Chat(String id, String peerName, String lastMessage, Date lastMessageSent) {
        this(id, peerName, lastMessage, lastMessageSent, ImmutableSet.<Message>of());
    }

    public synchronized ImmutableSet<Message> addNewOutgoingMessage(String... messages) {
        if (messages == null || messages.length == 0) {
            throw new IllegalArgumentException("messages cannot be null or empty");
        }

        List<Message> msgs = new ArrayList<>();
        for (String message : messages) {
            msgs.add(Message.newOutgoingMessage(id, peerName, message));
        }

        return ImmutableSet.copyOf(msgs);
    }

//    public synchronized void addMessages(List<Message> msgs) {
//        if (msgs == null || msgs.size() == 0) {
//            throw new IllegalArgumentException("message list cannot be null or empty");
//        }
//
//        for (Message msg : msgs) {
//            // don't re-add duplicates
//            if (getMessageLocation(msg) != 0) {
//                continue;
//            }
//
//            messageIDtoIndex.put(msg.id, messages.size());
//            messages.add(msg);
//        }
//    }
//
//    public synchronized int updateMessage(Message msg) {
//        if (msg == null) {
//            throw new IllegalArgumentException("message cannot be null");
//        }
//
//        // only update if message belongs to this chat
//        int index = getMessageLocation(msg);
//        if (index == 0) {
//            return 0;
//        }
//
//        messages.set(index, msg);
//        return index;
//    }
//
//    public synchronized int getMessageLocation(Message msg) {
//        if (msg == null) {
//            throw new IllegalArgumentException("message cannot be null");
//        }
//
//        if (!Objects.equals(msg.chatId, id)) {
//            return 0;
//        }
//
//        if (messageIDtoIndex.containsKey(msg.id)) {
//            return messageIDtoIndex.get(msg.id);
//        } else {
//            return 0;
//        }
//    }
}

@@ -138,19 +138,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -1751,37 +1751,34 @@
 ronized 
-ImmutableSet%3C
+ChatAndNew
 Message
-%3E
+s
  addNewO
@@ -1973,19 +1973,18 @@
         
-Lis
+Se
 t%3CMessag
@@ -1997,24 +1997,22 @@
 s = new 
-ArrayLis
+HashSe
 t%3C%3E();%0A 
@@ -2150,16 +2150,58 @@
   return
+ new ChatAndNewMessages(addMessages(msgs),
  Immutab
@@ -2218,16 +2218,17 @@
 Of(msgs)
+)
 ;%0A    %7D%0A
@@ -2232,38 +2232,614 @@
  %7D%0A%0A
-//    public synchronized void
+    public final class ChatAndNewMessages %7B%0A        public final Chat chat;%0A        public final ImmutableSet%3CMessage%3E messages;%0A%0A        public ChatAndNewMessages(Chat chat, ImmutableSet%3CMessage%3E messages) %7B%0A            if (chat == null) %7B%0A                throw new IllegalArgumentException(%22chat cannot be null%22);%0A            %7D%0A            if (messages == null %7C%7C messages.size() == 0) %7B%0A                throw new IllegalArgumentException(%22messages cannot be null or empty%22);%0A            %7D%0A%0A            this.chat = chat;%0A            this.messages = messages;%0A        %7D%0A    %7D%0A%0A    public synchronized Chat
  add
@@ -2847,19 +2847,18 @@
 essages(
-Lis
+Se
 t%3CMessag
@@ -3076,16 +3076,22 @@
       //
+ todo:
  don't r
@@ -3107,16 +3107,77 @@
 licates%0A
+        // todo: only add messages that belongs to this chat%0A
 //      
@@ -3371,26 +3371,43 @@
       %7D%0A
-//
+        return this;%0A
     %7D%0A
-//
 %0A//    p

add various todos