#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	execute_change,
	execute_askrem,
	execute_load,
	execute_remove,
};
enum{
	option_ado,
	option_odbc,
};


//--------- Modification Log -----------------
// CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
//  I reorder the enum above, used to be edit/remove/load so ogs will have dbEdit 2, but I see dbEdit 2 should open dialog, default should be to load to active wks
// CPY 1/21/2008 QA70-10972-P3 QUERY_BUILDER_SHOULD_BE_MODAL
/// AW 05/07/08 AW QA80-11454 DO_NOTHING_IF_SELECT_CANCE_IN_ADD_QUERY_FILE
///Sophy 8/3/2009 QA80-14057 SUPPORT_IMPORTING_INTO_WKS_USING_ODBC_IN_QUERY_BUILDER
///-----Kit 10/20/2010 ORG-1293-P1 ADD_SYSVAR_FOR_DATABASE_IMPORT_TOOL

typedef int (*FUNC_INT_WKSREF)(Worksheet& wks); //CPY 1/21/2008 QA70-10972-P3 QUERY_BUILDER_SHOULD_BE_MODAL

#include <..\originlab\datasource.h>
//#include <..\originlab\FileList.h>
//#include <..\originlab\QueryBldrDlg.h>

//put your own support static functions here
//enum {DBE_EDIT, DBE_REMOVE, DBE_LOAD};
/*
enum {
	ERR_FAIL_TO_LOAD   = XF_LOCAL_ERR_BEGIN,
	ERR_FAIL_TO_LOAD_TO_WKS,
	ERR_NO_ACTIVE_WKS,
};
*/

//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
// SQL must have at least "select", ds must also be quite long, not sure, just 2 for now
#define MIN_SQL_LENGTH 7
#define MIN_DS_LENGTH 2
static bool _is_ODS_file(const string& strFile, string& strNameExt)
{
	string strTemp, strExt;
	separate_file_name_ext(strFile, &strTemp, &strExt);
	strNameExt = GetFileName(strFile);
	
	strExt.MakeLower();
	if(strExt == "ods")
		return true;
	
	return false;
}
//-----


