package codeu.chat.database;

import codeu.chat.common.Conversation;
import codeu.chat.database.model.ConversationModel;
import codeu.chat.database.model.UserModel;
import com.google.firebase.database.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by greg on 6/1/17.
 */
public class DatabaseStartup {
    private final String USERS_CHILD = "users";
    private final String MESSAGES_CHILD = "messages";
    private final String CONVERSATIONS_CHILD = "conversations";

    // FireBase database reference for class
    private static DatabaseReference database = FirebaseDatabase.getInstance().getReference();

    // change to returnable list
    public Map<String, UserModel> getUsers() {
        Map<String, UserModel> usersMap = new HashMap<>();
        DatabaseReference usersRef = database.child(USERS_CHILD);
        usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Iterator<DataSnapshot> usersIterator = dataSnapshot.getChildren().iterator();
                while (usersIterator.hasNext()) {
                    UserModel user = usersIterator.next().getValue(UserModel.class);
                    String key = user.id;
                    System.out.println("ID: " + user.id);
                    System.out.println("Name: " + user.name);
                    System.out.println("Creation: " + user.creation);
                    usersMap.put(key, user);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getCode());
            }
        });
        return usersMap;
    }
    // change to returnable list
    public Map<String, ConversationModel> getConversations() {
        Map<String, ConversationModel> convosMap = new HashMap<>();
        DatabaseReference convosRef = database.child(CONVERSATIONS_CHILD);
        convosRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Iterator<DataSnapshot> convosIterator = dataSnapshot.getChildren().iterator();
                while (convosIterator.hasNext()) {
                    ConversationModel convo = convosIterator.next().getValue(ConversationModel.class);
                    String key = convo.id;
                    System.out.println("ID: " + convo.id);
                    System.out.println("Title: " + convo.title);
                    System.out.println("Owner: " + convo.owner);
                    System.out.println("Creation: " + convo.creation);
                    convosMap.put(key, convo);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getCode());
            }
        });
        return convosMap;
    }
}

@@ -27,47 +27,8 @@
 e;%0A%0A
-import codeu.chat.common.Conversation;%0A
 impo
@@ -71,24 +71,24 @@
 ationModel;%0A
+
 import codeu
@@ -239,46 +239,8 @@
 p;%0A%0A
-/**%0A * Created by greg on 6/1/17.%0A */%0A
 publ
@@ -1217,198 +1217,8 @@
 id;%0A
-                    System.out.println(%22ID: %22 + user.id);%0A                    System.out.println(%22Name: %22 + user.name);%0A                    System.out.println(%22Creation: %22 + user.creation);%0A
     
@@ -2164,32 +2164,32 @@
 onModel.class);%0A
+
                 
@@ -2219,268 +2219,8 @@
 id;%0A
-                    System.out.println(%22ID: %22 + convo.id);%0A                    System.out.println(%22Title: %22 + convo.title);%0A                    System.out.println(%22Owner: %22 + convo.owner);%0A                    System.out.println(%22Creation: %22 + convo.creation);%0A
     

Fix relative path for service account