package org.pocketcampus.plugin.freeroom.android.adapter;

import java.util.List;

import org.pocketcampus.plugin.freeroom.R;
import org.pocketcampus.plugin.freeroom.android.FreeRoomController;
import org.pocketcampus.plugin.freeroom.android.FreeRoomModel;
import org.pocketcampus.plugin.freeroom.android.utils.FRTimesClient;
import org.pocketcampus.plugin.freeroom.android.views.FreeRoomHomeView;
import org.pocketcampus.plugin.freeroom.shared.ActualOccupation;
import org.pocketcampus.plugin.freeroom.shared.FRPeriod;
import org.pocketcampus.plugin.freeroom.shared.FRRoom;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * <code>ActualOccupationArrayAdapter</code> is a simple
 * <code>ArrayAdapter</code>, which only redefines getView in order to change
 * the display without changing the data.
 * 
 * 
 * @author FreeRoom Project Team (2014/05)
 * @author Julien WEBER <julien.weber@epfl.ch>
 * @author Valentin MINDER <valentin.minder@epfl.ch>
 * 
 * @param <T>
 *            The type of data is the lists.
 */

public class ActualOccupationArrayAdapter<T> extends
		ArrayAdapter<ActualOccupation> {
	private Context context;
	private List<ActualOccupation> data;
	// hold the caller view for colors updates.
	private FreeRoomModel mModel;
	private FreeRoomController mController;
	private FreeRoomHomeView homeView;

	public ActualOccupationArrayAdapter(Context c, List<ActualOccupation> data,
			FreeRoomController mController, FreeRoomHomeView homeView) {
		super(c, R.layout.sdk_list_entry, R.id.sdk_list_entry_text, data);
		this.context = c;
		this.data = data;
		this.mController = mController;
		this.homeView = homeView;
		this.mModel = (FreeRoomModel) mController.getModel();
	}

	@Override
	public View getView(int index, View convertView, ViewGroup parent) {
		ViewHolder vholder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.freeroom_layout_room_details, null);
			vholder = new ViewHolder();
			vholder.setTextView((TextView) convertView
					.findViewById(R.id.freeroom_layout_details_text));
			vholder.setImageViewShare((ImageView) convertView
					.findViewById(R.id.freeroom_layout_details_share));
			vholder.setImageViewPeople((ImageView) convertView
					.findViewById(R.id.freeroom_layout_details_people));
			convertView.setTag(vholder);
		} else {
			vholder = (ViewHolder) convertView.getTag();
		}

		final ActualOccupation mActualOccupation = data.get(index);
		TextView tv = vholder.getTextView();
		FRPeriod mFrPeriod = mActualOccupation.getPeriod();
		tv.setText(FRTimesClient.getInstance().formatTimePeriod(mFrPeriod,
				false, false));
		// displayed text is minimal: only hours
		// free/occupied: it's known by color
		// user occupancy: indication by the occupation image

		boolean free = mActualOccupation.isAvailable();

		final ImageView ivpeople = vholder.getImageViewPeople();
		if (free) {
			ivpeople.setImageResource(mModel
					.getImageFromRatioOccupation(mActualOccupation
							.getRatioOccupation()));
		} else {
			ivpeople.setImageResource(R.drawable.occupation_occupied);
		}

		final ImageView ivshare = vholder.getImageViewShare();
		if (free) {
			ivshare.setImageResource(R.drawable.ic_action_share_enabled);
			OnClickListener ocl = new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					FRPeriod mPeriod = mActualOccupation.getPeriod();
					FRRoom mRoom = mModel.getDisplayedOccupancy().getRoom();
					homeView.displayShareDialog(mPeriod, mRoom);
				}
			};
			// share icon clickable
			ivshare.setOnClickListener(ocl);
			// whole line clickable
			convertView.setOnClickListener(ocl);
		} else {
			ivshare.setImageResource(R.drawable.ic_action_share_disabled);
			// share icon non clickable
			ivshare.setOnClickListener(null);
			// whole line non clickable
			convertView.setOnClickListener(null);
		}

		// TODO: remove this: only for message test!
		OnClickListener ocl2 = new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				FRPeriod mPeriod = mActualOccupation.getPeriod();
				FRRoom mRoom = mModel.getDisplayedOccupancy().getRoom();
				if (mActualOccupation.isSetUserMessages()) {
					System.out
							.println("actual occupation array adapdter - line 126");
					System.out.println(mRoom.getDoorCode());
					System.out.println(mPeriod.getTimeStampStart());
					System.out.println(mPeriod.getTimeStampEnd());
					System.out.println(mActualOccupation.getUserMessages());
				}
			}
		};
		convertView.setOnClickListener(ocl2);

		int color = free ? mModel.COLOR_CHECK_OCCUPANCY_FREE
				: mModel.COLOR_CHECK_OCCUPANCY_OCCUPIED;
		convertView.setBackgroundColor(color);
		return convertView;
	}

	// TODO: verify that it's working correctly (right method)
	@Override
	public boolean isEnabled(int index) {
		return data.get(index).isAvailable();
	}

	/**
	 * Class used to keep a view, it saves ressources by avoiding multiple
	 * inflate and findViewById operations.
	 * 
	 */
	private class ViewHolder {
		private TextView tv = null;
		private ImageView ivshare = null;
		private ImageView ivpeople = null;

		public void setTextView(TextView tv) {
			this.tv = tv;
		}

		public TextView getTextView() {
			return this.tv;
		}

		public void setImageViewShare(ImageView iv) {
			this.ivshare = iv;
		}

		public ImageView getImageViewShare() {
			return this.ivshare;
		}

		public void setImageViewPeople(ImageView iv) {
			this.ivpeople = iv;
		}

		public ImageView getImageViewPeople() {
			return this.ivpeople;
		}
	}

}

@@ -4090,668 +4090,8 @@
 %09%7D%0A%0A
-%09%09// TODO: remove this: only for message test!%0A%09%09OnClickListener ocl2 = new OnClickListener() %7B%0A%09%09%09@Override%0A%09%09%09public void onClick(View arg0) %7B%0A%09%09%09%09FRPeriod mPeriod = mActualOccupation.getPeriod();%0A%09%09%09%09FRRoom mRoom = mModel.getDisplayedOccupancy().getRoom();%0A%09%09%09%09if (mActualOccupation.isSetUserMessages()) %7B%0A%09%09%09%09%09System.out%0A%09%09%09%09%09%09%09.println(%22actual occupation array adapdter - line 126%22);%0A%09%09%09%09%09System.out.println(mRoom.getDoorCode());%0A%09%09%09%09%09System.out.println(mPeriod.getTimeStampStart());%0A%09%09%09%09%09System.out.println(mPeriod.getTimeStampEnd());%0A%09%09%09%09%09System.out.println(mActualOccupation.getUserMessages());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09convertView.setOnClickListener(ocl2);%0A%0A
 %09%09in

Revert "[freeroom.android] test: messages printed to sys out for testing!"