package de.czyrux.store.ui.catalog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import butterknife.BindView;
import de.czyrux.store.R;
import de.czyrux.store.core.domain.cart.CartProduct;
import de.czyrux.store.core.domain.cart.CartProductFactory;
import de.czyrux.store.core.domain.cart.CartService;
import de.czyrux.store.core.domain.product.Product;
import de.czyrux.store.core.domain.product.ProductResponse;
import de.czyrux.store.core.domain.product.ProductService;
import de.czyrux.store.inject.Injector;
import de.czyrux.store.ui.base.BaseFragment;
import de.czyrux.store.util.RxUtil;

public class CatalogFragment extends BaseFragment implements CatalogListener {

    private static final int GRID_COLUMNS = 1;

    @BindView(R.id.catalog_emptyView)
    View emptyView;

    @BindView(R.id.catalog_progressbar)
    ProgressBar progressBar;

    @BindView(R.id.catalog_recyclerview)
    RecyclerView recyclerView;

    private ProductService productService;

    private CartService cartService;

    public static CatalogFragment newInstance() {
        CatalogFragment fragment = new CatalogFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public CatalogFragment() {
    }

    @Override
    protected int layoutId() {
        return R.layout.catalog_fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        productService = Injector.productService();
        cartService = Injector.cartService();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(view.getContext(), GRID_COLUMNS, LinearLayoutManager.VERTICAL, false));
    }

    @Override
    public void onStart() {
        super.onStart();
        showProgressBar();
        addSubscritiption(productService.getAllCatalog()
                .compose(RxUtil.applyStandardSchedulers())
                .subscribe(this::onProductResponse, RxUtil.logError()));
    }

    private void showProgressBar() {
        progressBar.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);
        recyclerView.setAdapter(null);
    }

    private void onProductResponse(ProductResponse productResponse) {
        progressBar.setVisibility(View.GONE);
        if (productResponse.isEmpty()) {
            emptyView.setVisibility(View.VISIBLE);
        } else {
            emptyView.setVisibility(View.GONE);
            CatalogAdapter adapter = new CatalogAdapter(this);
            adapter.setProductList(productResponse.getProducts());
            recyclerView.setAdapter(adapter);
        }
    }

    @Override
    public void onProductClicked(Product product) {
        CartProduct cartProduct = CartProductFactory.newCartProduct(product, 1);
        addSubscritiption(cartService.addProduct(cartProduct)
                .compose(RxUtil.applyStandardSchedulers())
                .subscribe(RxUtil.emptyObserver()));

        Toast.makeText(getContext(), "Adding to cart..." + product.title, Toast.LENGTH_SHORT).show();
    }
}
@@ -2264,24 +2264,25 @@
 gressBar();%0A
+%0A
         addS
@@ -2723,43 +2723,24 @@
     
-p
+hideP
 rogress
-Bar.setVisibility(View.GONE
+bar(
 );%0A 
@@ -2795,74 +2795,176 @@
     
-emptyView.setVisibility(View.VISIBLE);%0A        %7D else %7B%0A  
+showEmptyCatalog();%0A        %7D else %7B%0A            showCatalog(productResponse);%0A        %7D%0A    %7D%0A%0A    private void showCatalog(ProductResponse productResponse) %7B%0A
         
   em
@@ -2955,26 +2955,24 @@
 ) %7B%0A        
-  
 emptyView.se
@@ -2991,28 +2991,24 @@
 View.GONE);%0A
-    
         Cata
@@ -3062,20 +3062,16 @@
         
-    
 adapter.
@@ -3121,28 +3121,24 @@
 ));%0A        
-    
 recyclerView
@@ -3159,29 +3159,201 @@
 apter);%0A    
-    %7D
+%7D%0A%0A    private void showEmptyCatalog() %7B%0A        emptyView.setVisibility(View.VISIBLE);%0A    %7D%0A%0A    private void hideProgressbar() %7B%0A        progressBar.setVisibility(View.GONE);
 %0A    %7D%0A%0A    

Clean CatalogFragment