// Path: mobile/src/main/java/io/ashdavies/eternity/domain/Message.java
// @AutoValue
// @FirebaseValue
// public abstract class Message {
// 
//   public abstract String uuid();
//   public abstract String text();
//   public abstract Author author();
//   public abstract long created();
// 
//   @Nullable
//   public abstract Message original();
// 
//   public Object toFirebaseValue() {
//     return new AutoValue_Message.FirebaseValue(this);
//   }
// 
//   private static Builder builder() {
//     return new AutoValue_Message.Builder()
//         .uuid(UUID.randomUUID().toString())
//         .created(System.currentTimeMillis());
//   }
// 
//   public static Builder from(String text) {
//     return builder().text(text);
//   }
// 
//   public static Builder from(Message message) {
//     return from(message.text()).original(message);
//   }
// 
//   public static Message create(DataSnapshot snapshot) {
//     return snapshot.getValue(AutoValue_Message.FirebaseValue.class).toAutoValue();
//   }
// 
//   @AutoValue.Builder
//   public abstract static class Builder {
// 
//     public abstract Builder uuid(String uuid);
//     public abstract Builder text(String text);
//     public abstract Builder author(Author author);
//     public abstract Builder created(long created);
// 
//     public abstract Builder original(@Nullable Message message);
// 
//     public abstract Message build();
//   }
// }


import io.ashdavies.commons.util.StringUtils;
import io.ashdavies.eternity.domain.Message;
import io.ashdavies.rx.repository.Repository;
import io.ashdavies.rx.rxfirebase.ChildEvent;
import io.ashdavies.rx.rxfirebase.RxFirebaseDatabase;
import io.reactivex.Completable;
import io.reactivex.Flowable;
import javax.inject.Inject;
package io.ashdavies.eternity.chat;


class MessageRepository implements Repository<Message, String> {

  private static final String CHILD_MESSAGES = "messages";
  private static final String CHILD_DIVIDER = "/";

  private static final String QUERY_ORDER = "created";

  private static final int QUERY_LIMIT = 100;

  @Inject
  MessageRepository() {
  }

  @Override
  public Flowable<Message> get(String id) {
    return getInstance(CHILD_MESSAGES, id)
        .onSingleValueEvent()
        .map(Message::create)
        .toFlowable();
  }

  @Override
  public Flowable<Message> getAll() {
    return RxFirebaseDatabase.getInstance(CHILD_MESSAGES)
        .limitToLast(QUERY_LIMIT)
        .orderByChild(QUERY_ORDER)
        .onChildEvent(ChildEvent.Type.CHILD_ADDED)
        .map(event -> Message.create(event.snapshot()));
  }

  @Override
// Path: mobile/src/main/java/io/ashdavies/eternity/domain/Message.java
// @AutoValue
// @FirebaseValue
// public abstract class Message {
// 
//   public abstract String uuid();
//   public abstract String text();
//   public abstract Author author();
//   public abstract long created();
// 
//   @Nullable
//   public abstract Message original();
// 
//   public Object toFirebaseValue() {
//     return new AutoValue_Message.FirebaseValue(this);
//   }
// 
//   private static Builder builder() {
//     return new AutoValue_Message.Builder()
//         .uuid(UUID.randomUUID().toString())
//         .created(System.currentTimeMillis());
//   }
// 
//   public static Builder from(String text) {
//     return builder().text(text);
//   }
// 
//   public static Builder from(Message message) {
//     return from(message.text()).original(message);
//   }
// 
//   public static Message create(DataSnapshot snapshot) {
//     return snapshot.getValue(AutoValue_Message.FirebaseValue.class).toAutoValue();
//   }
// 
//   @AutoValue.Builder
//   public abstract static class Builder {
// 
//     public abstract Builder uuid(String uuid);
//     public abstract Builder text(String text);
//     public abstract Builder author(Author author);
//     public abstract Builder created(long created);
// 
//     public abstract Builder original(@Nullable Message message);
// 
//     public abstract Message build();
//   }
// }


// Path: mobile/src/main/java/io/ashdavies/eternity/chat/MessageRepository.java
import io.ashdavies.commons.util.StringUtils;
import io.ashdavies.eternity.domain.Message;
import io.ashdavies.rx.repository.Repository;
import io.ashdavies.rx.rxfirebase.ChildEvent;
import io.ashdavies.rx.rxfirebase.RxFirebaseDatabase;
import io.reactivex.Completable;
import io.reactivex.Flowable;
import javax.inject.Inject;

package io.ashdavies.eternity.chat;


class MessageRepository implements Repository<Message, String> {

  private static final String CHILD_MESSAGES = "messages";
  private static final String CHILD_DIVIDER = "/";

  private static final String QUERY_ORDER = "created";

  private static final int QUERY_LIMIT = 100;

  @Inject
  MessageRepository() {
  }

  @Override
  public Flowable<Message> get(String id) {
    return getInstance(CHILD_MESSAGES, id)
        .onSingleValueEvent()
        .map(Message::create)
        .toFlowable();
  }

  @Override
  public Flowable<Message> getAll() {
    return RxFirebaseDatabase.getInstance(CHILD_MESSAGES)
        .limitToLast(QUERY_LIMIT)
        .orderByChild(QUERY_ORDER)
        .onChildEvent(ChildEvent.Type.CHILD_ADDED)
        .map(event -> Message.create(event.snapshot()));
  }

  @Override
  public Completable put(Message message, Resolver<Message, String> resolver) {