package fishrungames.bashgid.core.db;

import java.util.ArrayList;

import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.AlbumManager;
import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.AlbumManager.AlbumRecordData;
import fishrungames.bashgid.core.AlbumManager.AlbumShortData;

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

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class AlbumDataSource
{

	private BashgidSqliteHelper dbHelper = null;

	public AlbumDataSource(Context context, BashgidSqliteHelper dbHelper)
	{
		this.dbHelper = dbHelper;

	}

	public void CreateNewAlbum(AlbumManager.AlbumRecordData recordData)
	{
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				ContentValues values = new ContentValues();

				values.put(BashgidSqliteHelper.COLUMN_NAME, recordData.name);
				values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
				values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
				values.put(BashgidSqliteHelper.COLUMN_HIDDEN, recordData.hidden);

				if (!innerIsAlbumAlreadyExist(recordData.name, database))
				{
					database.insert(BashgidSqliteHelper.TABLE_ALBUM, null, values);
				} else
				{
					database.update(BashgidSqliteHelper.TABLE_ALBUM, values, BashgidSqliteHelper.COLUMN_NAME + " = " +"\"" + recordData.name +"\"", null);
				}

			} finally
			{
				dbHelper.close();
			}
		}

	}

	public boolean IsAlbumAlreadyExist(String name)
	{
		boolean result = false;

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				result = innerIsAlbumAlreadyExist(name, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;

	}

	public void AddPhotoToAlbum(String imageUrl, String albumName)
	{
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				ContentValues values = new ContentValues();

				values.put(BashgidSqliteHelper.COLUMN_IMAGE_URL, imageUrl);
				values.put(BashgidSqliteHelper.COLUMN_NAME, albumName);

				if (!innerPhotoAlbumRelationAlreadyExist(imageUrl, albumName, database))
				{
					database.insert(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, null, values);
				}

			} finally
			{
				dbHelper.close();
			}
		}
	}

	public AlbumFullData GetAlbumFullData(String name)
	{
		AlbumFullData result = null;

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{

				result = InnerGetAlbumFullData(name, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public AlbumFullData InnerGetAlbumFullData(String name, SQLiteDatabase database)
	{

		AlbumFullData result = null;

		AlbumRecordData recordData = innerGetAlbumByName(name, database);

		if (recordData != null)
		{

			result = new AlbumFullData(recordData);

			ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);

			for (String imageUrl : imageUrlArr)
			{
				result.photoRecordArr.add(MainActivity.getInstance().photoDataSource.InnerGetPhotoByImageUrl(imageUrl, database));
			}
			
			result.title = MainActivity.getInstance().translationDataSource.TryTranslateText(result.title, database);
			result.description = MainActivity.getInstance().translationDataSource.TryTranslateText(result.description, database);

		}

		return result;
	}

	public AlbumShortData GetAlbumShortData(String name)
	{
		AlbumShortData result = null;

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{

				AlbumRecordData recordData = innerGetAlbumByName(name, database);

				if (recordData != null)
				{

					result = new AlbumShortData(recordData);

					ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);

					result.photoCount = imageUrlArr.size();

					if (imageUrlArr.size() > 0)
					{
						result.firstImageUrl = imageUrlArr.get(0);
					}
					
					result.title = MainActivity.getInstance().translationDataSource.TryTranslateText(result.title, database);
					result.description = MainActivity.getInstance().translationDataSource.TryTranslateText(result.description, database);

				}

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public ArrayList<AlbumShortData> GetAllAlbumShortDataExceptHidden()
	{

		ArrayList<AlbumShortData> result = new ArrayList<AlbumShortData>();

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{
				Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME,
						BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_HIDDEN }, BashgidSqliteHelper.COLUMN_HIDDEN + "=0", null, null, null, null, null);

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

							AlbumShortData shortData = new AlbumShortData(new AlbumRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3)));

							ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(shortData.name, database);

							shortData.photoCount = imageUrlArr.size();

							if (imageUrlArr.size() > 0)
							{
								shortData.firstImageUrl = imageUrlArr.get(0);
							}

							result.add(shortData);
						} while (cursor.moveToNext());

					}
					
					cursor.close();
					cursor = null;
				}

			} finally
			{
				dbHelper.close();
			}

		}

		return result;
	}

	private boolean innerIsAlbumAlreadyExist(String name, SQLiteDatabase database)
	{
		boolean result = false;
		
		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME }, BashgidSqliteHelper.COLUMN_NAME
				+ "=?", new String[] { name }, null, null, null, null);

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

		return result;

	}

	private boolean innerPhotoAlbumRelationAlreadyExist(String imageUrl, String name, SQLiteDatabase database)
	{
		boolean result = false;
		
		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL,
				BashgidSqliteHelper.COLUMN_NAME }, BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?" + " AND " + BashgidSqliteHelper.COLUMN_NAME + "=?",
				new String[] { imageUrl, name }, null, null, null, null);

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

		return result;

	}

	private ArrayList<String> innerGetImageUrlArrInAlbum(String name, SQLiteDatabase database)
	{
		ArrayList<String> result = new ArrayList<String>();

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

		if (cursor != null)
		{
			if (cursor.moveToFirst())
			{
				do
				{
					result.add(cursor.getString(0));
				} while (cursor.moveToNext());
			}
			
			cursor.close();
			cursor = null;
		}

		return result;

	}

	public AlbumRecordData innerGetAlbumByName(String name, SQLiteDatabase database)
	{
		
		AlbumRecordData result = null;

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME, BashgidSqliteHelper.COLUMN_TITLE,
				BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_HIDDEN }, BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);

		if (cursor != null)
		{
			if (cursor.moveToFirst())
			{
				result = new AlbumRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3));
			}
			
			cursor.close();
			cursor = null;
		}

		return result;

	}

}
