package athary.audio.ency.Helpers.data;
 
import info.guardianproject.database.sqlcipher.SQLiteDatabase;
import info.guardianproject.database.sqlcipher.SQLiteOpenHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
 
public class NewDataDbAdapter {
	
 //Tables Definitions (7 Tables)
public static final String TBL_VER = "Vertable";
public static final String TBL_SHAIKHS = "Sheekh";
private static final String TBL_BOOKS = "Book";
private static final String TBL_CATEGORY = "Category";
private static final String TBL_CHAPTERS = "Chapters";
private static final String TBL_CONTENTS = "Contents";
private static final String TBL_CONTENTCAT = "ContentCat";

//All Tables Fields Definitions
public static final String KEY_ROWID = "_id";
public static final String KEY_SHEEKH_ID = "Sheekh_id";
public static final String KEY_BOOK_ID = "Book_id";
public static final String KEY_CATEGORY_ID = "Category_id";
public static final String KEY_CODE = "Code";
public static final String KEY_SHEEKH_NAME = "Sheekh_name";
public static final String KEY_BOOK_NAME = "Book_name";
public static final String KEY_SELECTION = "Selection";
public static final String KEY_CATEGORY_NAME = "Category_name";
public static final String KEY_CATEGORY_PARENT = "Category_parent";
public static final String KEY_TITLE = "Title";
public static final String KEY_FILETYPE = "filetype";
public static final String KEY_FILENAME= "FileName";
public static final String KEY_PATH= "Path";
public static final String KEY_SEQ = "Seq";
public static final String KEY_TAFREEG = "Tafreeg";
public static final String KEY_OFFSET = "Offset";
public static final String KEY_DURATION = "duration";
public static final String KEY_LINE = "Line";
public static final String KEY_FAV = "fav";
public static final String KEY_VER = "ver";

//Define Other Variables
public static String count;
public static String PATH;
private static NewDataDbAdapter instance;
private static final int DATABASE_VERSION = 1;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;

//------------------Sheekh Table Columns-----------------------------------------------
private static final String TBL_VER_CREATE = 
		  "create table if not exists " + TBL_VER + " (" +
		  KEY_ROWID + " integer PRIMARY KEY autoincrement," +
		  KEY_VER + " integer," +
		  " UNIQUE (" + KEY_ROWID +"));";
//-------------------------------------------------------------------------------------

//------------------Sheekh Table Columns-----------------------------------------------
private static final String TBL_SHAIKHS_CREATE = 
		  "CREATE VIRTUAL TABLE " + TBL_SHAIKHS + " USING fts3(" +
		  KEY_ROWID + " integer PRIMARY KEY autoincrement," +
		  KEY_SHEEKH_ID + " integer," +
		  KEY_SHEEKH_NAME + "," +
		  KEY_SELECTION + " integer," +
		  " UNIQUE (" + KEY_ROWID +"));";
//-------------------------------------------------------------------------------------

//------------------Book Table Columns-------------------------------------------------
private static final String TBL_BOOKS_CREATE = 
		  "CREATE VIRTUAL TABLE " + TBL_BOOKS + " USING fts3(" +
		  KEY_ROWID + " integer PRIMARY KEY autoincrement," +
		  KEY_SHEEKH_ID + " integer," +
		  KEY_BOOK_ID + " integer," +
		  KEY_BOOK_NAME + "," +
		  KEY_SELECTION + " integer," +
		  " UNIQUE (" + KEY_ROWID +"));";
//-------------------------------------------------------------------------------------

//------------------Category Table Columns---------------------------------------------
private static final String TBL_CATEGORY_CREATE = 
		  "CREATE VIRTUAL TABLE " + TBL_CATEGORY + " USING fts3(" +
		  KEY_ROWID + " integer PRIMARY KEY autoincrement," +
		  KEY_CATEGORY_ID + " integer," +
		  KEY_CATEGORY_NAME + "," +
		  KEY_CATEGORY_PARENT + " integer," +
		  " UNIQUE (" + KEY_ROWID +"));";
//-------------------------------------------------------------------------------------

//------------------Chapters Table Columns---------------------------------------------
private static final String TBL_CHAPTERS_CREATE = 
		  "CREATE VIRTUAL TABLE " + TBL_CHAPTERS + " USING fts3(" +
		  KEY_ROWID + " integer PRIMARY KEY autoincrement," +
		  KEY_CODE + " integer," +
		  KEY_SHEEKH_ID + " integer," +
		  KEY_BOOK_ID + " integer," +
		  KEY_TITLE + "," +
		  KEY_FILENAME + "," +
		  KEY_FILETYPE + "," +
		  KEY_PATH + "," +
		  " UNIQUE (" + KEY_ROWID +"));";
//-------------------------------------------------------------------------------------

//------------------Contents Table Columns---------------------------------------------
private static final String TBL_CONTENTS_CREATE = 
		"CREATE VIRTUAL TABLE " + TBL_CONTENTS + " USING fts3(" +
		KEY_ROWID + " integer PRIMARY KEY autoincrement," +
		KEY_CODE + " integer," +
		KEY_SHEEKH_ID + " integer," +
		KEY_BOOK_ID + " integer," +
		KEY_SEQ + " integer," +
		KEY_OFFSET + "," +
		KEY_DURATION + "," +
		KEY_LINE + " TEXT," +
		KEY_TAFREEG + " TEXT," +
		KEY_FAV + " integer," +
		" UNIQUE (" + KEY_ROWID +"));";
//-------------------------------------------------------------------------------------

//------------------ContentCat Table Columns---------------------------------------------------------
private static final String TBL_CONTENTCATS_CREATE = 
	  "CREATE VIRTUAL TABLE " + TBL_CONTENTCAT + " USING fts3(" +
	  KEY_ROWID + " integer PRIMARY KEY autoincrement," +
	  KEY_CODE + " integer," +
	  KEY_SEQ + " integer," +
	  KEY_CATEGORY_ID + " integer," +
	  KEY_SHEEKH_ID + " integer," +
	  KEY_BOOK_ID + " integer," +
	  " UNIQUE (" + KEY_ROWID +"));";
//-------------------------------------------------------------------------------------

