package com.facetedworlds.honeydolist.adapters;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.facetedworlds.honeydolist.R;
import com.updatecontrols.Dependent;
import com.updatecontrols.InvalidatedListener;
import com.updatecontrols.UpdateMethod;

import facetedworlds.honeydo.model.List;
import facetedworlds.honeydo.model.Task;
import facetedworlds.honeydo.model.Task__text;

public class TaskListAdapter extends ArrayAdapter<Task> {

	private List list;
	
	private Dependent depListSummaries;
	
	public TaskListAdapter(List list, final Activity context, int textViewResourceId) {
		super(context, textViewResourceId);
		
		this.list = list;
		depListSummaries = new Dependent(new UpdateMethod() {
			@Override
			public void update() {
				updateListSummaries();
			}
		});
		
		depListSummaries.addInalidatedListener(new InvalidatedListener() {
			@Override
			public void invalidated() {
				context.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						depListSummaries.onGet();
					}
				});
			}
		});
		depListSummaries.onGet();
	}

	private void updateListSummaries() {
		Log.i("TaskListAdapter", "updateListSummaries");
		
		this.clear();
		for (Task task : list.tasks()) {
			this.add(task);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Task task = this.getItem(position);
		View taskSummaryView = parent.findViewById(R.layout.task_summary);
		TextView taskTextView = (TextView)taskSummaryView.findViewById(R.id.task_text);
		taskTextView.setText(getTaskText(task));
		return taskSummaryView;
	}

	private String getTaskText(Task task) {
		String text = "";
		Log.i("TaskListAdapter", "updateListSummaries - add task");
		for (Task__text candidate : task.textCandidates()) {
			text = candidate.getValue();
			Log.i("TaskListAdapter", "updateListSummaries - add task " + text);
			break;
		}
		return text;
	}
	
}

@@ -71,16 +71,47 @@
 tivity;%0A
+import android.graphics.Color;%0A
 import a
@@ -246,58 +246,86 @@
 get.
-TextView;%0A%0Aimport com.facetedworlds.honeydolist.R;
+CheckBox;%0Aimport android.widget.LinearLayout;%0Aimport android.widget.TextView;%0A
 %0Aimp
@@ -655,16 +655,43 @@
 t list;%0A
+%09private Activity context;%0A
 %09%0A%09priva
@@ -869,16 +869,42 @@
 = list;%0A
+%09%09this.context = context;%0A
 %09%09depLis
@@ -1615,197 +1615,609 @@
 ;%0A%09%09
-View taskSummaryView = parent.findViewById(R.layout.task_summary);%0A%09%09TextView taskTextView = (TextView)taskSummaryView.findViewById(R.id.task_text);%0A%09%09taskTextView.setText(getTaskText(task)
+LinearLayout taskItem = new LinearLayout(context);%0A%09%09taskItem.setOrientation(LinearLayout.HORIZONTAL);%0A%09%09CheckBox taskCompleted = new CheckBox(context);%0A%09%09taskCompleted.setPadding(0, 4, 0, 0);%0A%09%09taskItem.addView(taskCompleted);%0A%09%09TextView taskTextView = new TextView(context);%0A%09%09taskTextView.setText(getTaskText(task));%0A%09%09taskTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));%0A%09%09taskTextView.setTextSize(22.0f);%0A%09%09taskTextView.setTextColor(Color.WHITE);%0A%09%09taskTextView.setPadding(4, 0, 0, 0);%0A%09%09taskItem.addView(taskTextView
 );%0A%09
@@ -2232,19 +2232,12 @@
 task
-SummaryView
+Item
 ;%0A%09%7D

Create task item view in code.