package org.commcare.android.adapters;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

import org.commcare.android.models.Entity;
import org.commcare.android.models.NodeEntityFactory;
import org.commcare.android.view.EntityView;
import org.commcare.dalvik.application.CommCareApplication;
import org.commcare.suite.model.Detail;
import org.javarosa.core.model.instance.TreeReference;

import java.util.Vector;

/**
 * Created by jschweers on 8/24/2015.
 * <p/>
 * Adapter for taking a nodeset, contextualizing it against an entity,
 * and then displaying one item for each node in the resulting set.
 */
public class EntitySubnodeListAdapter implements ListAdapter {

    private Context context;
    private Detail detail;
    private NodeEntityFactory factory;
    private Vector<TreeReference> references;

    public EntitySubnodeListAdapter(Context context, Detail detail, TreeReference contextReference, NodeEntityFactory factory) {
        this.context = context;
        this.detail = detail;
        this.factory = factory;

        TreeReference contextualizedNodeset = detail.getNodeset().contextualize(contextReference);
        this.references = CommCareApplication._().getCurrentSessionWrapper().getEvaluationContext().expandReference(contextualizedNodeset);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public int getCount() {
        return references.size();
    }

    @Override
    public Object getItem(int position) {
        return references.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        EntityView view = (EntityView) convertView;
        Entity entity = factory.getEntity(references.get(position));
        if (view == null) {
            view = new EntityView(context, detail, entity, null, null, position, false);
        } else {
            view.refreshViewsForNewEntity(entity, false, position);
        }
        return view;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return getCount() > 0;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
    }

}

@@ -338,23 +338,101 @@
 oid.
-view.EntityView
+util.AndroidUtil;%0D%0Aimport org.commcare.android.view.EntityView;%0D%0Aimport org.commcare.dalvik.R
 ;%0D%0Ai
@@ -1027,16 +1027,46 @@
 rences;%0D
+%0A    private int%5B%5D rowColors;%0D
 %0A%0D%0A    p
@@ -1529,16 +1529,193 @@
 deset);%0D
+%0A%0D%0A        this.rowColors = AndroidUtil.getThemeColorIDs(this.context,%0D%0A                new int%5B%5D%7BR.attr.drawer_pulldown_even_row_color, R.attr.drawer_pulldown_odd_row_color%7D);%0D
 %0A    %7D%0D%0A
@@ -2628,24 +2628,24 @@
   %7D else %7B%0D%0A
-
             
@@ -2712,16 +2712,151 @@
      %7D%0D%0A
+        int color = this.rowColors%5Bposition %25 2%5D;%0D%0A        if (color != -1) %7B%0D%0A            view.setBackgroundColor(color);%0D%0A        %7D%0D%0A
         

add striping