 private static class DatabaseHelper extends SQLiteOpenHelper {
 
DatabaseHelper(Context context) {
	super(context, PATH, null, DATABASE_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
	   db.execSQL(TBL_SHAIKHS_CREATE);
	   db.execSQL(TBL_BOOKS_CREATE);
	   db.execSQL(TBL_CATEGORY_CREATE);
	   db.execSQL(TBL_CHAPTERS_CREATE);
	   db.execSQL(TBL_CONTENTS_CREATE);
	   db.execSQL(TBL_CONTENTCATS_CREATE);
}
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	   db.execSQL("DROP TABLE IF EXISTS " + TBL_SHAIKHS);
	   db.execSQL("DROP TABLE IF EXISTS " + TBL_BOOKS);
	   db.execSQL("DROP TABLE IF EXISTS " + TBL_CATEGORY);
	   db.execSQL("DROP TABLE IF EXISTS " + TBL_CHAPTERS);
	   db.execSQL("DROP TABLE IF EXISTS " + TBL_CONTENTS);
	   db.execSQL("DROP TABLE IF EXISTS " + TBL_CONTENTCAT);

   onCreate(db);
  }
 }
 
 public NewDataDbAdapter(Context ctx) {
  this.mCtx = ctx;
 }

 public static synchronized NewDataDbAdapter getInstance (Context ctx) {
 	
 	if (instance == null) instance = new NewDataDbAdapter(ctx);
 	
 	return instance;
 }
 
 public NewDataDbAdapter open(String password) throws SQLException {
	 
     mDbHelper = new DatabaseHelper(mCtx);	
     mDb = mDbHelper.getWritableDatabase(password);
     //mDb.setICURoot("/mnt/sdcard/icudt46l.dat");
     //mDb.setLocale(new Locale("ar"));
     System.gc();

     return this;
 }

 public boolean isOpen ()
 {
 	if (mDb !=null)
 		return mDb.isOpen();
 	else
 		return false;
 }
  
 public void checkvertable(){
	 
	   mDb.execSQL(TBL_VER_CREATE); 

 }
 
 public int getver(){

	 int o  = mDb.getVersion();
	 return o;
 }
 
