package com.howard.rockear.adapters;

import java.util.ArrayList;

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.BaseExpandableListAdapter;
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;

/**
 * @author howard
 * 
 */
public class ArtistListAdapter extends BaseExpandableListAdapter implements
		SectionIndexer {

	private ArrayList<TrackListContent> groupList = null;
	private ArrayList<ArrayList<TrackListContent>> childList = null;
	private Context mContext = null;
	private ImageLoadSyncTask task;
	public ImageCache cache;
	private GroupViewHolder groupHolder = null;
	private ChildViewHolder childHolder = null;
	private TrackListContent mChildContent = null;
	private TrackListContent mGroupContent = null;

	/**
	 * @param groupList
	 * @param childList
	 * @param mContext
	 */
	public ArtistListAdapter(ArrayList<TrackListContent> groupList,
			ArrayList<ArrayList<TrackListContent>> childList, Context mContext) {
		super();
		this.groupList = groupList;
		this.childList = childList;
		this.mContext = mContext;
		cache = ImageCache.getInstance(mContext);
	}

	@Override
	public View getGroupView(int position, boolean arg1, View mView,
			ViewGroup arg3) {
		if (mView == null) {
			groupHolder = new GroupViewHolder();
			mView = LayoutInflater.from(mContext).inflate(
					R.layout.artist_group_list_item, null);

			groupHolder.tv_title = (TextView) mView
					.findViewById(R.id.tv_line_one_art);
			groupHolder.iv_icon = (ImageView) mView
					.findViewById(R.id.iv_icon_local_art);
			groupHolder.tv_letter = (TextView) mView
					.findViewById(R.id.tv_catalog_local_art);
			groupHolder.tv_choldCount = (TextView) mView
					.findViewById(R.id.tv_child_count);

			mView.setTag(groupHolder);
		} else {
			groupHolder = (GroupViewHolder) mView.getTag();
		}

		mGroupContent = groupList.get(position);
		if (position == 0) {
			groupHolder.tv_letter.setVisibility(View.VISIBLE);
			groupHolder.tv_letter.setText(mGroupContent.getLetterString());
		} else {
			String lastCatalog = groupList.get(position - 1).getLetterString();
			if (mGroupContent.getLetterString().equals(lastCatalog)) {
				groupHolder.tv_letter.setVisibility(View.GONE);
			} else {
				groupHolder.tv_letter.setVisibility(View.VISIBLE);
				groupHolder.tv_letter.setText(mGroupContent.getLetterString());
			}
		}

		// set up the content
		groupHolder.tv_title.setText(groupList.get(position).getArtistString());
		groupHolder.tv_choldCount.setText(getChildrenCount(position) + "songs");

		// if scrolling stop update
		if (!AppConstant.LIST_SCROLLING) {
			task = new ImageLoadSyncTask(groupHolder.iv_icon,
					groupList.get(position), mContext);
			task.execute("please do it");
		}

		return mView;
	}

	@Override
	public View getChildView(int gp, int cp, boolean arg2, View mView,
			ViewGroup arg4) {
		if (mView == null) {
			childHolder = new ChildViewHolder();
			mView = LayoutInflater.from(mContext).inflate(
					R.layout.artist_child_list_item, null);

			childHolder.tv_title = (TextView) mView
					.findViewById(R.id.tv_line_one_art_child);

			mView.setTag(childHolder);
		} else {
			childHolder = (ChildViewHolder) mView.getTag();
		}

		mChildContent = (TrackListContent) getChild(gp, cp);
		childHolder.tv_title.setText(mChildContent.getTitleString());

		return mView;
	}

	@Override
	public Object getChild(int group_p, int child_p) {
		return childList.get(group_p).get(child_p);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return arg1;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		Log.i("howard", "has child count======="
				+ childList.get(groupPosition).size());
		return childList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int group_p) {
		return groupList.get(group_p);
	}

	@Override
	public int getGroupCount() {
		return groupList.size();
	}

	@Override
	public long getGroupId(int group_p) {
		return group_p;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

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

	@Override
	public int getSectionForPosition(int position) {

		return 0;
	}

	// for the SectionAdapter
	@Override
	public int getPositionForSection(int section) {
		TrackListContent mContent;
		String l;
		if (section == '!') {
			return 0;
		} else {
			for (int i = 0; i < getGroupCount(); i++) {
				mContent = groupList.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 GroupViewHolder {
		TextView tv_title = null;
		ImageView iv_icon = null;
		TextView tv_letter = null;
		TextView tv_choldCount = null;
	}

	final class ChildViewHolder {
		TextView tv_title = null;
	}

	// get image
	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
		/*
		 * search memory first if null fetch and save to memory (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
		 */
		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) {
			mImageView.setImageBitmap(result);
			super.onPostExecute(result);
		}

	}
}
