package com.howard.rockear.Utils;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.howard.rockear.adapters.TrackListContent;
import com.howard.rockear.database.DataBaseHelper;

/**
 * @author howard
 * 
 */
public class QuerTools {

	private Context mContext;
	private ArrayList<TrackListContent> resultList = null;
	private DataBaseHelper helper;
	private SQLiteDatabase database;
	private Cursor cursor;
	private String TAG = "howard database";

	/**
	 * @param mContext
	 */
	public QuerTools(Context mContext) {
		super();
		this.mContext = mContext;
	}

	/**
	 * @param dbName
	 * @param tableName
	 * @param dbVersion
	 * @param order
	 * @return
	 */
	public ArrayList<TrackListContent> getListFrmDataBase(String dbName,
			String tableName, int dbVersion, String order, boolean limitCount) {

		try {

			helper = new DataBaseHelper(mContext, dbName, null, dbVersion);
			database = helper.getWritableDatabase();
			cursor = database.query(tableName, null, null, null, null, null,
					order);
			cursor.moveToFirst();
			Log.i(TAG, cursor.getCount() + "::::::::::::::::::::::");
			resultList = new ArrayList<TrackListContent>();

			// TITLE TEXT,ARTIST TEXT,ALBUM TEXT,DURATION TEXT,LETTER TEXT,
			// PATH TEXT,SONG_ID INTEGER,ALBUM_ID INTEGER
			while (!cursor.isAfterLast() && cursor.getCount() > 0) {
				Log.i(TAG,
						"begin---------finding in db------------>>>>>>>>>>>>>>>>>>>");
				TrackListContent trackListContent = new TrackListContent();
				trackListContent.setTitleString(cursor.getString(1));
				trackListContent.setArtistString(cursor.getString(2));
				trackListContent.setAlbumString(cursor.getString(3));
				trackListContent.setDurationString(cursor.getString(4));
				trackListContent.setLetterString(cursor.getString(5));
				trackListContent.setUrlString(cursor.getString(6));
				trackListContent.setId(cursor.getLong(7));
				trackListContent.setAlbumId(cursor.getLong(8));

				Log.i(TAG, trackListContent.getTitleString());
				resultList.add(trackListContent);
				if (limitCount) {
					if (resultList.size() > 12) {
						return resultList;
					}
				}
				cursor.moveToNext();
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			// recreate the table
			++dbVersion;
			helper.onUpgrade(database, --dbVersion, dbVersion);
		}

		return resultList;
	}

	/**
	 * @param song_id
	 * @param dbName
	 * @param tableName
	 * @param dbVersion
	 * @return
	 */
	public boolean checkIfHasAsFavourite(long song_id, String dbName,
			String tableName, int dbVersion) {
		try {

			helper = new DataBaseHelper(mContext, dbName, null, dbVersion);
			database = helper.getWritableDatabase();
			cursor = database.query(tableName, null, "SONG_ID=" + song_id,
					null, null, null, "TITLE DESC");
			if (cursor.getCount() > 0) {
				// Toast.makeText(mContext, ">0"+cursor.getCount(),
				// Toast.LENGTH_SHORT).show();
				Log.i(TAG, "" + cursor.getCount());
				return true;
			}

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			++dbVersion;
			helper.onUpgrade(database, --dbVersion, dbVersion);
		}
		return false;
	}

	public void removeTrackFrmDatabase(long song_id, String dbName,
			String tableName, int dbVersion) {
		try {
			helper = new DataBaseHelper(mContext, dbName, null, dbVersion);
			database = helper.getWritableDatabase();
			database.delete(tableName, "SONG_ID=?",
					new String[] { song_id + "" });
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			++dbVersion;
			helper.onUpgrade(database, --dbVersion, dbVersion);
		}
	}
}
