package org.adaptlab.chpir.android.survey.QuestionFragments;

import java.util.ArrayList;
import java.util.List;

import org.adaptlab.chpir.android.survey.QuestionFragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class SelectMultipleImageQuestionFragment extends QuestionFragment {
	private final int PADDING = 10;
	private final int WIDTH = 500;
	private final int HEIGHT = 500;
	private final int SELECTED = Color.GREEN;
	private final int UNSELECTED = Color.TRANSPARENT;
	private List<ImageView> mImageViews;
	
	@Override
	protected void createQuestionComponent(ViewGroup questionComponent) {
		for (ImageView v : createTempImages()) {
			questionComponent.addView(v);
		}
	}

	private List<ImageView> createTempImages() {
		mImageViews = new ArrayList<ImageView>();
		int id = getResources().getIdentifier("org.adaptlab.chpir.android.survey:drawable/" + "ic_action_picture", null, null);
		for (int i=0; i < 4; i++) {
			ImageView image = new ImageView(getActivity());
			image.setImageResource(id);
			image.setId(i);
			image.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
			image.getLayoutParams().width = WIDTH;
			image.getLayoutParams().height = HEIGHT;
			image.setPadding(PADDING, PADDING, PADDING, PADDING);
			image.setBackgroundColor(UNSELECTED); //getting the background color of ColorDrawable when it has not be set throws an exception
			image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					toggleImageBackgroundColor((ImageView) v);
				}
			});
			mImageViews.add(image);
		}
		return mImageViews;
	}
	
	private void toggleImageBackgroundColor(ImageView view) {
		ColorDrawable drawable = (ColorDrawable) view.getBackground();
		if (drawable.getColor() == SELECTED) {
			view.setBackgroundColor(UNSELECTED);
		} else {
			view.setBackgroundColor(SELECTED);
		}
		saveResponse();
	}

	@Override
	protected String serialize() {
		String serialized = "";
		for (int i=0; i<mImageViews.size(); i++) {
			ImageView view = mImageViews.get(i);
			ColorDrawable drawable = (ColorDrawable) view.getBackground();
			if (drawable.getColor() == SELECTED) {
				serialized += view.getId();
				if (i <  mImageViews.size() - 1) serialized += LIST_DELIMITER;
			}
		}
		return serialized;
	}

	@Override
	protected void deserialize(String responseText) {
		if (responseText.equals("")) return;    
        String[] listOfIndices = responseText.split(LIST_DELIMITER);
        for (String index : listOfIndices) {
            if (!index.equals("")) {
                Integer indexInteger = Integer.parseInt(index);
                mImageViews.get(indexInteger).setBackgroundColor(SELECTED);
            }
        }		
	}

}

@@ -165,16 +165,71 @@
 ragment;
+%0Aimport org.adaptlab.chpir.android.survey.Models.Image;
 %0A%0Aimport
@@ -889,25 +889,25 @@
 : create
-Temp
 Image
+View
 s()) %7B%0A%09
@@ -981,17 +981,17 @@
 eate
-Temp
 Image
+View
 s() 
@@ -1042,125 +1042,53 @@
 ;%0A%09%09
-int id = getResources().getIdentifier(%22org.adaptlab.chpir.android.survey:drawable/%22 + %22ic_action_picture%22, null, null
+List%3CImage%3E imageList = getQuestion().images(
 );%0A%09
@@ -1110,9 +1110,24 @@
 i %3C 
-4
+imageList.size()
 ; i+
@@ -1146,19 +1146,21 @@
 eView im
-age
+gView
  = new I
@@ -1185,27 +1185,29 @@
 ty());%0A%09%09%09im
-age
+gView
 .setImageRes
@@ -1207,27 +1207,51 @@
 mage
-Resource(id
+Bitmap(imageList.get(i).getBitmap()
 );%0A%09%09%09im
 age.
@@ -1242,27 +1242,29 @@
 ap());%0A%09%09%09im
-age
+gView
 .setId(i);%0A%09
@@ -1263,27 +1263,29 @@
 Id(i);%0A%09%09%09im
-age
+gView
 .setLayoutPa
@@ -1389,27 +1389,29 @@
 ENT));%0A%09%09%09im
-age
+gView
 .getLayoutPa
@@ -1437,19 +1437,21 @@
 H;%0A%09%09%09im
-age
+gView
 .getLayo
@@ -1479,27 +1479,29 @@
 EIGHT;%0A%09%09%09im
-age
+gView
 .setPadding(
@@ -1538,27 +1538,29 @@
 DING);%0A%09%09%09im
-age
+gView
 .setBackgrou
@@ -1676,19 +1676,21 @@
 on%0A%09%09%09im
-age
+gView
 .setOnCl
@@ -1860,19 +1860,21 @@
 s.add(im
-age
+gView
 );%0A%09%09%7D%0A%09

use downloaded images for select multiple images question