/***
	Copyright (c) 2008 CommonsWare, LLC
	
	Licensed under the Apache License, Version 2.0 (the "License"); you may
	not use this file except in compliance with the License. You may obtain
	a copy of the License at
		http://www.apache.org/licenses/LICENSE-2.0
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

package com.commonsware.android.fancylists.eight;

import android.content.Context;
import android.database.DataSetObserver;
import android.widget.ListAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;

public class CheckableWrapper extends AdapterWrapper {
	Context ctxt=null;
	boolean[] states=null;
	
	public CheckableWrapper(Context ctxt, ListAdapter delegate) {
		super(delegate);
		
		this.ctxt=ctxt;
		this.states=new boolean[delegate.getCount()];
		
		for (int i=0;i<delegate.getCount();i++) {
			this.states[i]=false;
		}
	}
	
	public List<Integer> getCheckedPositions() {
		List<Integer> result=new ArrayList<Integer>();
		
		for (int i=0;i<delegate.getCount();i++) {
			if (states[i]) {
				result.add(new Integer(i));
			}
		}
		
		return(result);
	}
	
	public List<Object> getCheckedObjects() {
		List<Object> result=new ArrayList<Object>();
		
		for (int i=0;i<delegate.getCount();i++) {
			if (states[i]) {
				result.add(delegate.getItem(i));
			}
		}
		
		return(result);
	}
	
	public View getView(int position, View convertView,
											ViewGroup parent) {
		ViewWrapper wrap=null;
		View row=convertView;
														
		if (convertView==null) {
			LinearLayout layout=new LinearLayout(ctxt);
			CheckBox cb=new CheckBox(ctxt);
			View guts=delegate.getView(position, null, parent);
		
			layout.setOrientation(LinearLayout.HORIZONTAL); 
					
			cb.setLayoutParams(new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT,
						LinearLayout.LayoutParams.FILL_PARENT));
			guts.setLayoutParams(new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.FILL_PARENT,
						LinearLayout.LayoutParams.FILL_PARENT));
					
			cb.setOnCheckedChangeListener(
				new CheckBox.OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
																				boolean isChecked) {
						states[(Integer)buttonView.getTag()]=isChecked;
					}
			});

			layout.addView(cb); 					
			layout.addView(guts);
			
			wrap=new ViewWrapper(layout);
			wrap.setGuts(guts);
			layout.setTag(wrap);
			
			cb.setTag(new Integer(position));
			cb.setChecked(states[position]);
				
			row=layout;				
		}
		else {
			wrap=(ViewWrapper)convertView.getTag();
			wrap.setGuts(delegate.getView(position, wrap.getGuts(),
																		parent));
			wrap.getCheckBox().setTag(new Integer(position));
			wrap.getCheckBox().setChecked(states[position]);
		}
		
		return(row);
	}		
}
