package com.howard.rockear.adapters;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.howard.rockear.R;
import com.howard.rockear.Utils.ImageProvider;
import com.howard.rockear.constant.AppConstant;
import com.howard.rockear.loaders.ImageCache;

public class TrackListAdapter extends BaseAdapter implements SectionIndexer {

	private ViewHolder viewHolder = null;
	private List<TrackListContent> listContents = null;
	private Context context = null;
	private ImageLoadSyncTask task;
	public ImageCache cache;

	// private AnimationDrawable animator;

	public TrackListAdapter(List<TrackListContent> listContent, Context mContext) {
		super();
		listContents = listContent;
		context = mContext;
		cache = ImageCache.getInstance(context);
	}

	@Override
	public View getView(int position, View mView, ViewGroup viewGroup) {

		if (mView == null) {
			viewHolder = new ViewHolder();
			mView = LayoutInflater.from(context).inflate(
					R.layout.tracklist_item, null);
			viewHolder.tv_title = (TextView) mView
					.findViewById(R.id.tv_line_one);
			viewHolder.iv_icon = (ImageView) mView
					.findViewById(R.id.iv_icon_local);
			viewHolder.tv_letter = (TextView) mView
					.findViewById(R.id.tv_catalog_local);
			viewHolder.tv_duration = (TextView) mView
					.findViewById(R.id.tv_small_right);
			viewHolder.tv_artist = (TextView) mView
					.findViewById(R.id.tv_line_two);
			mView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) mView.getTag();
		}

		final TrackListContent mContent = listContents.get(position);
		if (position == 0) {
			viewHolder.tv_letter.setVisibility(View.VISIBLE);
			viewHolder.tv_letter.setText(mContent.getLetterString());
		} else {
			String lastCatalog = listContents.get(position - 1)
					.getLetterString();
			if (mContent.getLetterString().equals(lastCatalog)) {
				viewHolder.tv_letter.setVisibility(View.GONE);
			} else {
				viewHolder.tv_letter.setVisibility(View.VISIBLE);
				viewHolder.tv_letter.setText(mContent.getLetterString());
			}
		}

		viewHolder.tv_title
				.setText(listContents.get(position).getTitleString());
		viewHolder.tv_artist.setText(listContents.get(position)
				.getArtistString());
		viewHolder.tv_duration.setText(listContents.get(position)
				.getDurationString());

		if (!AppConstant.LIST_SCROLLING) {
			task = new ImageLoadSyncTask(viewHolder.iv_icon,
					listContents.get(position), context);
			task.execute("please do it");
		}

		return mView;
	}

	@Override
	public int getCount() {
		return listContents.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	final static class ViewHolder {
		TextView tv_title = null;
		TextView tv_letter = null;
		TextView tv_artist = null;
		TextView tv_duration = null;
		ImageView iv_icon = null;
	}

	@Override
	public Object[] getSections() {
		return null;
	}

	@Override
	public int getSectionForPosition(int position) {

		return 0;
	}

	@Override
	public int getPositionForSection(int section) {
		TrackListContent mContent;
		String l;
		if (section == '!') {
			return 0;
		} else {
			for (int i = 0; i < getCount(); i++) {
				mContent = listContents.get(i);
				l = mContent.getLetterString();
				char firstChar = l.toUpperCase().charAt(0);
				if (firstChar == section) {
					return i + 1;
				}

			}
		}
		mContent = null;
		l = null;
		return -1;
	}

	final class ImageLoadSyncTask extends AsyncTask<String, String, Bitmap> {
		private ImageView mImageView;
		private TrackListContent trackListContent;
		private Context mContext;

		/**
		 * @param mImageView
		 * @param trackListContent
		 * @param mContext
		 */
		public ImageLoadSyncTask(ImageView mImageView,
				TrackListContent trackListContent, Context mContext) {
			super();
			this.mImageView = mImageView;
			this.trackListContent = trackListContent;
			this.mContext = mContext;
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			Bitmap bitmap = cache.get(trackListContent.getId() + "");
			Log.i("howard", cache + "");

			if (bitmap != null) {
				Log.i("howard", "frm cache not null------>>>" + bitmap);
				return bitmap;

			} else {
				bitmap = ImageProvider.getArtWork(mContext,
						trackListContent.getId(),
						trackListContent.getAlbumId(), true);
				cache.add(trackListContent.getId() + "", bitmap);
			}
			return bitmap;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			// animator.stop();
			mImageView.setImageBitmap(result);
			super.onPostExecute(result);
		}

	}
}