void dbEdit(const int& execute, const string& fname, const Worksheet& iw, const string& sql, const string& connect, const int& option)
{
	//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
	//Worksheet wks=Project.ActiveLayer();
	string strDSFile, strDS, strSQL, strODQfile;
	Worksheet wks;
	if(iw)
		wks = iw;
	else
		wks = Project.ActiveLayer();
	int cmd = execute;
	//-----
	
	/// AW 06/05/2006 MORE_ON_LOAD_ODQ
	// we need a tag to know if QueryBldrDlg called from LOAD ODQ or others, here, we just simplely use nonZero for it, so need another head file for it.
	//---- CPY 1/21/2008 QA70-10972-P3 QUERY_BUILDER_SHOULD_BE_MODAL
	//DWORD dwCntrl = 0;
	//----
	/// END MORE_ON_LOAD_ODQ
	if(execute_load == cmd)
	{
		LPCSTR lpcszODQfile = NULL;//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
		if(wks)
		{
			//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
			//if ( !db_load_query(strDS, strSQL, NULL, &strDSFile, &strODQfile) )
			if(fname.GetLength() > 1)
			{
				string strDSFileName;
				bool bIsODS = _is_ODS_file(fname, strDSFileName);
				bool bIsFileValid = fname.IsFile();
				if(bIsFileValid && !bIsODS)
					lpcszODQfile = fname;
				else
				{
					if(bIsODS) // must be bIsFileValid
					{
						if(!db_load_data_source(fname, strDS, &strDSFile))
							XF_THROW_EX(XFERR_FAIL_TO_LOAD_ODS, fname);
						if(strDSFile.IsEmpty())
							strDSFile = strDSFileName;
						if(strSQL.IsEmpty())
							strSQL = " "; // empty will remove it
						if(!db_wks_set_query(wks, strDS, strSQL, strDSFile))
							XF_THROW(XFERR_FAIL_TO_LOAD_DATABASE_TO_WKS);
	
						return;
					}
					XF_THROW_EX(XF_CD_NOT_VALID_PATH, fname);
				}
			}
			///Sophy 8/3/2009 QA80-14057 SUPPORT_IMPORTING_INTO_WKS_USING_ODBC_IN_QUERY_BUILDER
	#ifndef	__SUPPORT_IMPORT_DATA_BY_ODBC_IN_QUERY_BUILDER__
			if ( !db_load_query(strDS, strSQL, lpcszODQfile, &strDSFile, &strODQfile) )
			//----
			{
				XF_THROW_EX(XFERR_FAIL_TO_LOAD_DATABASE, fname);
			}
	#else
			string	strQueryMode = "";
			if ( !db_load_query(strDS, strSQL, lpcszODQfile, &strDSFile, &strODQfile, &strQueryMode) )
			//----
			{
				XF_THROW_EX(XFERR_FAIL_TO_LOAD_DATABASE, fname);
			}
			
			BOOL	bUseODBC = strQueryMode.CompareNoCase(STR_QUERY_MODE_ADO) == 0 ? FALSE : TRUE;
			
	#endif	//__SUPPORT_IMPORT_DATA_BY_ODBC_IN_QUERY_BUILDER__
			//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
			///// AW 09/15/07 QA80-9625 MORE_ON_CANCEL_LOAD_ODQ 
			//if( strODQfile.IsEmpty() )
			//	XF_THROW(CER_NO_ERROR);	
			///// END MORE_ON_CANCEL_LOAD_ODQ
			//-----
			/// AW 05/07/08 AW QA80-11454 DO_NOTHING_IF_SELECT_CANCE_IN_ADD_QUERY_FILE
			if( strODQfile.IsEmpty() )
				return;
			/// END DO_NOTHING_IF_SELECT_CANCE_IN_ADD_QUERY_FILE
			
			///Sophy 8/3/2009 QA80-14057 SUPPORT_IMPORTING_INTO_WKS_USING_ODBC_IN_QUERY_BUILDER
	#ifndef	__SUPPORT_IMPORT_DATA_BY_ODBC_IN_QUERY_BUILDER__
			if(!db_wks_set_query(wks, strDS, strSQL, strDSFile))
				XF_THROW(XFERR_FAIL_TO_LOAD_DATABASE_TO_WKS);
	#else
			if(!db_wks_set_query(wks, strDS, strSQL, strDSFile, false, &bUseODBC))
				XF_THROW(XFERR_FAIL_TO_LOAD_DATABASE_TO_WKS);
	#endif	//__SUPPORT_IMPORT_DATA_BY_ODBC_IN_QUERY_BUILDER__
		}
		else
			XF_THROW(XFERR_NO_ACTIVE_WORKSHEET);
		
		//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
		//dwCntrl = 1;			/// AW 06/05/2006 MORE_ON_LOAD_ODQ
		return;
		//-----
	}
	//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
	//if(execute_remove == cmd)
	else if(execute_askrem == cmd || execute_remove == cmd)
	//-----
	{
		if(wks)
		{
			//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
			//if(IDOK == MessageBox(GetWindow(), _L("Are you sure you want to remove the SQL object in the active worksheet?"), _L("SQL in Worksheet"), MB_OKCANCEL|MB_ICONQUESTION)) 		
			if(execute_remove == cmd ||
				IDOK == MessageBox(GetWindow(), 
				_L("Are you sure you want to remove the SQL object in the active worksheet?"), 
				_L("SQL in Worksheet"), MB_OKCANCEL|MB_ICONQUESTION))
			//-----
				db_wks_set_query(wks);
		}
		else
			XF_THROW(XFERR_NO_ACTIVE_WORKSHEET);
		return;
	}
	//----- CPY 1/21/2008 QA70-10972 LT_CONTROL_OF_DB_ACCESS
	else if(execute_change == cmd && (sql.GetLength() > MIN_SQL_LENGTH || connect.GetLength() > MIN_DS_LENGTH))
	{
		if(!wks)
			XF_THROW(XFERR_NO_ACTIVE_WORKSHEET);
		
		db_wks_get_query(wks, strDS, strSQL, strDSFile);
		
		if(sql.GetLength() > MIN_SQL_LENGTH)
			strSQL = sql;
		if(connect.GetLength() > MIN_DS_LENGTH)
			strDS = connect;
		///Sophy 8/3/2009 QA80-14057 SUPPORT_IMPORTING_INTO_WKS_USING_ODBC_IN_QUERY_BUILDER
	#ifndef	__SUPPORT_IMPORT_DATA_BY_ODBC_IN_QUERY_BUILDER__
		if(!db_wks_set_query(wks, strDS, strSQL, strDSFile, false))
	#else
		BOOL bUseODBC = option_odbc == option ? TRUE : FALSE;
		if( !db_wks_set_query(wks, strDS, strSQL, strDSFile, false, &bUseODBC) )
	#endif	//__SUPPORT_IMPORT_DATA_BY_ODBC_IN_QUERY_BUILDER__
			XF_THROW(XFERR_FAIL_TO_LOAD_DATABASE_TO_WKS);
			
		return;
	}
	//-----
	
	string strFunc = "QueryBldrDlg";
	//------------------ CPY 1/21/2008 QA70-10972-P3 QUERY_BUILDER_SHOULD_BE_MODAL
	//FUNC_INT_DWORD pfn = Project.FindFunction(strFunc, "OriginLab\\QueryBldrDlg", true);
	/// AW 06/05/2006 MORE_ON_LOAD_ODQ
	//if(pfn)
	//	pfn(0,0);	
	//if(pfn)
	//	pfn(0,dwCntrl);	 
	/// END MORE_ON_LOAD_ODQ
	
	///-----Kit 10/20/2010 ORG-1293-P1 ADD_SYSVAR_FOR_DATABASE_IMPORT_TOOL
	#ifndef	__NEW_DATABASEIMPORTDLG__
	FUNC_INT_WKSREF pfn = Project.FindFunction(strFunc, "OriginLab\\QueryBldrDlg", true);
	#else
	double dVal;
	LT_get_var("@did", &dVal); 
	int nVal = (dVal == 0.0) ? 1 : 0;
	string strTitleText = _L("Attention\r\nOrigin provides two different database import dialogs. Please select one of them.\r\nNote: To manually change the default database import dialog, please refer to the @DID system variable.\r\n\r\nWhat kind of dialog do you like:");
	vector<string> vsChoices(2);
	vsChoices[0] = _L("SQL Editor to type SQL statement directly");
	vsChoices[1] = _L("Graphical Query Builder to create SQL statement");
	string strCheckBox = _L("Do not show this message in the future");
	
	int nChoice = RatioReminderMessage("ADODlgReminder", strTitleText, &vsChoices, nVal, strCheckBox);
	if ( nChoice == -1 ) //when cancel, should use default value
		nChoice = nVal;
	LT_set_var("@did", (nChoice == 0 ? 1 : 0));
	
	FUNC_INT_WKSREF pfn;
	if( 1 == nChoice )
		pfn = Project.FindFunction( strFunc, "OriginLab\\QueryBldrDlg", true );
	else
		pfn = Project.FindFunction( "OpenDatabaseImportDlg", "OriginLab\\DatabaseImportDlg", true );
	#endif	//__NEW_DATABASEIMPORTDLG__
	///-----End ADD_SYSVAR_FOR_DATABASE_IMPORT_TOOL
	if(pfn)
		pfn(wks);
	//-------------------
	
}

//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void dbEdit_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
