package fishrungames.bashgid.core.db;

import java.util.ArrayList;

import fishrungames.bashgid.MainActivity;

import fishrungames.bashgid.core.ChannelManager;
import fishrungames.bashgid.core.ChannelManager.ChannelFullData;
import fishrungames.bashgid.core.ChannelManager.ChannelRecordData;

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

import android.database.sqlite.SQLiteDatabase;

public class ChannelDataSource
{

	private BashgidSqliteHelper dbHelper = null;

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

	}

	public void CreateNewChannel(ChannelManager.ChannelRecordData 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 (!innerIsChannelAlreadyExist(recordData.name, database))
				{
					database.insert(BashgidSqliteHelper.TABLE_CHANNEL, null, values);
				} else
				{
					database.update(BashgidSqliteHelper.TABLE_CHANNEL, values, BashgidSqliteHelper.COLUMN_NAME + " = " + "\"" + recordData.name + "\"", null);
				}

			} finally
			{
				dbHelper.close();
			}
		}

	}

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

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				result = innerIsChannelAlreadyExist(name, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;

	}

	public void AddVideoToChannel(String videoUrl, String channelName)
	{
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{

			try
			{
				ContentValues values = new ContentValues();

				values.put(BashgidSqliteHelper.COLUMN_VIDEO_URL, videoUrl);
				values.put(BashgidSqliteHelper.COLUMN_NAME, channelName);

				if (!innerVideoChannelRelationAlreadyExist(videoUrl, channelName, database))
				{
					database.insert(BashgidSqliteHelper.TABLE_VIDEO_CHANNEL_RELATION, null, values);
				}

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

	public ChannelFullData GetChannelFullData(String name)
	{
		ChannelFullData result = null;

		SQLiteDatabase database = dbHelper.getWritableDatabase();

		if (database != null)
		{
			try
			{

				result = InnerGetChannelFullData(name, database);

			} finally
			{
				dbHelper.close();
			}
		}

		return result;
	}

	public ChannelFullData InnerGetChannelFullData(String name, SQLiteDatabase database)
	{

		ChannelFullData result = null;

		ChannelRecordData recordData = InnerGetChannelByName(name, database);

		if (recordData != null)
		{

			result = new ChannelFullData(recordData);

			ArrayList<String> videoUrlArr = innerGetVideoUrlArrInAlbum(name, database);

			for (String videoUrl : videoUrlArr)
			{
				result.videoRecordArr.add(MainActivity.getInstance().videoDataSource.InnerGetVideoByVideoUrl(videoUrl, database));
			}

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

		}

		return result;
	}

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

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_CHANNEL, 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 innerVideoChannelRelationAlreadyExist(String videoUrl, String name, SQLiteDatabase database)
	{
		boolean result = false;

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

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

			cursor.close();
			cursor = null;
		}

		return result;

	}

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

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_VIDEO_CHANNEL_RELATION, new String[] { BashgidSqliteHelper.COLUMN_VIDEO_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 ChannelRecordData InnerGetChannelByName(String name, SQLiteDatabase database)
	{

		ChannelRecordData result = null;

		Cursor cursor = database.query(BashgidSqliteHelper.TABLE_CHANNEL, 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 ChannelRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3));

			}

			cursor.close();
			cursor = null;
		}

		return result;

	}

}
