package com.howard.rockear.fragment;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

import com.howard.rockear.R;
import com.howard.rockear.Utils.MediaUtils;
import com.howard.rockear.activity.ActivityRockEar;
import com.howard.rockear.adapters.ArtistListAdapter;
import com.howard.rockear.adapters.PinyinComparator;
import com.howard.rockear.adapters.TrackListContent;
import com.howard.rockear.constant.AppConstant;
import com.howard.rockear.service.RockEarService;

/**
 * A placeholder fragment containing a simple view.
 */
public class FragmentArtists extends FragmentBase implements OnScrollListener {

	private View rootView = null;
	private Context mContext;
	private ExpandableListView mListView;
	private String TAG = "howard";
	private ArtistListAdapter adapter = null;
	private ArrayList<TrackListContent> groupList = null;
	private ArrayList<ArrayList<TrackListContent>> childList = null;

	// baseActivity
	private ActivityRockEar baseActivity;

	private static FragmentArtists fragment;

	public static FragmentArtists newInstance() {
		if (fragment == null) {
			fragment = new FragmentArtists();
		}
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_artist, container, false);
		initListView();
		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mContext = activity.getApplicationContext();
		baseActivity = (ActivityRockEar) activity;
	}

	@Override
	public void onDestroy() {
		Log.i(TAG, "onDestory");
		super.onDestroy();
	}

	@Override
	public void onPause() {
		Log.i(TAG, "onPause");
		super.onPause();
	}

	@Override
	public void onResume() {
		Log.i(TAG, "onResume");
		super.onResume();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		Log.i(TAG, "onSave");
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onStart() {
		Log.i(TAG, "onStart");
		super.onStart();
	}

	@Override
	public void onStop() {
		Log.i(TAG, "onStop");
		super.onStop();
	}

	private void initListView() {

		mListView = (ExpandableListView) rootView.findViewById(R.id.elv_art);

		if (AppConstant.ARTIST_GROUP_LIST != null
				&& AppConstant.ARTIST_CHILD_LIST != null) {
			groupList = AppConstant.ARTIST_GROUP_LIST;
			childList = AppConstant.ARTIST_CHILD_LIST;
		} else {
			groupList = new ArrayList<TrackListContent>();
			childList = new ArrayList<ArrayList<TrackListContent>>();
			groupList = MediaUtils.findAllArtistFromList(MediaUtils
					.GetMusicDataList(mContext));
			Collections.sort(groupList, new PinyinComparator());
			childList = MediaUtils.findTracksByArtist(mContext, groupList);
		}

		adapter = new ArtistListAdapter(groupList, childList, mContext);
		Log.i(TAG, "" + adapter);
		mListView.setAdapter(adapter);

		mListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {

				RockEarService.NOW_PLAYING_LIST = childList.get(groupPosition);
				RockEarService.NOW_PLAYING_INDEX = childPosition;
				try {
					baseActivity.mServiceCallBack.playTrack();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				return false;
			}
		});
	}

	@Override
	public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
		super.onScroll(arg0, arg1, arg2, arg3);

	}

	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {
		super.onScrollStateChanged(arg0, arg1);

	}

	@Override
	public void handleScroll() {
	}

	@Override
	public void handleIdle() {
		adapter.notifyDataSetChanged();

	}

	@Override
	public void handleFling() {

	}
}