package fishrungames.bashgid.core.db;

import java.util.ArrayList;
import java.util.Collections;

import fishrungames.bashgid.MainActivity;

import fishrungames.bashgid.core.ArticleManager;
import fishrungames.bashgid.core.ArticleManager.ArticleShortData;
import fishrungames.bashgid.core.JournalManager;
import fishrungames.bashgid.core.JournalManager.JournalRecordData;
import fishrungames.bashgid.core.JournalManager.JournalSemiFullData;

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

import android.database.sqlite.SQLiteDatabase;

public class JournalDataSource
{

	private BashgidSqliteHelper dbHelper = null;

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

	}

	public void CreateNewJournal(JournalManager.JournalRecordData 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);
				values.put(BashgidSqliteHelper.COLUMN_GEOCATEGORY, recordData.geoCategory);
				values.put(BashgidSqliteHelper.COLUMN_MAIN, recordData.main);

				if (!innerIsJournalAlreadyExist(recordData.name, database))
				{
					database.insert(BashgidSqliteHelper.TABLE_JOURNAL, null, values);
				} else
				{
					// Replace
					database.update(BashgidSqliteHelper.TABLE_JOURNAL, values, BashgidSqliteHelper.COLUMN_NAME + " = " + "\"" + recordData.name + "\"", null);
				}

			} finally
			{
				dbHelper.close();
			}
		}

	}

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

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				result = innerIsJournalAlreadyExist(name, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;

	}

	public void AddArticleToJournal(String articleName, String journalName)
	{
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				ContentValues values = new ContentValues();

				values.put(BashgidSqliteHelper.COLUMN_ARTICLE_NAME, articleName);
				values.put(BashgidSqliteHelper.COLUMN_JOURNAL_NAME, journalName);

				if (!InnerArticleJournalRelationAlreadyExist(articleName, journalName, database))
				{

					database.insert(BashgidSqliteHelper.TABLE_ARTICLE_JOURNAL_RELATION, null, values);

				}

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

	public JournalSemiFullData GetJournalSemiFullData(String name)
	{
		return GetJournalSemiFullData(name, null, 0);
	}

	public JournalSemiFullData GetJournalSemiFullData(String name, int count)
	{
		return GetJournalSemiFullData(name, null, count);
	}

	public JournalSemiFullData GetJournalSemiFullData(String name, String startAfterArticleName, int count)
	{
		JournalSemiFullData result = null;

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{

				result = InnerGetJournalSemiFullData(name, database, startAfterArticleName, count);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public ArrayList<JournalSemiFullData> GetAllJournalListExceptHidden(int count)
	{
		ArrayList<JournalSemiFullData> result = new ArrayList<JournalSemiFullData>();
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{

				Cursor cursor = database.query(BashgidSqliteHelper.TABLE_JOURNAL, new String[] { BashgidSqliteHelper.COLUMN_NAME,
						BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_HIDDEN,
						BashgidSqliteHelper.COLUMN_GEOCATEGORY, BashgidSqliteHelper.COLUMN_MAIN }, BashgidSqliteHelper.COLUMN_HIDDEN + "=0", null, null, null,
						null, null);

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

						do

						{

							JournalSemiFullData journal = new JournalSemiFullData(new JournalRecordData(cursor.getString(0), cursor.getString(1),
									cursor.getString(2), cursor.getInt(3), cursor.getInt(4), cursor.getInt(5)));

							ArrayList<String> articleArr;

							if (count > 0)
							{
								articleArr = innerGetArticleArrInJournal(cursor.getString(0), database, count);
							} else
							{
								articleArr = innerGetArticleArrInJournal(cursor.getString(0), database);
							}
							for (String article : articleArr)
							{
								if (!MainActivity.getInstance().articleDataSource.InnerArticleIsHidden(article, database))
								{
									journal.articleArr.add(MainActivity.getInstance().articleDataSource.InnerGetArticleShortData(article, database));
								}
							}

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

							result.add(journal);

						} while (cursor.moveToNext());

					}

					cursor.close();
					cursor = null;
				}
			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public ArrayList<JournalSemiFullData> GetJournalListMain(int count)
	{
		// Xperimental - need to create a real superjournal

		ArrayList<JournalSemiFullData> result = new ArrayList<JournalSemiFullData>();
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{

				Cursor cursor = database.query(BashgidSqliteHelper.TABLE_JOURNAL, new String[] { BashgidSqliteHelper.COLUMN_NAME,
						BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_HIDDEN,
						BashgidSqliteHelper.COLUMN_GEOCATEGORY, BashgidSqliteHelper.COLUMN_MAIN }, BashgidSqliteHelper.COLUMN_MAIN + "=1", null, null, null,
						null, null);

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

						do

						{

							JournalSemiFullData journal = new JournalSemiFullData(new JournalRecordData(cursor.getString(0), cursor.getString(1),
									cursor.getString(2), cursor.getInt(3), cursor.getInt(4), cursor.getInt(5)));

							ArrayList<String> articleArr;

							if (count > 0)
							{
								articleArr = innerGetArticleArrInJournal(cursor.getString(0), database, count);
							} else
							{
								articleArr = innerGetArticleArrInJournal(cursor.getString(0), database);
							}

							for (String article : articleArr)
							{
								if (!MainActivity.getInstance().articleDataSource.InnerArticleIsHidden(article, database))
								{
									journal.articleArr.add(MainActivity.getInstance().articleDataSource.InnerGetArticleShortData(article, database));
								}
							}

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

							result.add(journal);

						} while (cursor.moveToNext());

					}

					cursor.close();
					cursor = null;
				}
			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public ArrayList<JournalRecordData> GetGeoJournalList()
	{
		ArrayList<JournalRecordData> result = new ArrayList<JournalRecordData>();
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{

				Cursor cursor = database.query(BashgidSqliteHelper.TABLE_JOURNAL, new String[] { BashgidSqliteHelper.COLUMN_NAME,
						BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_HIDDEN,
						BashgidSqliteHelper.COLUMN_GEOCATEGORY, BashgidSqliteHelper.COLUMN_MAIN }, BashgidSqliteHelper.COLUMN_GEOCATEGORY + "=1", null, null,
						null, null, null);

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

						do

						{
							JournalRecordData journal = new JournalRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3),
									cursor.getInt(4), cursor.getInt(5));

							journal.title = MainActivity.getInstance().translationDataSource.TryTranslateText(journal.title, database);

							// journal.description =
							// MainActivity.getInstance().translationDataSource.TryTranslateText(journal.description,
							// database);

							result.add(journal);

						} while (cursor.moveToNext());

					}

					cursor.close();
					cursor = null;
				}
			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public JournalSemiFullData InnerGetJournalSemiFullData(String name, SQLiteDatabase database, String startAfterArticleName, int count)
	{

		JournalSemiFullData result = null;

		JournalRecordData recordData = innerGetJournalByName(name, database);

		if (recordData != null)
		{

			result = new JournalSemiFullData(recordData);

			ArrayList<String> articleArr = innerGetArticleArrInJournal(name, database);

			ArrayList<ArticleShortData> intermediateArticleArr = new ArrayList<ArticleShortData>();

			for (String article : articleArr)
			{

				intermediateArticleArr.add(MainActivity.getInstance().articleDataSource.InnerGetArticleShortData(article, database));

			}

			Collections.sort(intermediateArticleArr, new ArticleManager.ArticleShortDataTimeSortComparator());

			boolean articleCanGo = false;

			if (startAfterArticleName == null || startAfterArticleName.length() == 0)
			{
				articleCanGo = true;
			}

			int i = 0;

			for (ArticleShortData article : intermediateArticleArr)
			{
				if (!articleCanGo)
				{
					if (article.name.equals(startAfterArticleName))
					{
						articleCanGo = true;
					}
				} else
				{
					if (count == 0 || i < count)
					{
						result.articleArr.add(article);
						i++;
					}

				}
			}

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

		}

		return result;
	}

	private boolean innerIsJournalAlreadyExist(String name, SQLiteDatabase database)
	{
		boolean result = false;

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_JOURNAL, 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;

	}

	public boolean InnerArticleJournalRelationAlreadyExist(String articleName, String journalName, SQLiteDatabase database)
	{
		boolean result = false;

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ARTICLE_JOURNAL_RELATION, new String[] { BashgidSqliteHelper.COLUMN_ARTICLE_NAME,
				BashgidSqliteHelper.COLUMN_JOURNAL_NAME }, BashgidSqliteHelper.COLUMN_ARTICLE_NAME + "=?" + " AND " + BashgidSqliteHelper.COLUMN_JOURNAL_NAME
				+ "=?", new String[] { articleName, journalName }, null, null, null, null);

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

			cursor.close();
			cursor = null;
		}

		return result;

	}

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

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ARTICLE_JOURNAL_RELATION, new String[] { BashgidSqliteHelper.COLUMN_ARTICLE_NAME },
				BashgidSqliteHelper.COLUMN_JOURNAL_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;

	}

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

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ARTICLE_JOURNAL_RELATION, new String[] { BashgidSqliteHelper.COLUMN_ARTICLE_NAME },
				BashgidSqliteHelper.COLUMN_JOURNAL_NAME + "=?", new String[] { name }, null, null, null, null);

		if (cursor != null)
		{
			if (cursor.moveToFirst())
			{
				int i = 0;
				do
				{
					result.add(cursor.getString(0));
					i++;
				} while (cursor.moveToNext() && i < count);

			}

			cursor.close();
			cursor = null;
		}

		return result;

	}

	public JournalRecordData innerGetJournalByName(String name, SQLiteDatabase database)
	{

		JournalRecordData result = null;

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

		if (cursor != null)
		{
			if (cursor.getCount() > 0)
			{
				cursor.moveToFirst();

				result = new JournalRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3), cursor.getInt(4),
						cursor.getInt(5));
			}
			cursor.close();
			cursor = null;
		}

		return result;

	}

}
