#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <event_utils.h>
#include <..\originlab\grobj_utils.h>
#include <..\originlab\graph_utils.h>
#ifdef _FOR_SMART_LOADING_ONLY
#include <..\originlab\AxisScrollbar.h>
#endif //_FOR_SMART_LOADING_ONLY
///////////////////////////////////////////////////////////////////////
// 	Modification Log
//	Jasmine 01/05/09 v8.0993d QA80-12897 KKEP_SAME_RESOLUTION_AS_INPUT_PAGE_TO_CONSISTENT_WITH_DUPLICATE
///////////////////////////////////////////////////////////////////////

//put your own support static functions here
///Jake 05/23/07 SET_SIZE_FOR_PAGE_AND_ADD_FULL_PAGE_OPTION
//----Jake 05/22/07 SET_LAYER_SIZE_BY_DEF
//static void _set_layer(GraphLayer &gl,double dWidth,double dHeight,double dLeft,double dTop,int unit)
//{
//	default setting of the layers, unit is % of pagh by default , for full page
	//double dWidth = 68.2, dHeight = 71.8, dLeft = 17.9, dTop = 11.6;
	//int unit;
	//
	//if(!bFullPage)
	//{
		//layer_get_size(gl, dWidth, dHeight,&unit);
		//layer_get_position( gl,  &dLeft, &dTop);
	//}
	//
	//layer_set_size(gl, dWidth, dHeight,&unit);
	//layer_set_position(gl,dLeft,dTop);
//}
//----end SET_LAYER_SIZE_BY_DEF


//static bool _graph_get_size(GraphPage gp,double& dGpWidth,double& dGpHeight)
//{
	//Tree tr;
    //tr = gp.GetFormat(FPB_ALL, FOB_ALL, true, true);
    //if(!tr)
    	//return false;
    //
    //dGpWidth = tr.Root.Dimension.Width.dVal;
	//dGpHeight = tr.Root.Dimension.Height.dVal;
	//
	//return true;
//}
//
//static bool _graph_set_size(GraphPage& gp,double dGpWidth,double dGpHeight)
//{
	//Tree tr;
    //tr = gp.GetFormat(FPB_ALL, FOB_ALL, true, true);
    //if(!tr)
    	//return false;
    //
    //tr.Root.Dimension.Width.dVal = dGpWidth;
	//tr.Root.Dimension.Height.dVal = dGpHeight;
	//
	//return  gp.ApplyFormat(tr,TRUE,TRUE);
//}

static bool _create_graph_for_layer(GraphPage& igp, GraphLayer glDest,int bFullPage)
{
	///Jasmine 01/05/09 v8.0993d QA80-12897 KKEP_SAME_RESOLUTION_AS_INPUT_PAGE_TO_CONSISTENT_WITH_DUPLICATE
	//GraphPage gp();
	//gp.Create();
	GraphPage gp = igp.Clone();
	///End KKEP_SAME_RESOLUTION_AS_INPUT_PAGE_TO_CONSISTENT_WITH_DUPLICATE
	//gp.SetLongName(igp.GetName()); /// Iris 05/28/2007 v8.0621b by Max, not use the parent igp name as the long name of all other pages.
	
	///Iris 06/01/2007 v8.0631 FIX_USE_INCORRECT_WAY_TO_FULL_PAGE
	/*
	double dWidth, dHeight, dLeft, dTop;//default setting of the layers, unit is % of pagh by default , for full page
	int unit;
	
	///Jake 06/04/07 FIX_FULL_PAGE_SETUP
	if(!bFullPage)
	{
	layer_get_size(glDest, dWidth, dHeight,&unit);
	layer_get_position(glDest,  &dLeft, &dTop);
	
	double dGpWidth, dGpHeight;
	_graph_get_size(igp,dGpWidth,dGpHeight);
	_graph_set_size(gp,dGpWidth,dGpHeight);
	}
	*/
	///Jasmine 01/05/09 v8.0993d QA80-12897 KKEP_SAME_RESOLUTION_AS_INPUT_PAGE_TO_CONSISTENT_WITH_DUPLICATE
	//Tree tr;
    //tr = igp.GetFormat(FPB_ALL, FOB_ALL, true, true);
    //if(!tr)
    	//return false;
    //gp.ApplyFormat(tr,TRUE,TRUE);
    ///End KKEP_SAME_RESOLUTION_AS_INPUT_PAGE_TO_CONSISTENT_WITH_DUPLICATE
    
	layer_set_link(glDest, -1);
	///Vinden 04/30/2008 v8.0853 XFERR_FAIL_TO_ADD_LAYER
	//gp.AddLayer(glDest);
	int nRet=gp.AddLayer(glDest);
	if(nRet<0)
		return false;
	///End XFERR_FAIL_TO_ADD_LAYER
	
	///Jasmine 01/05/09 v8.0993d QA80-12897 KKEP_SAME_RESOLUTION_AS_INPUT_PAGE_TO_CONSISTENT_WITH_DUPLICATE
	//gp.Layers(0).Destroy();
	for(int ii = 0; ii < nRet; ii++)
	{
		gp.Layers(0).Destroy();
	}
	///End KKEP_SAME_RESOLUTION_AS_INPUT_PAGE_TO_CONSISTENT_WITH_DUPLICATE
	
	GraphLayer gl = gp.Layers(0);
	
	//---Jake 07/12/07 FIXED_NOT_ALL_SCROLLBAR_DELETE
	//remove_axis_scrollbar(gl);///Jake 06/14/07 v8.0639 REMOVE_AXIS_FROM_LAYER
	remove_axis_scrollbar(gl,true);
	remove_axis_scrollbar(gl,false);
	//end FIXED_NOT_ALL_SCROLLBAR_DELETE
	
	//layer_set_size(gp.Layers(0), dWidth, dHeight,&unit);
	//layer_set_position(gp.Layers(0),dLeft,dTop,&unit);
	// gp.AddLayer(glDest);
	
	if(bFullPage)
	{
		//GraphLayer gl = gp.Layers(0);
		gl.LT_execute("layer 0;");
	}
	
	gp.LT_execute("page.zoomWhole=1");///Jake 06/05/07 SHOW_WHOLE_PAGE
	///end FIX_FULL_PAGE_SETUP
	
	///FIX_USE_INCORRECT_WAY_TO_FULL_PAGE
	
	///Iris 06/01/2007 v8.0631 FIX_USE_INCORRECT_WAY_TO_FULL_PAGE

	///FIX_USE_INCORRECT_WAY_TO_FULL_PAGE
	//_set_layer(gp.Layers(0),);
	
   return true;///Vinden 04/30/2008 v8.0853 FUNCTION_ADDLAYERS_RUN_WRONG
}
///end SET_SIZE_FOR_PAGE_AND_ADD_FULL_PAGE_OPTION

