package fishrungames.bashgid.core.db;

import java.util.concurrent.Semaphore;

import android.content.Context;

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

public class BashgidSqliteHelper extends SQLiteOpenHelper
{

	// Newstyle
	public static final String TABLE_PHOTO = "table_photo";
	public static final String TABLE_ALBUM = "table_album";
	public static final String TABLE_PHOTO_ALBUM_RELATION = "table_photo_album_relation";
	
	public static final String TABLE_ARTICLE = "table_article";
	
	public static final String TABLE_ALBUM_ARTICLE_RELATION = "table_album_article_relation";
	public static final String TABLE_CHANNEL_ARTICLE_RELATION = "table_channel_article_relation";
	
	public static final String TABLE_VIDEO = "table_video";
	
	public static final String TABLE_CHANNEL = "table_channel";
	public static final String TABLE_VIDEO_CHANNEL_RELATION = "table_video_channel_relation";
	
	
	public static final String TABLE_JOURNAL = "table_journal";
	public static final String TABLE_ARTICLE_JOURNAL_RELATION = "table_article_journal_relation";
	
	public static final String TABLE_TRANSLATION = "table_translation";

	public static final String COLUMN_ID = "id";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_TITLE = "title";
	public static final String COLUMN_DESCRIPTION = "description";
	public static final String COLUMN_IMAGE_URL = "imageUrl";
	public static final String COLUMN_IMAGE_HASH = "imageHash";
	public static final String COLUMN_GEOLAT = "geoLat";
	public static final String COLUMN_GEOLON = "geoLon";
	public static final String COLUMN_ADDRESS = "address";
	public static final String COLUMN_PHONE = "phone";

	
	public static final String COLUMN_TYPE = "type";
	public static final String COLUMN_CONTENT = "content";
	public static final String COLUMN_EXTERNAL_LINK = "externalLink";
	public static final String COLUMN_EXTERNAL_LINK_TEXT = "externalLinkText";

	public static final String COLUMN_ALBUM_NAME = "albumName";
	public static final String COLUMN_CHANNEL_NAME = "channelName";
	public static final String COLUMN_ARTICLE_NAME = "articleName";
	public static final String COLUMN_JOURNAL_NAME = "journalName";
	
	public static final String COLUMN_VIDEO_URL = "videoUrl";
	public static final String COLUMN_PREVIEW_IMAGE_URL = "previewImageUrl";
	public static final String COLUMN_PREVIEW_IMAGE_HASH = "previewImageHash";
	
	public static final String COLUMN_HIDDEN = "hidden";
	public static final String COLUMN_GEOCATEGORY = "geoCategory";
	public static final String COLUMN_MAIN = "main";
	
	
	public static final String COLUMN_DATE_TIME = "dateTime";
	
	public static final String COLUMN_TEXT_EN = "textEn";
	public static final String COLUMN_TEXT_RU = "textRu";
	public static final String COLUMN_TEXT_ZH = "textZh";
	
	public static final String COLUMN_TEXT_EN_UP = "textEnUp";
	public static final String COLUMN_TEXT_RU_UP = "textRuUp";
	public static final String COLUMN_TEXT_ZH_UP = "textZhUp";
	
	private static final String DATABASE_NAME = "bashgid.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement


	
	private static final String TABLE_PHOTO_CREATE = "create table " + TABLE_PHOTO + "(" + COLUMN_ID + " integer primary key autoincrement"
			+ ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_IMAGE_URL + " text not null" + ", " + COLUMN_IMAGE_HASH + " text not null" + ", " + COLUMN_GEOLAT
			+ " real not null" + ", " + COLUMN_GEOLON + " real not null" + ");";
	
	private static final String TABLE_ALBUM_CREATE = "create table " + TABLE_ALBUM + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
			+ ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_HIDDEN + " integer not null" + ");";
	
	private static final String TABLE_PHOTO_ALBUM_RELATION_CREATE = "create table " + TABLE_PHOTO_ALBUM_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_IMAGE_URL + " text not null"
			+ ", " + COLUMN_NAME + " text not null" + ");";
	
	
	private static final String TABLE_ARTICLE_CREATE = "create table " + TABLE_ARTICLE + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
			+ ", " + COLUMN_TYPE + " integer not null" + ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_CONTENT + " text not null"  + ", " + COLUMN_GEOLAT
			+ " real not null" + ", " + COLUMN_GEOLON + " real not null" + ", "
			+ COLUMN_ADDRESS + " text not null" + ", " 
			+ COLUMN_PHONE + " text not null" + ", " 
			+ COLUMN_EXTERNAL_LINK + " text not null" + ", "
			+ COLUMN_EXTERNAL_LINK_TEXT + " text not null" + ", "+ COLUMN_DATE_TIME + " text not null" + ", " + COLUMN_HIDDEN + " integer not null" +");";
	
