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

import java.util.List;

import javax.inject.Inject;

import it.polimi.dima.giftlist.data.model.Product;
import it.polimi.dima.giftlist.domain.repository.ProductRepository;
import it.polimi.dima.giftlist.domain.executor.PostExecutionThread;
import it.polimi.dima.giftlist.domain.executor.ThreadExecutor;
import rx.Observable;

/**
 * Created by Elena on 27/01/2016.
 */
public class GetProductListUseCase extends UseCase<List<Product>> {

    private final int DEFAULT_OFFSET = 25;
    private final ProductRepository productRepository;
    private final String category;
    private final String keywords;

    @Inject
    public GetProductListUseCase(String category,
                                 String keywords,
                                 ProductRepository productRepository,
                                 ThreadExecutor threadExecutor,
                                 PostExecutionThread postExecutionThread) {
        super(threadExecutor, postExecutionThread);
        this.productRepository = productRepository;
        this.category = category;
        this.keywords = keywords;
    }

    @Override
    protected Observable<List<Product>> buildUseCaseObservable() {
        return this.productRepository.getProductList(category, keywords, DEFAULT_OFFSET);
    }
}

@@ -45,16 +45,76 @@
 actor;%0A%0A
+import java.math.BigDecimal;%0Aimport java.math.RoundingMode;%0A
 import j
@@ -200,23 +200,24 @@
 a.model.
-Product
+Currency
 ;%0Aimport
@@ -246,42 +246,25 @@
 st.d
-omain.repository.ProductRepository
+ata.model.Product
 ;%0Aim
@@ -303,36 +303,37 @@
 ain.
-executor.PostExecutionThread
+repository.CurrencyRepository
 ;%0Aim
@@ -372,31 +372,36 @@
 ain.
-executor.ThreadExecu
+repository.ProductReposi
 tor
+y
 ;%0Aim
@@ -419,16 +419,43 @@
 ervable;
+%0Aimport rx.functions.Func2;
 %0A%0A/**%0A *
@@ -568,24 +568,31 @@
     private 
+static 
 final int DE
@@ -589,30 +589,32 @@
 nal int 
-DEFAULT_OFFSET
+PRODUCT_PER_PAGE
  = 25;%0A 
@@ -628,68 +628,198 @@
 ate 
-final ProductRepository productRepository;%0A    private final
+static final int DIGITS = 2;%0A    private static final int STARTING_OFFSET = 0;%0A    private ProductRepository productRepository;%0A    private CurrencyRepository currencyRepository;%0A    private
  Str
@@ -844,22 +844,16 @@
 private 
-final 
 String k
@@ -860,16 +860,46 @@
 eywords;
+%0A    private int searchOffset;
 %0A%0A    @I
@@ -941,73 +941,43 @@
 ase(
-String category,%0A                                 String keywords
+ProductRepository productRepository
 ,%0A  
@@ -1007,23 +1007,24 @@
         
-Product
+Currency
 Reposito
@@ -1018,39 +1018,40 @@
 rencyRepository 
-product
+currency
 Repository,%0A    
@@ -1083,37 +1083,23 @@
     
-ThreadExecutor threadExecut
+String categ
 or
+y
 ,%0A  
@@ -1133,101 +1133,79 @@
     
-PostExecutionThread postExecutionThread) %7B%0A        super(threadExecutor, postExecutionThread)
+String keywords) %7B%0A        this.currencyRepository = currencyRepository
 ;%0A  
@@ -1318,24 +1318,69 @@
 = keywords;%0A
+        this.searchOffset = STARTING_OFFSET;%0A
     %7D%0A%0A    @
@@ -1467,20 +1467,48 @@
     
-return this.
+Observable%3CList%3CProduct%3E%3E productList = 
 prod
@@ -1560,22 +1560,1070 @@
 ds, 
-DEFAULT_OFFSET
+searchOffset*PRODUCT_PER_PAGE);%0A        Observable%3CList%3CCurrency%3E%3E currencyList = currencyRepository.getCurrencyList();%0A        searchOffset++;%0A        return Observable.combineLatest(productList, currencyList, new Func2%3CList%3CProduct%3E, List%3CCurrency%3E, List%3CProduct%3E%3E() %7B%0A            @Override%0A            public List%3CProduct%3E call(List%3CProduct%3E productList, List%3CCurrency%3E currencies) %7B%0A                for (Product p : productList) %7B%0A                    for (Currency c : currencies) %7B%0A                        if (c.getCurrencyType().equals(p.getCurrencyType())) %7B%0A                            p.setConvertedPrice(round(p.getPrice() / c.getRate(), DIGITS));%0A                        %7D%0A                    %7D%0A                %7D%0A                return productList;%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private float round(float value, int places) %7B%0A        if (places %3C 0) %7B%0A            throw new IllegalArgumentException();%0A        %7D%0A        BigDecimal bd = new BigDecimal(value);%0A        bd = bd.setScale(places, RoundingMode.HALF_UP);%0A        return bd.floatValue(
 );%0A 

Complete transfer to rx paradigm for data retrieval, fix regression in query to etsy API by readding increasing offset