package fishrungames.bashgid.core.db;

import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.VideoManager.VideoRecordData;

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

import android.database.sqlite.SQLiteDatabase;

public class VideoDataSource
{
	private BashgidSqliteHelper dbHelper = null;

	public VideoDataSource(Context context, BashgidSqliteHelper dbHelper)
	{

		this.dbHelper = dbHelper;

	}

	public void AddOrReplaceVideo(VideoRecordData recordData)
	{
		SQLiteDatabase database = dbHelper.getWritableDatabase();

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

				values.put(BashgidSqliteHelper.COLUMN_TYPE, recordData.type);
				values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
				values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
				values.put(BashgidSqliteHelper.COLUMN_VIDEO_URL, recordData.videoUrl);
				values.put(BashgidSqliteHelper.COLUMN_PREVIEW_IMAGE_URL, recordData.previewImageUrl);
				values.put(BashgidSqliteHelper.COLUMN_PREVIEW_IMAGE_HASH, recordData.previewImageHash);

				boolean r = isVideoAlreadyExist(recordData.videoUrl, database);
				if (!r)
				{
					database.insert(BashgidSqliteHelper.TABLE_VIDEO, null, values);
				} else
				{
					database.update(BashgidSqliteHelper.TABLE_VIDEO, values, BashgidSqliteHelper.COLUMN_VIDEO_URL + " = " + "\"" + recordData.videoUrl + "\"",
							null);
				}

			} finally
			{
				dbHelper.close();
			}
		}

	}

	public VideoRecordData GetVideoByVideoUrl(String videoUrl)
	{
		VideoRecordData result = null;

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{
				result = InnerGetVideoByVideoUrl(videoUrl, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public VideoRecordData InnerGetVideoByVideoUrl(String videoUrl, SQLiteDatabase database)
	{

		VideoRecordData result = null;

		Cursor cursor = database
				.query(BashgidSqliteHelper.TABLE_VIDEO, new String[] { BashgidSqliteHelper.COLUMN_TYPE, BashgidSqliteHelper.COLUMN_VIDEO_URL,
						BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_PREVIEW_IMAGE_URL,
						BashgidSqliteHelper.COLUMN_PREVIEW_IMAGE_HASH }, BashgidSqliteHelper.COLUMN_VIDEO_URL + "=?", new String[] { videoUrl }, null, null,
						null, null);

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

				result = new VideoRecordData(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
						cursor.getString(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 isVideoAlreadyExist(String videoUrl, SQLiteDatabase database)
	{
		boolean result = false;

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_VIDEO, new String[] { BashgidSqliteHelper.COLUMN_VIDEO_URL },
				BashgidSqliteHelper.COLUMN_VIDEO_URL + "=?", new String[] { videoUrl }, null, null, null, null);

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

			cursor.close();
			cursor = null;
		}

		return result;

	}

}
