package fishrungames.bashgid.core.db;

import java.util.ArrayList;
import java.util.Locale;

import fishrungames.bashgid.core.TranslationManager.TranslationRecordData;

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

import android.database.sqlite.SQLiteDatabase;

public class TranslationDataSource
{

	private BashgidSqliteHelper dbHelper = null;

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

	}

	public void CreateNewTranslation(TranslationRecordData recordData)
	{
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				ContentValues values = new ContentValues();

				values.put(BashgidSqliteHelper.COLUMN_NAME, recordData.name);
				values.put(BashgidSqliteHelper.COLUMN_TEXT_EN, recordData.textEn);
				values.put(BashgidSqliteHelper.COLUMN_TEXT_RU, recordData.textRu);
				values.put(BashgidSqliteHelper.COLUMN_TEXT_ZH, recordData.textZh);
				values.put(BashgidSqliteHelper.COLUMN_TEXT_EN_UP, recordData.textEnUp);
				values.put(BashgidSqliteHelper.COLUMN_TEXT_RU_UP, recordData.textRuUp);
				values.put(BashgidSqliteHelper.COLUMN_TEXT_ZH_UP, recordData.textZhUp);

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

			} finally
			{
				dbHelper.close();
			}
		}

	}

	public String TryTranslateText(String name, SQLiteDatabase database)
	{
		if (innerIsTranslationAlreadyExist(name, database))
		{
			TranslationRecordData record = innerGetTranslationByName(name, database);

			if (Locale.getDefault().equals(new Locale("ru")))
			{
				return record.textRu;
			} else if (Locale.getDefault() == Locale.CHINA)
			{
				return record.textZh;
			}

			return record.textEn;
		} else
		{
			return name;
		}
	}

	public ArrayList<String> SearchRussian(String partString)
	{
		ArrayList<String> result = new ArrayList<String>();

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				result = innerGetTranslationNameByPartString_Russian(partString, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public ArrayList<String> SearchEnglish(String partString)
	{
		ArrayList<String> result = new ArrayList<String>();

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				result = innerGetTranslationNameByPartString_English(partString, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public ArrayList<String> SearchChinese(String partString)
	{
		ArrayList<String> result = new ArrayList<String>();

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				result = innerGetTranslationNameByPartString_Chinese(partString, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public TranslationRecordData GetTranslationRecordData(String name)
	{
		TranslationRecordData result = null;

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{

				result = innerGetTranslationByName(name, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

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

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_TRANSLATION, 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 TranslationRecordData innerGetTranslationByName(String name, SQLiteDatabase database)
	{

		TranslationRecordData result = null;

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_TRANSLATION, new String[] { BashgidSqliteHelper.COLUMN_NAME,
				BashgidSqliteHelper.COLUMN_TEXT_EN, BashgidSqliteHelper.COLUMN_TEXT_RU, BashgidSqliteHelper.COLUMN_TEXT_ZH,
				BashgidSqliteHelper.COLUMN_TEXT_EN_UP, BashgidSqliteHelper.COLUMN_TEXT_RU_UP, BashgidSqliteHelper.COLUMN_TEXT_ZH_UP },
				BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);

		if (cursor != null)
		{
			if (cursor.moveToFirst())
			{
				result = new TranslationRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
						cursor.getString(5), cursor.getString(6));
			}

			cursor.close();
			cursor = null;
		}

		return result;

	}

	public ArrayList<String> innerGetTranslationNameByPartString_Russian(String partString, SQLiteDatabase database)
	{

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

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_TRANSLATION, new String[] { BashgidSqliteHelper.COLUMN_NAME },
				BashgidSqliteHelper.COLUMN_TEXT_RU_UP + " LIKE ?", new String[] { "%" + partString + "%" }, 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 ArrayList<String> innerGetTranslationNameByPartString_English(String partString, SQLiteDatabase database)
	{

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

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_TRANSLATION, new String[] { BashgidSqliteHelper.COLUMN_NAME },
				BashgidSqliteHelper.COLUMN_TEXT_EN_UP + " LIKE ?", new String[] { "%" + partString + "%" }, 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 ArrayList<String> innerGetTranslationNameByPartString_Chinese(String partString, SQLiteDatabase database)
	{

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

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_TRANSLATION, new String[] { BashgidSqliteHelper.COLUMN_NAME },
				BashgidSqliteHelper.COLUMN_TEXT_ZH_UP + " LIKE ?", new String[] { "%" + partString + "%" }, 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;

	}

}
