package com.howard.rockear.Utils;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AlbumColumns;
import android.provider.MediaStore.Audio.AudioColumns;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;

import com.howard.rockear.adapters.TrackListContent;

@SuppressLint("DefaultLocale")
public class MediaUtils {

	/**
	 * @param c
	 * @return get all the tracks frm android
	 */
	public static ArrayList<TrackListContent> GetMusicDataList(Context c) {
		ArrayList<TrackListContent> list = new ArrayList<TrackListContent>();
		ContentResolver cr = c.getContentResolver();
		Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

		if (cursor != null && cursor.getCount() > 0) {
			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				long id = cursor
						.getLong(cursor.getColumnIndex(BaseColumns._ID));
				String title = cursor.getString(cursor
						.getColumnIndexOrThrow(MediaColumns.TITLE));

				String singer = cursor.getString(cursor
						.getColumnIndexOrThrow(AudioColumns.ARTIST));

				int time = cursor.getInt(cursor
						.getColumnIndexOrThrow(AudioColumns.DURATION));
				time = time / 60000;

				String name = cursor.getString(cursor
						.getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME));
				//
				// String suffix = name
				// .substring(name.length() - 4, name.length());

				String url = cursor.getString(cursor
						.getColumnIndexOrThrow(MediaColumns.DATA));
				String album = cursor.getString(cursor
						.getColumnIndexOrThrow(AudioColumns.ALBUM));
				long albumid = cursor.getLong(cursor
						.getColumnIndex(AudioColumns.ALBUM_ID));

				if (url.endsWith(".mp3") || url.endsWith(".MP3")) {
					TrackListContent track = new TrackListContent();
					track.setTitleString(title);
					track.setDisplayNameString(name);
					track.setArtistString(singer);
					track.setId(id);
					track.setDurationString(time + "" + "min");
					track.setUrlString(url);
					track.setAlbumString(album);
					track.setAlbumId(albumid);
					// get the first letter only
					track.setLetterString(LetterUtil.getFirstLetter(title)
							.substring(0, 1));
					list.add(track);
				}
			}
		}
		cursor.close();
		return list;

	}

	/**
	 * @param list
	 * @return get the artist frm trackList
	 */
	public static ArrayList<TrackListContent> findAllArtistFromList(
			ArrayList<TrackListContent> list) {

		String mArtistName = null;
		TrackListContent mTrackListContent = new TrackListContent();
		ArrayList<TrackListContent> _listContents = new ArrayList<TrackListContent>();
		ArrayList<String> artists = new ArrayList<String>();

		for (int i = 0; i < list.size(); i++) {

			mTrackListContent = list.get(i);
			mArtistName = mTrackListContent.getArtistString();
			Log.i("howard", mArtistName);
			if (!artists.contains(mArtistName)) {
				artists.add(mArtistName);
				_listContents.add(mTrackListContent);
			}
		}
		artists = null;
		return _listContents;
	}

	/**
	 * @param context
	 * @param artistList
	 * @return query the tracks of the given artist list
	 */
	public static ArrayList<ArrayList<TrackListContent>> findTracksByArtist(
			Context context, ArrayList<TrackListContent> artistList) {
		Cursor cursor = null;
		ArrayList<ArrayList<TrackListContent>> childList = new ArrayList<ArrayList<TrackListContent>>();
		for (int i = 0; i < artistList.size(); i++) {

			ArrayList<TrackListContent> _traList = new ArrayList<TrackListContent>();

			cursor = context.getContentResolver().query(
					MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
					null,
					// need a "'" here
					AudioColumns.ARTIST + "=" + "\'"
							+ artistList.get(i).getArtistString() + "\'", null,
					MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

			if (cursor != null && cursor.getCount() > 0) {
				for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
						.moveToNext()) {
					long id = cursor.getLong(cursor
							.getColumnIndex(BaseColumns._ID));
					String title = cursor.getString(cursor
							.getColumnIndexOrThrow(MediaColumns.TITLE));

					String singer = cursor.getString(cursor
							.getColumnIndexOrThrow(AudioColumns.ARTIST));

					int time = cursor.getInt(cursor
							.getColumnIndexOrThrow(AudioColumns.DURATION));
					time = time / 60000;

					String name = cursor.getString(cursor
							.getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME));
					//
					// String suffix = name
					// .substring(name.length() - 4, name.length());

					String url = cursor.getString(cursor
							.getColumnIndexOrThrow(MediaColumns.DATA));
					String album = cursor.getString(cursor
							.getColumnIndexOrThrow(AudioColumns.ALBUM));
					long albumid = cursor.getLong(cursor
							.getColumnIndex(AudioColumns.ALBUM_ID));

					if (url.endsWith(".mp3") || url.endsWith(".MP3")) {
						TrackListContent track = new TrackListContent();
						track.setTitleString(title);
						track.setDisplayNameString(name);
						track.setArtistString(singer);
						track.setId(id);
						track.setDurationString(time + "" + "min");
						track.setUrlString(url);
						track.setAlbumString(album);
						track.setAlbumId(albumid);
						track.setLetterString(LetterUtil.getFirstLetter(title)
								.substring(0, 1));
						_traList.add(track);
					}
				}
			}
			// close here
			cursor.close();
			childList.add(_traList);
		}
		cursor.close();
		return childList;
	}

	/**
	 * @param context
	 * @return get the album frm android
	 */
	public static ArrayList<TrackListContent> findAllAlbumFrmStore(
			Context context) {
		ArrayList<TrackListContent> _listContents = new ArrayList<TrackListContent>();
		ContentResolver cr = context.getContentResolver();
		Cursor cursor = cr.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
				null, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);

		if (cursor != null && cursor.getCount() > 0) {
			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				long id = cursor
						.getLong(cursor.getColumnIndex(BaseColumns._ID));

				String singer = cursor.getString(cursor
						.getColumnIndexOrThrow(AlbumColumns.ARTIST));

				String album = cursor.getString(cursor
						.getColumnIndexOrThrow(AlbumColumns.ALBUM));

				TrackListContent track = new TrackListContent();
				track.setArtistString(singer);
				track.setId(id);
				track.setAlbumString(album);
				track.setLetterString(LetterUtil.getFirstLetter(album)
						.substring(0, 1));
				_listContents.add(track);
			}
		}
		cursor.close();
		return _listContents;
	}
}