#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	op_get,
	op_run,
	op_set,
};
//put additional include files here

///////////////////////////////////////////////////////////////////////
//////// modification log
/// Hong 02/21/10 QA80-15106 FIX_RUN_FAIL_TO_TRIGGER_RECALCULATE_EVEN_CHANGE_GUI_SETTINGS
/// Folger 02/25/10 QA81-15106-P2 OP_CHANGE_SUPPORT_CHANGE_OPERATION_MODE
/// Folger 04/22/10 QA81-15106-P5 OP_CHANGE_FAILED_TO_GET_OPERATION_ON_MATRIX_OBJECT
/// Folger 04/22/10 QA81-15348 OP_CHANGE_FROM_PA
/// Folger 05/24/10 QA81-15348-P1 OP_CHANGE_FROM_PA_SHOULD_NOT_CHANGE_OP_TREE_WHEN_RUN
/// Folger 08/27/2010 ORG-905-P1 OP_CHANGE_SHOULD_UPDATE_RANGES_IN_XF_GETN_TREE
///////////////////////////////////////////////////////////////////////
//put your own support static functions here

///------ Folger 04/22/10 QA81-15348 OP_CHANGE_FROM_PA
#include <..\originlab\theme_utils.h>
///------ End OP_CHANGE_FROM_PA


void op_change(const Range& ir, TreeNode& tr, const int& op, int& result)
{
	
	if(tr==NULL || !tr.IsValid())
		XF_THROW(CER_INVALID_TREENODE);
	if(ir==NULL || !ir.IsValid())
		XF_THROW(CER_INVALID_RANGE);
	
	int r1, c1, r2, c2;
	Datasheet ds;
	if( !ir.GetRange(0, r1, c1, r2, c2, ds) )
		XF_THROW(XFERR_FAILED_GET_RANGE);
	
	bool bCheckHierarchical = false;
	if( r1 == 0 && c1 == 0 && r2 == -1 && c2 == -1 )
	{
		c1 = -1;
		bCheckHierarchical = true;
	}
	
	Worksheet	wks(ds);
	///------ Folger 04/22/10 QA81-15106-P5 OP_CHANGE_FAILED_TO_GET_OPERATION_ON_MATRIX_OBJECT
	//if( !wks.IsValid() )
		//XF_THROW(CER_INVALID_WKS);
		
	//UINT nOpID = wks.GetIncomingOperation(r1, c1, bCheckHierarchical);
	UINT		nOpID = 0;
	if ( wks )
	{
		nOpID = wks.GetIncomingOperation(r1, c1, bCheckHierarchical);
	}
	else
	{
		OperationManager	opMngr;
		int					nCount = opMngr.GetCount();
		for ( int ii=0; ii<opMngr.GetCount(); ++ii )
		{
			OperationBase&	opb = opMngr.GetOperation(ii);
			if ( !opb )
				continue;
			
			DataRange		dr;
			if ( opb.GetOutput(dr) && dr && dr.IsIntersect(ir) )
			{
				nOpID = opb.GetUID();
				break;
			}
		}
	}
	///------ End OP_CHANGE_FAILED_TO_GET_OPERATION_ON_MATRIX_OBJECT
	
	if( 0 == nOpID )
		XF_THROW(XFERR_OPERATION_NOT_FOUND);
	
	OperationBase& operation = Project.GetOperationObject(nOpID);
	if( !operation.IsValid() )
		XF_THROW(CER_GET_OPERATION_OBJECT);
	
	result = 1;
	if( op_get == op) //get
	{
		operation.GetTree(tr);
		///------ Folger 04/22/10 QA81-15348 OP_CHANGE_FROM_PA
		WizOperationChangeHelper		clHelper;
		clHelper.Convert(tr, TRUE);
		///------ End OP_CHANGE_FROM_PA
		///------ Folger 08/27/2010 ORG-905-P1 OP_CHANGE_SHOULD_UPDATE_RANGES_IN_XF_GETN_TREE
		//if ( tr.GUI )
		///------ End OP_CHANGE_SHOULD_UPDATE_RANGES_IN_XF_GETN_TREE
			result = operation.OperationtoGUI(tr, false, NULL, false, 0, 0);
		return;
	}
	
	///------ Folger 04/22/10 QA81-15348 OP_CHANGE_FROM_PA
	WizOperationChangeHelper		clHelper;
	///------ Folger 05/24/10 QA81-15348-P1 OP_CHANGE_FROM_PA_SHOULD_NOT_CHANGE_OP_TREE_WHEN_RUN
	TreeNode						tr2 = tr.Clone();
	///------ End OP_CHANGE_FROM_PA_SHOULD_NOT_CHANGE_OP_TREE_WHEN_RUN
	BOOL	bWiz = clHelper.Convert(tr2, FALSE);
	///------ End OP_CHANGE_FROM_PA
	
	// set
	if(tr2.GUI && !operation.GUItoOperation(tr2, GUI_OP_CHANGE_PARAMS))
	{
		result = -1;
		return;
	}
	
	///------ Folger 02/25/10 QA81-15106-P2 OP_CHANGE_SUPPORT_CHANGE_OPERATION_MODE
	vector<string>	vsGUIs = {"GUI", "xfGetN"};
	TreeNode		trGUI;
	for ( int ii=0; ii<vsGUIs.GetSize(); ++ii )
	{
		if ( (trGUI = tr2.GetNode(vsGUIs[ii])) )
			break;
	}
	
	///------ Folger 04/22/10 QA81-15348 OP_CHANGE_FROM_PA
	//TreeNode	trAutoUpdate = trGUI.AutoUpdate;
	TreeNode	trAutoUpdate = bWiz ? tr2.wizExtern.wizTheme.Shared.AutoUpdate : trGUI.AutoUpdate;
	///------ End OP_CHANGE_FROM_PA
	if ( trAutoUpdate )
	{
		if ( AU_NONE == trAutoUpdate.nVal )
		{
			operation.SetAsNone();
			return;
		}
	}
	///------ End OP_CHANGE_SUPPORT_CHANGE_OPERATION_MODE
	
	/// Hong 02/21/10 QA80-15106 FIX_RUN_FAIL_TO_TRIGGER_RECALCULATE_EVEN_CHANGE_GUI_SETTINGS
	operation.SetModified();
	/// end FIX_RUN_FAIL_TO_TRIGGER_RECALCULATE_EVEN_CHANGE_GUI_SETTINGS
	operation.SetTree(tr2);
	if(op_run == op)
		Project.Run(operation.GetUID());
	
	///------ Folger 02/25/10 QA81-15106-P2 OP_CHANGE_SUPPORT_CHANGE_OPERATION_MODE
	if ( trAutoUpdate )
	{
		if ( AU_AUTO == trAutoUpdate.nVal || AU_ON_COMMAND == trAutoUpdate.nVal )
		{
			operation.SetAutoUpdateMode(trAutoUpdate.nVal);
		}
		else
		{
			ASSERT(FALSE);
		}
	}
	///------ End OP_CHANGE_SUPPORT_CHANGE_OPERATION_MODE
}

//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 op_change_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
