package it.polimi.dima.giftlist.domain.interactor;

import com.pushtorefresh.storio.sqlite.StorIOSQLite;
import com.pushtorefresh.storio.sqlite.queries.Query;
import com.pushtorefresh.storio.sqlite.queries.RawQuery;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

import it.polimi.dima.giftlist.data.db.table.EbayProductTable;
import it.polimi.dima.giftlist.data.db.table.EtsyProductTable;
import it.polimi.dima.giftlist.data.db.table.WishlistTable;
import it.polimi.dima.giftlist.data.model.EbayProduct;
import it.polimi.dima.giftlist.data.model.EtsyProduct;
import it.polimi.dima.giftlist.data.model.Product;
import it.polimi.dima.giftlist.data.model.Wishlist;
import rx.Observable;
import timber.log.Timber;

/**
 * Created by Alessandro on 18/03/16.
 */
public class GetDbProductListUseCase extends UseCase<List<Product>> {

    EventBus eventBus;
    StorIOSQLite db;
    long wishlistId;

    @Inject
    public GetDbProductListUseCase(EventBus eventBus, StorIOSQLite db, long wishlistId) {
        this.eventBus = eventBus;
        this.db = db;
        this.wishlistId = wishlistId;
    }

    @Override
    protected Observable<List<Product>> buildUseCaseObservable() {
        Timber.d("useCase buildUseCase");
        return Observable.concat(getWishlistEbayProductList(),
                                 getWishlistEtsyProductList());
    }



    private Observable<List<Product>> getWishlistEbayProductList() {
        Timber.d("useCase getEbayList");
        return db.get()
                .listOfObjects(EbayProduct.class)
                .withQuery(Query.builder()
                        .table(EbayProductTable.TABLE)
                        .where(EbayProductTable.COLUMN_WISHLIST_ID + "= ?")
                        .whereArgs(wishlistId)
                        .build())
                .prepare()
                .asRxObservable()
                .flatMap(ebayProducts -> Observable.from(ebayProducts))
                .cast(Product.class)
                .toList();
    }

    private Observable<List<Product>> getWishlistEtsyProductList() {
        Timber.d("useCase getEtsyList");
        return db.get()
                .listOfObjects(EtsyProduct.class)
                .withQuery(Query.builder()
                        .table(EtsyProductTable.TABLE)
                        .where(EtsyProductTable.COLUMN_WISHLIST_ID + "= ?")
                        .whereArgs(wishlistId)
                        .build())
                .prepare()
                .asRxObservable()
                .flatMap(etsyProducts -> Observable.from(etsyProducts))
                .cast(Product.class)
                .toList();
    }
}

@@ -252,16 +252,44 @@
 ntBus;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -750,24 +750,51 @@
 Observable;%0A
+import rx.functions.Func2;%0A
 import timbe
@@ -1349,11 +1349,18 @@
 e.co
-nca
+mbineLates
 t(ge
@@ -1373,70 +1373,236 @@
 istE
-bayProductList(),%0A                                 getWishl
+tsyProductList(),%0A                getWishlistEbayProductList(),%0A                new Func2%3CList%3CEtsyProduct%3E, List%3CEbayProduct%3E, List%3CProduct%3E%3E() %7B%0A                    @Override%0A                    public List%3CProduct%3E call(L
 ist
+%3C
 Etsy
@@ -1608,22 +1608,331 @@
 yProduct
-List()
+%3E etsyProducts, List%3CEbayProduct%3E ebayProducts) %7B%0A                        List%3CProduct%3E products = new ArrayList%3CProduct%3E();%0A                        products.addAll(etsyProducts);%0A                        products.addAll(ebayProducts);%0A                        return products;%0A                    %7D%0A                %7D
 );%0A    %7D
@@ -1955,32 +1955,36 @@
 Observable%3CList%3C
+Ebay
 Product%3E%3E getWis
@@ -2442,143 +2442,8 @@
 le()
-%0A                .flatMap(ebayProducts -%3E Observable.from(ebayProducts))%0A                .cast(Product.class)%0A                .toList()
 ;%0A  
@@ -2467,32 +2467,36 @@
 Observable%3CList%3C
+Etsy
 Product%3E%3E getWis
@@ -2954,143 +2954,8 @@
 le()
-%0A                .flatMap(etsyProducts -%3E Observable.from(etsyProducts))%0A                .cast(Product.class)%0A                .toList()
 ;%0A  

Fix handling of observables query of db products