 public void rekey (String password)
 {	
	// mDb.execSQL("ATTACH DATABASE '" + path + "maindb.db' AS encrypted KEY '"+ password + "'");
	// mDb.execSQL("SELECT sqlcipher_export('encrypted')"); 
	// mDb.execSQL("DETACH DATABASE encrypted"); 
 
//	mDb.execSQL("PRAGMA KEY = ''");
	//mDb.execSQL("PRAGMA cipher = 'bf-cbc'");
	//mDb.execSQL("PRAGMA cipher_page_size = 4096");
 //	mDb.execSQL("PRAGMA rekey = '"+ password + "'");
 	System.gc();
 }
 
 public void VACUUM()
 {	mDb.execSQL("VACUUM");
 	System.gc();
 }

 public void close() {
  if (mDbHelper != null) {
   mDbHelper.close();
  }
 }
 
//#################### New Data Insertion ##############################################################################################################################################
 public long newver(Integer ver) {
	 ContentValues Value = new ContentValues();
	 Value.put(KEY_VER, ver);
 return mDb.insert(TBL_VER, null, Value);
}
public long newshaikh(Integer sheekhid, String sheekh_name, Integer Selection) {
	 ContentValues shaikhValues = new ContentValues();
	 shaikhValues.put(KEY_SHEEKH_ID, sheekhid);
	 shaikhValues.put(KEY_SHEEKH_NAME, sheekh_name); 
	 shaikhValues.put(KEY_SELECTION, Selection); 
 return mDb.insert(TBL_SHAIKHS, null, shaikhValues);
}

public long newbook(Integer sheekhid, Integer bookid,String book_name, Integer Selection) {
	 ContentValues bookValues = new ContentValues();
	 bookValues.put(KEY_SHEEKH_ID, sheekhid);
	 bookValues.put(KEY_BOOK_ID, bookid);
	 bookValues.put(KEY_BOOK_NAME, book_name);
	 bookValues.put(KEY_SELECTION, Selection); 
return mDb.insert(TBL_BOOKS, null, bookValues);
}

public long newCategory(Integer categoryid, String category_name, Integer category_parent) {
	 ContentValues CATValues = new ContentValues();
	 CATValues.put(KEY_CATEGORY_ID, categoryid);
	 CATValues.put(KEY_CATEGORY_NAME, category_name);
	 CATValues.put(KEY_CATEGORY_PARENT, category_parent);
return mDb.insert(TBL_CATEGORY, null, CATValues);
}

public long newChapter(Integer code, Integer sheekhid, Integer bookid, String title, String filename, String filetype, String path){
	 ContentValues CHAPValues = new ContentValues();
	 CHAPValues.put(KEY_CODE, code);
	 CHAPValues.put(KEY_SHEEKH_ID, sheekhid);
	 CHAPValues.put(KEY_BOOK_ID, bookid);
	 CHAPValues.put(KEY_TITLE, title);
	 CHAPValues.put(KEY_FILENAME, filename);
	 CHAPValues.put(KEY_FILETYPE, filetype);
	 CHAPValues.put(KEY_PATH, path);
	 return mDb.insert(TBL_CHAPTERS, null, CHAPValues);
	}

public long newContent(Integer code, Integer sheekhid, Integer bookid, Integer seq, String offset, String duration, String line, String tafreeg, Integer fav){
	 ContentValues CONValues = new ContentValues();
	 CONValues.put(KEY_CODE, code);
	 CONValues.put(KEY_SHEEKH_ID, sheekhid);
	 CONValues.put(KEY_BOOK_ID, bookid);
	 CONValues.put(KEY_SEQ, seq);
	 CONValues.put(KEY_OFFSET, offset);
	 CONValues.put(KEY_DURATION, duration);
	 CONValues.put(KEY_LINE, line);
	 CONValues.put(KEY_TAFREEG, tafreeg);
	 CONValues.put(KEY_FAV, fav);
	 return mDb.insert(TBL_CONTENTS, null, CONValues);
	}

public long newContentCat(Integer code, Integer seq, Integer categoryid, Integer sheekhid, Integer bookid){
	 ContentValues CATValues = new ContentValues();
	 CATValues.put(KEY_CODE, code);
	 CATValues.put(KEY_SEQ, seq);
	 CATValues.put(KEY_CATEGORY_ID, categoryid);
	 CATValues.put(KEY_SHEEKH_ID, sheekhid);
	 CATValues.put(KEY_BOOK_ID, bookid);
	 return mDb.insert(TBL_CONTENTCAT, null, CATValues);
	}
//########################################################################################################################################################################################

//#################### Updates Of Some Tables Data #######################################################################################################################################
public int Update_Sheekh_Selection(Integer val,Integer sheekhid){
	  ContentValues selectionval = new ContentValues();
	  selectionval.put(KEY_SELECTION, val);   
	  return mDb.update(TBL_SHAIKHS, selectionval, KEY_SHEEKH_ID+" MATCH '" + sheekhid + "'", null);
}

public int Update_Book_Selection(Integer val,Integer Sheekhid, Integer bookid){
	  ContentValues selectionval = new ContentValues();
	  selectionval.put(KEY_SELECTION, val);   
	  return mDb.update(TBL_BOOKS, selectionval, KEY_BOOK_ID+" MATCH '" + bookid + "' AND " + KEY_SHEEKH_ID+"=" + Sheekhid, null);
}

public int Updateby_All_Sheekhs_Selection(Integer val,Integer Selection){
	  ContentValues selectionval = new ContentValues();
	  selectionval.put(KEY_SELECTION, val);   
	  return mDb.update(TBL_SHAIKHS, selectionval, KEY_SELECTION+" MATCH '" + Selection + "'", null);
}

public int Updateby_All_Books_Selection(Integer val,Integer Selection){
	  ContentValues selectionval = new ContentValues();
	  selectionval.put(KEY_SELECTION, val);   
	  return mDb.update(TBL_BOOKS, selectionval, KEY_SELECTION+" MATCH '" + Selection + "'", null);
}

public int updatebyFAV(Integer code, Integer seq, Integer fav){
	  ContentValues favvalues = new ContentValues();
	  favvalues.put(KEY_FAV, fav);   
	  return mDb.update(TBL_CONTENTS, favvalues, KEY_CODE+" MATCH '"+code + "' AND " + KEY_SEQ+"="+seq, null);
}

public int updatebyAllFAV(){
	  ContentValues favvalues = new ContentValues();
	  favvalues.put(KEY_FAV, 0);   
	  return mDb.update(TBL_CONTENTS, favvalues, KEY_FAV+" MATCH '1'", null);
}
//########################################################################################################################################################################################

//#################### Get Maximum Of Some Values #######################################################################################################################################
public long getMax_Sheekh_Id()
{
    String query = "SELECT MAX(" + KEY_SHEEKH_ID + ") AS max_id FROM " + TBL_SHAIKHS;
    Cursor cursor = mDb.rawQuery(query, null);

    int id = 0;     
    if (cursor.moveToFirst())
    {
        do
        {           
            id = cursor.getInt(0);                  
        } while(cursor.moveToNext());           
    }
    return id;
}

public long getMax_Code()
{
    String query = "SELECT MAX(" + KEY_CODE + ") AS max_code FROM " + TBL_CHAPTERS;
    Cursor cursor = mDb.rawQuery(query, null);

    int id = 0;     
    if (cursor.moveToFirst())
    {
        do
        {           
            id = cursor.getInt(0);                  
        } while(cursor.moveToNext());           
    }
    return id;
}

//########################################################################################################################################################################################

//#################### Fetching All Data In Tables #######################################################################################################################################
public Cursor Allver() {Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TBL_VER , null); if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor AllSheekhs() {Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TBL_SHAIKHS , null); if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor AllBooks() {Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TBL_BOOKS , null); if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor AllCategories() {Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CATEGORY , null); if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor AllChapters() {Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CHAPTERS , null); if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor AllContents() {Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS , null); if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor AllContentCat() {Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTCAT , null); if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
//########################################################################################################################################################################################

//#################### Fetching Sheekh Table By Some Criteria ############################################################################################################################
public Cursor SheekhBy_SHEEKH_ID(Integer input) throws SQLException {Cursor mCursor = null; if (input == null)  {mCursor = null;} else	mCursor = mDb.rawQuery("SELECT * FROM " + TBL_SHAIKHS + " WHERE " + KEY_SHEEKH_ID + " MATCH '" + input + "'",null);	if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
//########################################################################################################################################################################################

//#################### Fetching All Tables For Sheekhs Fawzan & Al Rajhi Data ############################################################################################################################
public Cursor SheekhBy__Fawzan_Rajhi() throws SQLException {Cursor mCursor = null; mCursor = mDb.rawQuery("SELECT * FROM " + TBL_SHAIKHS + " WHERE " + KEY_SHEEKH_ID + "=9 OR "+ KEY_SHEEKH_ID + "=19",null);	if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
public Cursor BooksBy__Fawzan_Rajhi() throws SQLException {Cursor mCursor = null; mCursor = mDb.rawQuery("SELECT * FROM " + TBL_BOOKS + " WHERE " + KEY_SHEEKH_ID + "=9 OR "+ KEY_SHEEKH_ID + "=19",null);	if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
public Cursor ChaptersBy__Fawzan_Rajhi() throws SQLException {Cursor mCursor = null; mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CHAPTERS + " WHERE " + KEY_SHEEKH_ID + "=9 OR "+ KEY_SHEEKH_ID + "=19",null);	if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
public Cursor ContentshBy__Fawzan_Rajhi() throws SQLException {Cursor mCursor = null; mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE " + KEY_SHEEKH_ID + "=9 OR "+ KEY_SHEEKH_ID + "=19",null);	if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
public Cursor ContentCatBy__Fawzan_Rajhi() throws SQLException {Cursor mCursor = null; mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTCAT + " WHERE " + KEY_SHEEKH_ID + "=9 OR "+ KEY_SHEEKH_ID + "=19",null);	if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
//########################################################################################################################################################################################

//#################### Fetching Book Table By Some Criteria ##############################################################################################################################
public Cursor BooksBy_SHEEKH_ID(Integer input) throws SQLException {Cursor mCursor = null; if (input == null)  {mCursor = null;} else mCursor = mDb.rawQuery("SELECT * FROM " + TBL_BOOKS + " WHERE " + KEY_SHEEKH_ID + " MATCH '" + input + "'",null);	if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor BooksBy_BOOK_ID(Integer input) throws SQLException {Cursor mCursor = null; if (input == null)  {mCursor = null;}	else mCursor = mDb.rawQuery("SELECT * FROM " + TBL_BOOKS + " WHERE " + KEY_BOOK_ID + " MATCH '" + input + "'",null); if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor BooksBy_Selection_Cursor() throws SQLException {Cursor mCursor = null; mCursor = mDb.rawQuery("SELECT * FROM " + TBL_BOOKS + " WHERE " + KEY_SELECTION + " =1",null);	if (mCursor != null) {mCursor.moveToFirst();} return mCursor;} 

public String BooksBy_Selection() throws SQLException {
	Cursor mCursor = null;
	Cursor cCursor = null;

	String searchcri="";
	
	mCursor = mDb.rawQuery("SELECT " + KEY_SHEEKH_ID + "," + KEY_BOOK_ID + " FROM " + TBL_BOOKS + " WHERE " + KEY_SELECTION + " MATCH '1'",null);
	if (mCursor != null) {mCursor.moveToFirst();

	for (int i=0;i<mCursor.getCount();i++){
	 
	cCursor = mDb.rawQuery("SELECT Code FROM " + TBL_CHAPTERS + " WHERE " + KEY_SHEEKH_ID + " MATCH '" + mCursor.getInt(mCursor.getColumnIndex(KEY_SHEEKH_ID)) + "' AND " + KEY_BOOK_ID + "=" + mCursor.getInt(mCursor.getColumnIndex(KEY_BOOK_ID)) ,null);
	if (cCursor != null) {cCursor.moveToFirst();
	for (int ii = 0; ii < cCursor.getCount(); ii++) {
		searchcri = searchcri + cCursor.getString(cCursor.getColumnIndex(KEY_CODE)) + " ";
		cCursor.moveToNext();
	}
	}
	mCursor.moveToNext();
	}
	}

		
		return searchcri;
}
public String BooksBy_Codes(Integer SHEEKHID, Integer BOOKID) throws SQLException {
	Cursor cCursor = null;

	String searchcri="";

	 
	cCursor = mDb.rawQuery("SELECT Code FROM " + TBL_CHAPTERS + " WHERE " + KEY_SHEEKH_ID + " MATCH " + SHEEKHID + " AND " + KEY_BOOK_ID + "=" + BOOKID ,null);
	if (cCursor != null) {cCursor.moveToFirst();
	for (int ii = 0; ii < cCursor.getCount(); ii++) {
		searchcri = searchcri + cCursor.getString(cCursor.getColumnIndex(KEY_CODE)) + " ";
		cCursor.moveToNext();
	}
	}

		return searchcri;
}
//########################################################################################################################################################################################

//#################### Fetching Category Table By Some Criteria ##########################################################################################################################
public Cursor CategoryBy_CATEGORY_PARENT(Integer input) throws SQLException {Cursor mCursor = null;	if (input == null)  {mCursor = null;} else	mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CATEGORY + " WHERE " + KEY_CATEGORY_PARENT + "=" + input,null); if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
//########################################################################################################################################################################################

//#################### Fetching Chapters Table By Some Criteria ##########################################################################################################################
public Cursor ChaptersBy_BOOK_GROUP(Integer sheekhid, Integer bookid) throws SQLException {Cursor mCursor = null; if (sheekhid == null || bookid == null)  {mCursor = null;} else mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CHAPTERS + " WHERE " + KEY_SHEEKH_ID + "=" + sheekhid + " AND " +  KEY_BOOK_ID + "=" + bookid + " GROUP BY " + KEY_TITLE + " ORDER BY CAST(" + KEY_FILENAME + " as UNSIGNED INTEGER)," + KEY_FILENAME + " ASC",null); if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
public Cursor ChaptersBy_Title(String title, Integer sheekhid, Integer bookid) throws SQLException {Cursor mCursor = null; if (sheekhid == null || bookid == null)  {mCursor = null;} else	mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CHAPTERS + " WHERE " + KEY_TITLE + " like '%" + title + "%' AND " + KEY_SHEEKH_ID + "=" + sheekhid + " AND " + KEY_BOOK_ID + "=" + bookid + " ORDER BY CAST(" + KEY_FILENAME + " as UNSIGNED INTEGER)," + KEY_FILENAME + " ASC",null); if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
public Cursor ChaptersBy_CODE(Integer input) throws SQLException {Cursor mCursor = null; if (input == null)  {mCursor = null;} else	mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CHAPTERS + " WHERE " + KEY_CODE + " MATCH '" + input + "'",null);	if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
//########################################################################################################################################################################################

//#################### Fetching Contents Table By Some Criteria ##########################################################################################################################
public Cursor ContentsBy_CODE(Integer input) throws SQLException {Cursor mCursor = null; if (input == null)  {mCursor = null;} else	mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE " + KEY_CODE + " MATCH '" + input + "' ORDER BY CAST(" + KEY_SEQ + " as UNSIGNED INTEGER)," + KEY_SEQ + " ASC",null);	if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor ContentsBy_FAV(Integer inputfav) throws SQLException {Cursor mCursor = null; if (inputfav == null)  {mCursor = null;} else mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE " + KEY_FAV + " MATCH '" + inputfav + "'",null);	if (mCursor != null) {mCursor.moveToFirst();} return mCursor;}
public Cursor ContentsBy_CAT(Integer code, Integer seq) throws SQLException {Cursor mCursor = null; if (code == null || seq == null)  {mCursor = null;}	else mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE " + KEY_CODE + " MATCH '" + code + "' AND " + KEY_SEQ + "=" + seq,null); if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}

//Search Cursors *************************************************************************************************************************************************************************
public Cursor Contents_SearchAll(String Search_Type, String inputText) throws SQLException {
	Cursor mCursor = null;
	String terms="'%";
	String limiter="||";
	
	if (inputText == null || inputText.length() == 0)  {mCursor = null;}
	
	else 
	{
		if (Search_Type.equals("Exact")) mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE replace(replace(replace(replace(replace(replace(replace(replace( " + KEY_LINE + ", 'î',''), 'ð',''), 'ï',''), 'ò',''), 'ñ',''), 'ë',''),'í','') ,'ì','') LIKE '%" + inputText + "%'",null);
		else if (Search_Type.equals("Parts") || Search_Type.equals("Each")) {if (Search_Type.equals("Parts")) limiter = "||"; else if (Search_Type.equals("Each")) limiter = "OR"; for (int i = 0; i < inputText.split(" ").length - 1; i++) {if (!inputText.split(" ")[i].equals("")) terms = terms + inputText.split(" ")[i] + "%' " + limiter + " '%";} terms = terms + inputText.split(" ")[inputText.split(" ").length - 1] + "%'"; mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE replace(replace(replace(replace(replace(replace(replace(replace( " + KEY_LINE + ", 'î',''), 'ð',''), 'ï',''), 'ò',''), 'ñ',''), 'ë',''),'í','') ,'ì','') LIKE " + terms, null);}
		else if (Search_Type.equals("Root")) mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE replace(replace(replace(replace(replace(replace(replace(replace( " + KEY_LINE + ", 'î',''), 'ð',''), 'ï',''), 'ò',''), 'ñ',''), 'ë',''),'í','') ,'ì','') LIKE " + terms, null);
		else if (Search_Type.equals("Tashkeel")) mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE " + KEY_LINE + " MATCH '%*" + inputText + "*%'",null);
	
	}
	
	if (mCursor != null) {mCursor.moveToFirst();}
	
		return mCursor;
}

public Cursor Contents_Search_Custom(String Search_Type, String inputText, String Code) throws SQLException {
	Cursor mCursor = null;
	String terms="'%";
	String limiter="||";
	
	if (inputText == null || inputText.length() == 0)  {mCursor = null;}
	
	else 
	{
		if (Search_Type.equals("Exact")) mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE replace(replace(replace(replace(replace(replace(replace(replace( " + KEY_LINE + ", 'î',''), 'ð',''), 'ï',''), 'ò',''), 'ñ',''), 'ë',''),'í','') ,'ì','') LIKE '%" + inputText + "%' AND " + KEY_CODE + " in " + Code, null);
		else if (Search_Type.equals("Parts") || Search_Type.equals("Each")) {if (Search_Type.equals("Parts")) limiter = "||"; else if (Search_Type.equals("Each")) limiter = "OR"; for (int i = 0; i < inputText.split(" ").length - 1; i++) {if (!inputText.split(" ")[i].equals("")) terms = terms + inputText.split(" ")[i] + "%' " + limiter + " '%";} terms = terms + inputText.split(" ")[inputText.split(" ").length - 1] + "%'"; mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE replace(replace(replace(replace(replace(replace(replace(replace( " + KEY_LINE + ", 'î',''), 'ð',''), 'ï',''), 'ò',''), 'ñ',''), 'ë',''),'í','') ,'ì','') LIKE " + terms + " AND " + KEY_CODE + " in " + Code, null);}
		else if (Search_Type.equals("Root")) mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE replace(replace(replace(replace(replace(replace(replace(replace( " + KEY_LINE + ", 'î',''), 'ð',''), 'ï',''), 'ò',''), 'ñ',''), 'ë',''),'í','') ,'ì','') LIKE " + terms + " AND " + KEY_CODE + " in " + Code, null);
		else if (Search_Type.equals("Tashkeel")) mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTS + " WHERE " + KEY_LINE + " MATCH '%*" + inputText + "*%' AND " + KEY_CODE + " in " + Code, null);
	}
	
	if (mCursor != null) {mCursor.moveToFirst();}
	
		return mCursor;
}
//****************************************************************************************************************************************************************************************
//########################################################################################################################################################################################

//#################### Fetching ContentCat Table By Some Criteria ########################################################################################################################
public Cursor ContentCatBy_CATEGORY_ID(Integer input) throws SQLException {Cursor mCursor = null; if (input == null)  {mCursor = null;}	else mCursor = mDb.rawQuery("SELECT * FROM " + TBL_CONTENTCAT + " WHERE " + KEY_CATEGORY_ID + " MATCH '" + input + "'",null); if (mCursor != null) {mCursor.moveToFirst();}	return mCursor;}
//########################################################################################################################################################################################


}