package foam.android.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import foam.android.core.AttributeUtils;
import foam.core.DAO;
import foam.core.X;

/**
 * FOAM wrapper for presenting a DAO's data in a RecyclerView in list mode.
 *
 * The view to use for the row is specified in XML as foam:row_view and its value can either be a
 * <code>@layout/foobar</code> layout reference or a class name.
 */
public class DAOListViewBridge extends OneWayViewBridge<RecyclerView, DAO> {
  private DAOAdapter adapter;
  private Context context;
  private String rowView;

  public DAOListViewBridge(Context context) {
    this(context, null);
  }
  public DAOListViewBridge(Context context, AttributeSet attrs) {
    this.context = context;
    view = attrs == null ? new RecyclerView(context) : new RecyclerView(context, attrs);

    // Look in the attrs for a row_view setting.
    rowView = AttributeUtils.find(attrs, "row_view");
    tryToBuildView();
  }

  public void X(X x) {
    super.X(x);
    tryToBuildView();
  }

  public View getView() {
    if (view == null) {
      tryToBuildView();
      if (view == null) {
        Log.e("DAOListView", "Tried to build DAOListView before its X is set.");
        return null;
      }
    }
    return super.getView();
  }

  private void tryToBuildView() {
    X x = X();
    if (x == null) return;

    DAO dao = (DAO) X().get("dao");
    if (dao == null) dao = (DAO) X().get("data");
    if (dao == null) return;
    adapter = new DAOAdapter(X(), dao, new DetailViewFactory(rowView, dao.getModel()));
    view.setAdapter(adapter);
    final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    view.setLayoutManager(layoutManager);
  }

  @Override
  protected void updateViewFromValue() {
    // TODO(braden): Implement me. Need to figure out the context and DAO first, though.
  }
}

@@ -53,16 +53,160 @@
 ontext;%0A
+import android.content.res.TypedArray;%0Aimport android.graphics.Canvas;%0Aimport android.graphics.Rect;%0Aimport android.graphics.drawable.Drawable;%0A
 import a
@@ -1672,35 +1672,33 @@
 DAO dao = (DAO) 
-X()
+x
 .get(%22dao%22);%0A   
@@ -1727,19 +1727,17 @@
 = (DAO) 
-X()
+x
 .get(%22da
@@ -1771,16 +1771,17 @@
 return;%0A
+%0A
     adap
@@ -1801,19 +1801,17 @@
 Adapter(
-X()
+x
 , dao, n
@@ -2074,16 +2074,72 @@
 nager);%0A
+    view.addItemDecoration(new RowDecoration(context));%0A
   %7D%0A%0A  @
@@ -2281,10 +2281,1140 @@
 gh.%0A  %7D%0A
+%0A%0A  class RowDecoration extends RecyclerView.ItemDecoration %7B%0A    private Drawable mDivider;%0A    public RowDecoration(Context context) %7B%0A      final TypedArray a = context.obtainStyledAttributes(new int%5B%5D %7B android.R.attr.listDivider %7D);%0A      mDivider = a.getDrawable(0);%0A      a.recycle();%0A    %7D%0A%0A    @Override%0A    public void onDraw(Canvas c, RecyclerView parent) %7B%0A      final int left = parent.getPaddingLeft();%0A      final int right = parent.getWidth() - parent.getPaddingRight();%0A%0A      final int childCount = parent.getChildCount();%0A      for (int i = 0; i %3C childCount; i++) %7B%0A        final View child = parent.getChildAt(i);%0A        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();%0A        final int top = child.getBottom() + params.bottomMargin;%0A        final int bottom = top + mDivider.getIntrinsicHeight();%0A        mDivider.setBounds(left, top, right, bottom);%0A        mDivider.draw(c);%0A      %7D%0A    %7D%0A%0A    @Override%0A    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) %7B%0A      outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());%0A    %7D%0A  %7D%0A
 %7D%0A

Add row dividers to DAOListView