package com.howard.rockear.fragment;

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

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.howard.rockear.R;
import com.howard.rockear.Utils.QuerTools;
import com.howard.rockear.activity.ActivityRockEar;
import com.howard.rockear.adapters.PinyinComparator;
import com.howard.rockear.adapters.TrackListAdapter;
import com.howard.rockear.adapters.TrackListContent;
import com.howard.rockear.constant.AppConstant;
import com.howard.rockear.service.RockEarService;
import com.howard.rockear.views.CircleTextView;
import com.howard.rockear.views.SideBar;

/**
 * A placeholder fragment containing a simple view.
 */
public class FragmentFavourite extends FragmentBase {

	private View rootView = null;
	private Context mContext;
	private ListView mListView;
	private String TAG = "howard";
	public TrackListAdapter adapter = null;
	private List<TrackListContent> contents = null;

	private SideBar indexBar;
	private CircleTextView mDiaTextView;
	private static FragmentFavourite fragment;

	private ActivityRockEar baseActivity;

	// tools
	QuerTools qTools;

	@SuppressLint("HandlerLeak")
	private Handler indexBarHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				setIndexBar();
				break;
			}
		}

	};

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

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

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onDestroy()
	 */
	@Override
	public void onDestroy() {
		Log.i(TAG, "onDestory");
		super.onDestroy();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onPause()
	 */
	@Override
	public void onPause() {
		Log.i(TAG, "onPause");
		super.onPause();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onResume()
	 */
	@Override
	public void onResume() {
		Log.i(TAG, "onPause");
		super.onResume();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onSaveInstanceState(android.os.Bundle)
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		Log.i(TAG, "onSave");
		super.onSaveInstanceState(outState);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onStart()
	 */
	@Override
	public void onStart() {
		Log.i(TAG, "onStart");
		super.onStart();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onStop()
	 */
	@Override
	public void onStop() {
		Log.i(TAG, "onStop");
		super.onStop();
	}

	private void initListView() {

		mListView = (ListView) rootView.findViewById(R.id.lv_local);
		indexBar = (SideBar) rootView.findViewById(R.id.sideBar);

		if (AppConstant.FAVOURITE_LIST != null) {
			contents = AppConstant.FAVOURITE_LIST;
		} else {
			qTools = new QuerTools(mContext);
			contents = qTools.getListFrmDataBase("Favourite.db", "mTable", 1,
					"TITLE DESC", false);
			Log.i(TAG, "����QuerTools" + contents.size());
			Collections.sort(contents, new PinyinComparator());
		}

		if (contents.size() > 0) {
			RockEarService.NOW_PLAYING_LIST = (ArrayList<TrackListContent>) contents;
		}

		adapter = new TrackListAdapter(contents, getActivity());
		mListView.setAdapter(adapter);
		indexBar.setListView(mListView, false);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				RockEarService.NOW_PLAYING_LIST = (ArrayList<TrackListContent>) contents;
				RockEarService.NOW_PLAYING_INDEX = position;
				try {
					baseActivity.mServiceCallBack.playTrack();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		});

		mListView.setOnScrollListener(this);
	}

	public void updateListData() {
		contents = qTools.getListFrmDataBase("Favourite.db", "mTable", 1,
				"TITLE DESC", false);
		Collections.sort(contents, new PinyinComparator());
		adapter = new TrackListAdapter(contents, getActivity());
		mListView.setAdapter(adapter);
	}

	private void setIndexBar() {
		indexBar.setTextView(mDiaTextView);
	}

	/**
	 * @param textView
	 */
	public void setDialogTextView(CircleTextView textView) {
		mDiaTextView = textView;
	}

	@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() {

	}

}