package fishrungames.bashgid.core.db;

import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.PhotoManager.PhotoRecordData;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

public class PhotoDataSource
{

	private BashgidSqliteHelper dbHelper = null;

	public PhotoDataSource(Context context, BashgidSqliteHelper dbHelper)
	{

		this.dbHelper = dbHelper;

	}

	public void AddOrReplacePhoto(PhotoRecordData recordData)
	{
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{
				ContentValues values = new ContentValues();

				// values.put(BashgidSqliteHelper.COLUMN_ID, 1);
				values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
				values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
				values.put(BashgidSqliteHelper.COLUMN_IMAGE_URL, recordData.imageUrl);
				values.put(BashgidSqliteHelper.COLUMN_IMAGE_HASH, recordData.imageHash);
				values.put(BashgidSqliteHelper.COLUMN_GEOLAT, recordData.geoLat);
				values.put(BashgidSqliteHelper.COLUMN_GEOLON, recordData.geoLon);

				boolean r = isPhotoAlreadyExist(recordData.imageUrl, database);
				if (!r)
				{
					database.insert(BashgidSqliteHelper.TABLE_PHOTO, null, values);
				} else
				{
					database.update(BashgidSqliteHelper.TABLE_PHOTO, values, BashgidSqliteHelper.COLUMN_IMAGE_URL + " = " + "\"" + recordData.imageUrl + "\"",
							null);
				}

			} finally
			{
				dbHelper.close();
			}
		}

	}

	public PhotoRecordData GetPhotoByImageUrl(String imageUrl)
	{
		PhotoRecordData result = null;

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{
				result = InnerGetPhotoByImageUrl(imageUrl, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public PhotoRecordData InnerGetPhotoByImageUrl(String imageUrl, SQLiteDatabase database)
	{

		PhotoRecordData result = null;

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO, new String[] { BashgidSqliteHelper.COLUMN_TITLE,
				BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_IMAGE_URL, BashgidSqliteHelper.COLUMN_IMAGE_HASH,
				BashgidSqliteHelper.COLUMN_GEOLAT, BashgidSqliteHelper.COLUMN_GEOLON }, BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?", new String[] { imageUrl },
				null, null, null, null);

		if (cursor != null)
		{
			if (cursor.moveToFirst())
			{

				result = new PhotoRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getDouble(4),
						cursor.getDouble(5));

				result.title = MainActivity.getInstance().translationDataSource.TryTranslateText(result.title, database);
				result.description = MainActivity.getInstance().translationDataSource.TryTranslateText(result.description, database);
			}

			cursor.close();
			cursor = null;
		}

		return result;

	}

	private boolean isPhotoAlreadyExist(String imageUrl, SQLiteDatabase database)
	{
		boolean result = false;

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL },
				BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?", new String[] { imageUrl }, null, null, null, null);

		if (cursor != null)
		{
			if (cursor.getCount() > 0)
			{
				result = true;
			}

			cursor.close();
			cursor = null;
		}

		return result;

	}

}