	private static final String TABLE_ALBUM_ARTICLE_RELATION_CREATE = "create table " + TABLE_ALBUM_ARTICLE_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_ALBUM_NAME + " text not null"
			+ ", " + COLUMN_ARTICLE_NAME + " text not null" + ");";
	
	private static final String TABLE_CHANNEL_ARTICLE_RELATION_CREATE = "create table " + TABLE_CHANNEL_ARTICLE_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_CHANNEL_NAME + " text not null"
			+ ", " + COLUMN_ARTICLE_NAME + " text not null" + ");";
	
	
	private static final String TABLE_VIDEO_CREATE = "create table " + TABLE_VIDEO + "(" + COLUMN_ID + " integer primary key autoincrement"+ ", " + COLUMN_TYPE + " integer not null"
			 + ", " + COLUMN_VIDEO_URL + " text not null" + ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_PREVIEW_IMAGE_URL + " text not null"
			 + ", " + COLUMN_PREVIEW_IMAGE_HASH + " text not null"+ ");";
	
	private static final String TABLE_CHANNEL_CREATE = "create table " + TABLE_CHANNEL + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
			+ ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_HIDDEN + " integer not null" + ");";
	
	private static final String TABLE_VIDEO_CHANNEL_RELATION_CREATE = "create table " + TABLE_VIDEO_CHANNEL_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_VIDEO_URL + " text not null"
			+ ", " + COLUMN_NAME + " text not null" + ");";
	
	private static final String TABLE_JOURNAL_CREATE = "create table " + TABLE_JOURNAL + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
			+ ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_HIDDEN + " integer not null" + ", " + COLUMN_GEOCATEGORY + " integer not null" + ", " + COLUMN_MAIN + " integer not null" + ");";
	
	private static final String TABLE_ARTICLE_JOURNAL_RELATION_CREATE = "create table " + TABLE_ARTICLE_JOURNAL_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_ARTICLE_NAME + " text not null"
			+ ", " + COLUMN_JOURNAL_NAME + " text not null" + ");";
	
	private static final String TABLE_TRANSLATION_CREATE = "create table " + TABLE_TRANSLATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
			+ ", " + COLUMN_TEXT_EN + " text not null"+ ", " + COLUMN_TEXT_RU + " text not null" + ", " + COLUMN_TEXT_ZH + " text not null" 
		    + ", " + COLUMN_TEXT_EN_UP + " text not null"+ ", " + COLUMN_TEXT_RU_UP + " text not null" + ", " + COLUMN_TEXT_ZH_UP + " text not null" + ");";
	
	public final Semaphore mutex = new Semaphore(1, true);

	public BashgidSqliteHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database)
	{
		database.execSQL(TABLE_PHOTO_CREATE);
		database.execSQL(TABLE_ALBUM_CREATE);
		database.execSQL(TABLE_PHOTO_ALBUM_RELATION_CREATE);
		database.execSQL(TABLE_ARTICLE_CREATE);
		database.execSQL(TABLE_ALBUM_ARTICLE_RELATION_CREATE);
		database.execSQL(TABLE_CHANNEL_ARTICLE_RELATION_CREATE);
		
		database.execSQL(TABLE_VIDEO_CREATE);
		database.execSQL(TABLE_CHANNEL_CREATE);
		database.execSQL(TABLE_VIDEO_CHANNEL_RELATION_CREATE);
		
		database.execSQL(TABLE_JOURNAL_CREATE);
		database.execSQL(TABLE_ARTICLE_JOURNAL_RELATION_CREATE);
		database.execSQL(TABLE_TRANSLATION_CREATE);
		
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		//Log.w(BashgidSqliteHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHOTO);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALBUM);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHOTO_ALBUM_RELATION);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ARTICLE);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALBUM_ARTICLE_RELATION);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHANNEL_ARTICLE_RELATION);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEO);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHANNEL);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEO_CHANNEL_RELATION);
		
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_JOURNAL);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ARTICLE_JOURNAL_RELATION);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRANSLATION);
		
		onCreate(db);
	}
	
	
	@Override
	public SQLiteDatabase getWritableDatabase()
	{
		SQLiteDatabase result = null;
		
		try
		{
			mutex.acquire();
			try
			{
				result = super.getWritableDatabase();
				
			} catch (Exception e)
			{
				mutex.release();
			}
		} catch (InterruptedException ie)
		{
			Log.e("Error in getWritableDatabase()", "Error in getWritableDatabase()");
		}
		
		return result;
		
	}
	
	@Override
	public void close()
	{
		super.close();
		mutex.release();
	}
	
	
	
}
