package com.sims.topaz.adapter;

import java.sql.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sims.topaz.R;
import com.sims.topaz.network.modele.Message;
import com.sims.topaz.utils.CameraUtils;
import com.sims.topaz.utils.MyTypefaceSingleton;
import com.sims.topaz.utils.SimsContext;

public class UserMessageAdapter extends ArrayAdapter<Message>  {

	private int count = 0;
	private byte[] image;
	public UserMessageAdapter(Context mDelegate, int resource, List<Message> messagesList,byte[] mImage) {
		super(mDelegate, resource, messagesList);
		this.image = mImage;
		count = messagesList.size();
	}	
	
	@Override
	public int getCount() {
		return count;
	}
	
	@SuppressWarnings("deprecation")
	public View getView(int position, View convertView, ViewGroup parent){
		View view = convertView;
		ViewHolder holder = null; 
		
		if(view == null){
			holder=new ViewHolder(); 
			LayoutInflater inflater = 
					(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.fragment_message_item, null);
			holder.mUserName = (TextView) view.findViewById(R.id.message_item_person_name);
			holder.mUserName.setTypeface(MyTypefaceSingleton.getInstance().getTypeFace());

			holder.mUserMessage = (TextView) view.findViewById(R.id.message_item_text);
			holder.mUserMessage.setTypeface(MyTypefaceSingleton.getInstance().getTypeFace());

			holder.mMessageDate = (TextView) view.findViewById(R.id.message_item_time);
			holder.mMessageDate.setTypeface(MyTypefaceSingleton.getInstance().getTypeFace());

			holder.mUserImage = (ImageView) view.findViewById(R.id.message_item_image);
			if(image!=null){
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;
				options.inSampleSize = CameraUtils.calculateInSampleSize(options, 32, 32);
				Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length,options);
				holder.mUserImage.setBackgroundDrawable(new BitmapDrawable(SimsContext.getContext().getResources(),bmp));
			}			
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		if(position >= 0){
			
			if(getItem(position)!=null){
				Message m = getItem(position);
				holder.mUserName.setText(m.getUserName());
				holder.mUserMessage.setText(m.getText());
				holder.mMessageDate.setText(DateFormat.format
						(getContext().getString(R.string.date_format), 
								new Date( m.getTimestamp()) ) );
			}
		}
		return view;
	}

	class ViewHolder {  
		TextView mUserName;
		TextView mUserMessage;
		TextView mMessageDate;
		ImageView mUserImage;
	}  
	
	
}

@@ -224,16 +224,57 @@
 awable;%0A
+import android.support.v4.util.LruCache;%0A
 import a
@@ -304,16 +304,16 @@
 Format;%0A
-
 import a
@@ -815,16 +815,146 @@
  image;%0A
+%09//tutorial: https://developer.android.com/training/displaying-bitmaps/display-bitmap.html%0A%09private Bitmap mImageBitmap = null;%0A%09%0A
 %09public 
@@ -1147,16 +1147,19 @@
 size();%0A
+%09%09%0A
 %09%7D%09%0A%09%0A%09@
@@ -2184,16 +2184,17 @@
 image);%0A
+%0A
 %09%09%09if(im
@@ -2201,24 +2201,53 @@
 age!=null)%7B%0A
+%09%09%09%09if(mImageBitmap==null)%7B%0A%09
 %09%09%09%09BitmapFa
@@ -2299,16 +2299,17 @@
 ions();%0A
+%09
 %09%09%09%09opti
@@ -2339,24 +2339,25 @@
 = true;%0A%09%09%09%09
+%09
 options.inSa
@@ -2427,19 +2427,22 @@
 %09%09%09%09
+%09mImage
 Bitmap 
-bmp 
 = Bi
@@ -2502,16 +2502,22 @@
 tions);%0A
+%09%09%09%09%7D%0A
 %09%09%09%09hold
@@ -2615,10 +2615,19 @@
 s(),
-b
 m
+ImageBitma
 p));

improve image loading