package productcatalog.pages;

import com.google.common.base.Optional;
import common.contexts.AppContext;
import common.contexts.UserContext;
import common.prices.PriceFinder;
import common.utils.PriceFormatter;
import io.sphere.sdk.models.Image;
import io.sphere.sdk.models.LocalizedStrings;
import io.sphere.sdk.products.Price;
import io.sphere.sdk.products.ProductProjection;
import io.sphere.sdk.products.ProductVariant;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;

public class ProductThumbnailData {
    private final ProductProjection product;
    private final ProductVariant variant;
    private final AppContext context;
    private final PriceFormatter priceFormatter;

    public ProductThumbnailData(final ProductProjection product, final ProductVariant variant,
                                final AppContext context, final PriceFormatter priceFormatter) {
        this.product = product;
        this.variant = variant;
        this.context = context;
        this.priceFormatter = priceFormatter;
    }

    public String getText() {
        return findSuitableLanguage(product.getName());
    }

    public String getDescription() {
        return product.getDescription().map(this::findSuitableLanguage).orElse("");
    }

    public String getImage() {
        return variant.getImages().stream().findFirst().map(Image::getUrl).orElse("");
    }

    public String getPrice() {
        return findPrice(variant.getPrices(), context.user())
                .transform(price -> priceFormatter.format(price.getValue(), context))
                .or("");
    }

    private Optional<Price> findPrice(List<Price> prices, UserContext userContext) {
        return PriceFinder.of().findPrice(prices,
                userContext.currency(),
                userContext.country(),
                userContext.customerGroup(),
                userContext.channel(),
                ZonedDateTime.of(LocalDateTime.now(), userContext.zoneId()));
    }

    public String findSuitableLanguage(final LocalizedStrings localizedStrings) {
        return localizedStrings.get(context.user().language())
                .orElse(localizedStrings.get(context.user().fallbackLanguages())
                        .orElse(localizedStrings.get(context.project().languages()).orElse("")));
    }
}

@@ -28,48 +28,8 @@
 s;%0A%0A
-import com.google.common.base.Optional;%0A
 impo
@@ -460,20 +460,53 @@
 va.util.
-List
+Collection;%0Aimport java.util.Optional
 ;%0A%0Apubli
@@ -1512,17 +1512,11 @@
    .
-transform
+map
 (pri
@@ -1590,16 +1590,20 @@
      .or
+Else
 (%22%22);%0A  
@@ -1649,12 +1649,24 @@
 ice(
-List
+final Collection
 %3CPri
@@ -1676,16 +1676,22 @@
  prices,
+ final
  UserCon

Refactor ProductThumbnailData removed guava dependency