package de.fau.cs.mad.fablab.android.view.fragments.productsearch;

import com.pedrogomez.renderers.AdapteeCollection;
import com.pedrogomez.renderers.ListAdapteeCollection;


import javax.inject.Inject;

import de.fau.cs.mad.fablab.android.model.ProductModel;
import de.fau.cs.mad.fablab.android.viewmodel.common.ObservableArrayList;
import de.fau.cs.mad.fablab.android.viewmodel.common.commands.Command;
import de.fau.cs.mad.fablab.rest.core.Product;

public class ProductSearchFragmentViewModel implements ObservableArrayList.Listener<Product> {
    ProductModel mModel;
    Listener mListener;

    private ListAdapteeCollection<ProductSearchViewModel> mProductSearchViewModelCollection;

    boolean mSearchState = false;

    private Command<String> searchCommand = new Command<String>() {
        @Override
        public void execute(String parameter) {
            mSearchState = true;
            mModel.searchForProduct(parameter);
            mListener.onSearchStateChanged();
        }
    };


    @Inject
    public ProductSearchFragmentViewModel(ProductModel mModel){
        this.mModel = mModel;
        mModel.getProducts().setListener(this);
        mProductSearchViewModelCollection = new ListAdapteeCollection<>();
    }

    public void setListener(Listener listener) {
        mListener = listener;
    }

    public Command<String> getSearchCommand() {
        return searchCommand;
    }


    @Override
    public void onItemAdded(Product newItem) {
        mProductSearchViewModelCollection.add(new ProductSearchViewModel(newItem));
        mSearchState = false;
        if (mListener != null) {
            mListener.onDataChanged();
            mListener.onSearchStateChanged();
        }
    }

    @Override
    public void onItemRemoved(Product removedItem) {
        //TODO not working, item should be removed
        mProductSearchViewModelCollection.remove(new ProductSearchViewModel(removedItem));
        if (mListener != null) {
            mListener.onDataChanged();
        }
    }

    public AdapteeCollection<ProductSearchViewModel> getProductSearchViewModelCollection() {
        return mProductSearchViewModelCollection;
    }

    public boolean getSearchState() {
        return mSearchState;
    }

    public void initialize() {
        if (mListener != null) {
            for (Product product : mModel.getProducts()) {
                mProductSearchViewModelCollection.add(new ProductSearchViewModel(product));
            }
            mListener.onDataChanged();
        }
    }

    public interface Listener{
        void onDataChanged();

        void onSearchStateChanged();
    }
}

@@ -169,16 +169,90 @@
 tion;%0A%0A%0A
+import java.util.HashMap;%0Aimport java.util.concurrent.ConcurrentHashMap;%0A%0A
 import j
@@ -271,16 +271,16 @@
 Inject;%0A
-
 %0Aimport 
@@ -758,16 +758,95 @@
 lection;
+%0A    private HashMap%3CProduct, ProductSearchViewModel%3E mProductSearchViewModels;
 %0A%0A    bo
@@ -1379,24 +1379,77 @@
 ection%3C%3E();%0A
+        mProductSearchViewModels = new HashMap%3C%3E();%0A%0A
     %7D%0A%0A    p
@@ -1668,32 +1668,112 @@
 duct newItem) %7B%0A
+        ProductSearchViewModel viewModel = new ProductSearchViewModel(newItem);%0A
         mProduct
@@ -1794,36 +1794,53 @@
 lCollection.add(
-new 
+viewModel);%0A        m
 ProductSearchVie
@@ -1845,24 +1845,29 @@
 iewModel
+s.put
 (newItem
 ));%0A    
@@ -1854,25 +1854,35 @@
 .put(newItem
-)
+, viewModel
 );%0A        m
@@ -2116,104 +2116,102 @@
     
-//TODO not working, item should be removed%0A        mProductSearchViewModelCollection.remove(new 
+mProductSearchViewModelCollection.remove(mProductSearchViewModels.get(removedItem));%0A        m
 Prod
@@ -2228,16 +2228,24 @@
 iewModel
+s.remove
 (removed
@@ -2241,33 +2241,32 @@
 ove(removedItem)
-)
 ;%0A        if (mL

fix productsearch list refresh. remember instance of ProductSearchViewModel so it could be removed