package sk.vander.contacts.data.provider;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoExpose;
import rx.Observable;
import rx.schedulers.Schedulers;
import rx.subjects.BehaviorSubject;
import sk.vander.contacts.App;
import sk.vander.contacts.base.annotation.ApplicationScope;
import sk.vander.contacts.data.api.model.Contact;
import sk.vander.contacts.data.api.model.Order;
import sk.vander.contacts.data.api.model.request.ContactRequest;
import sk.vander.contacts.data.api.model.response.ListResponse;
import sk.vander.contacts.data.api.service.ContactService;
import sk.vander.contacts.data.api.service.OrderService;

/**
 * Created by arashid on 27/06/16.
 */
@ApplicationScope
@AutoExpose(App.class)
public class DataProvider {
  private final ContactService contactService;
  private final OrderService orderService;
  private final BehaviorSubject<Contact> selectedContact = BehaviorSubject.create();

  @Inject public DataProvider(ContactService contactService, OrderService orderService) {
    this.contactService = contactService;
    this.orderService = orderService;
  }

  public Observable<List<Contact>> getContacts() {
    return Observable.concat(
        contactService.getContactsCached().map(ListResponse::items).onErrorResumeNext(Observable.just(Collections.emptyList())),
        contactService.getContacts().map(ListResponse::items))
        .subscribeOn(Schedulers.io());
  }

  public Observable<List<Order>> getOrders() {
    return selectedContact.flatMap(c -> Observable.concat(
        orderService.getOrdersCached(c.id()).map(ListResponse::items).onErrorResumeNext(Observable.just(Collections.emptyList())),
        orderService.getOrders(c.id()).map(ListResponse::items)))
        .subscribeOn(Schedulers.io());
  }

  public BehaviorSubject<Contact> selectedContact() {
    return selectedContact;
  }

  public Observable<Contact> createContact(ContactRequest rc) {
    return contactService.createContact(rc).subscribeOn(Schedulers.io());
  }
}

@@ -47,51 +47,119 @@
 ort 
-java.util.Collections;%0Aimport java.util.Lis
+com.fernandocejas.frodo.annotation.RxLogObservable;%0A%0Aimport java.util.List;%0Aimport java.util.concurrent.TimeUni
 t;%0A%0A
@@ -1202,24 +1202,175 @@
 rvice;%0A  %7D%0A%0A
+//  note for edge case: need to apply delay, cached onNext gets consumed by onError in enclosing flatMap, testable with no internet%0A  @RxLogObservable%0A
   public Obs
@@ -1539,37 +1539,85 @@
 ble.
-just(Collections.emptyList())
+empty()),%0A        Observable.%3CList%3CContact%3E%3Eempty().delay(1, TimeUnit.SECONDS
 ),%0A 
@@ -1714,32 +1714,51 @@
 ers.io());%0A  %7D%0A%0A
+  @RxLogObservable%0A
   public Observa
@@ -1950,37 +1950,83 @@
 ble.
-just(Collections.emptyList())
+empty()),%0A        Observable.%3CList%3COrder%3E%3Eempty().delay(1, TimeUnit.SECONDS
 ),%0A 

fix for offline edge case