package com.nbusy.app.data;

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

import titan.client.messages.MsgMessage;

/**
 * Converts objects used at different components to each other.
 * i.e. A message retrieved from NBusy server into a message format to be displayed in an activity.
 *
 * Terminology:
 * - Titan: Objects used by the Titan Java client.
 * - NBusy: Objects used in (NBusy) app's /worker and /activities namespaces.
 * - DB: Objects stored in app's database, which differ slightly from those used in activities for storage optimization.
 */
public class DataMap {

    public static MsgMessage[] nbusyToTitanMessages(com.nbusy.app.data.Message... msgs) {
        MsgMessage[] titanMsgs = new MsgMessage[msgs.length];

        for (int i = 0; i < msgs.length; i++) {
            titanMsgs[i] = new MsgMessage(msgs[i].chatId, null, msgs[i].to, msgs[i].sent, msgs[i].body);
        }

        return titanMsgs;
    }

    public static Message[] titanToNBusyMessages(MsgMessage... msgs) {
        Message[] nbusyMsgs = new Message[msgs.length];

        for (int i = 0; i < msgs.length; i++) {
            nbusyMsgs[i] = Message.newIncomingMessage(msgs[i].chatId, msgs[i].from, msgs[i].message, msgs[i].time);
        }

        return nbusyMsgs;
    }

    public static List<Message> dbToNBusyMessages(UserProfile profile, List<Message> msgs) {
        List<Message> convertedMsgs = new ArrayList<>();
        for (Message msg : msgs) {
            if (msg.owner) {
//                msg.from = profile.name;
            }
        }

        return msgs;
    }
}

@@ -1386,17 +1386,10 @@
 ge%3E 
-converted
+nb
 Msgs
@@ -1410,16 +1410,80 @@
 st%3C%3E();%0A
+        Chat chat = profile.getChat(msgs.get(0).chatId).get();%0A%0A
         
@@ -1542,10 +1542,8 @@
 ) %7B%0A
-//
     
@@ -1558,31 +1558,268 @@
     
-msg.from = profile.name
+nbMsgs.add(new Message(msg.id, msg.chatId, profile.name, chat.peerName, true, msg.body, msg.sent, msg.status));%0A            %7D else %7B%0A                nbMsgs.add(new Message(msg.id, msg.chatId, chat.peerName, profile.name, false, msg.body, msg.sent, msg.status))
 ;%0A  
@@ -1856,17 +1856,19 @@
  return 
-m
+nbM
 sgs;%0A   

add dbToNBusyMessages