
package com.uwetrottmann.shopr.loaders;

import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.util.Log;

import com.uwetrottmann.androidutils.Lists;
import com.uwetrottmann.shopr.algorithm.AdaptiveSelection;
import com.uwetrottmann.shopr.algorithm.model.Attributes;
import com.uwetrottmann.shopr.algorithm.model.ClothingType;
import com.uwetrottmann.shopr.algorithm.model.Color;
import com.uwetrottmann.shopr.algorithm.model.Item;
import com.uwetrottmann.shopr.algorithm.model.Price;
import com.uwetrottmann.shopr.algorithm.model.Sex;
import com.uwetrottmann.shopr.provider.ShoprContract.Items;
import com.uwetrottmann.shopr.settings.AppSettings;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Returns a list of items based on the current user model.
 */
public class ItemLoader extends GenericSimpleLoader<List<Item>> {

    private static final String TAG = "ItemLoader";
    private Location mLocation;
    private boolean mIsInit;

    public ItemLoader(Context context, Location location, boolean isInit) {
        super(context);
        mLocation = location;
        mIsInit = isInit;
    }

    @Override
    public List<Item> loadInBackground() {
        if (mLocation == null) {
            return new ArrayList<Item>();
        }

        AdaptiveSelection manager = AdaptiveSelection.get();

        // get initial case base
        if (mIsInit) {
            Log.d(TAG, "Initializing case base.");
            List<Item> caseBase = getInitialCaseBase();
            manager.setInitialCaseBase(caseBase);

            int maxRecommendations = AppSettings.getMaxRecommendations(getContext());
            AdaptiveSelection.get().setMaxRecommendations(maxRecommendations);
        }

        Log.d(TAG, "Fetching recommendations.");
        List<Item> recommendations = manager.getRecommendations();

        return recommendations;
    }

    private List<Item> getInitialCaseBase() {
        // TODO Implement all attributes
        List<Item> caseBase = Lists.newArrayList();

        Cursor query = getContext().getContentResolver().query(
                Items.CONTENT_URI,
                new String[] {
                        Items._ID, Items.CLOTHING_TYPE, Items.BRAND, Items.PRICE, Items.IMAGE_URL,
                        Items.COLOR, Items.SEX
                }, null, null, null);

        if (query != null) {
            while (query.moveToNext()) {
                Item item = new Item();

                item.id(query.getInt(0));
                item.image(query.getString(4));
                item.shopId(1);
                // name
                String type = query.getString(1);
                String brand = query.getString(2);
                item.name(type + " " + brand);
                // price
                BigDecimal price = new BigDecimal(query.getDouble(3));
                item.price(price);
                // critiquable attributes
                item.attributes(new Attributes()
                        .putAttribute(new ClothingType(query.getString(1)))
                        .putAttribute(new Color(query.getString(5)))
                        .putAttribute(new Price(price))
                        .putAttribute(new Sex(query.getString(6))));

                caseBase.add(item);
            }

            query.close();
        }

        return caseBase;
    }

}

@@ -2678,22 +2678,45 @@
     
-String type = 
+ClothingType type = new ClothingType(
 quer
@@ -2729,16 +2729,17 @@
 tring(1)
+)
 ;%0A      
@@ -2813,16 +2813,44 @@
 ame(type
+.currentValue().descriptor()
  + %22 %22 +
@@ -3122,44 +3122,12 @@
 ute(
-new ClothingType(query.getString(1))
+type
 )%0A  

Use English type names.