void layextract(const GraphPage& igp, const string& layer, const int& keep, const int& fullpage)
{
	if(!igp)
		XF_THROW(XFERR_INVALID_PAGE);
		
	vector<int> vnLayers;
	GraphPage gp = igp;
	int nMax = igp.Layers.Count();
	//if(vnLayers.GetSize() == 1)
	if(nMax == 1)
		XF_THROW(XFERR_ONLY_ONE_LAYER);
	int nErr = get_indices_from_origin_object_index_range_string(layer, vnLayers, nMax);
	if(nErr != CER_NO_ERROR)
		XF_THROW(nErr);
	for(int ii = 0; ii < vnLayers.GetSize(); ii++)
	{	
		int index = vnLayers[ii] -1;
		GraphLayer glDest= igp.Layers(index);
		if(glDest.IsValid())
		{
		    //remove_axis_scrollbar(glDest);///Jake 06/14/07 v8.0639 REMOVE_AXIS_FROM_LAYER
			//----Jake 06/12/07 v8.0639 DEBUG_FOR_BREAK_LINKED_LAYERS
			int nParentLayer, pXAxis, pYAxis, pnUnit;
			string pX1, pX2, pY1, pY2;
			layer_get_link(glDest,nParentLayer,&pXAxis,&pYAxis,&pnUnit,&pX1,&pX2,&pY1,&pY2);
			///Vinden 04/30/2008 v8.0853 XFERR_FAIL_TO_ADD_LAYER
			//_create_graph_for_layer(gp, glDest,fullpage);
			 bool bRet=_create_graph_for_layer(gp, glDest,fullpage);
			 if(!bRet)
				XF_THROW(XFERR_FAIL_TO_ADD_LAYER);
			///End XFERR_FAIL_TO_ADD_LAYER
			layer_set_link(glDest,nParentLayer,pXAxis,pYAxis,&pnUnit,pX1,pX2,pY1,pY2);
			//-----end DEBUG_FOR_BREAK_LINKED_LAYERS
		}
	}
	if(!keep)
		gp.Destroy();
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int layextract_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return layextract_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	int nErr = CER_NO_ERROR;
	int layers;
	GraphPage gp;
	if(!( okxf_resolve_string_get_origin_object(trGetN.igp.strVal, &gp) && gp.IsValid()) )
		nErr = XFERR_INVALID_PAGE;
	else if(2 > (layers = gp.Layers.Count()))
		nErr = XFERR_ONLY_ONE_LAYER;
	if(nErr != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErr;
	}
	
	return false;

}

int layextract_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//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 layextract_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int layextract_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
