#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_invalid,
	type_long,
	type_unit,
	type_comment,
	type_rate,
	type_para,
	type_ud,
	type_all,
};
enum{
	col_new,
	col_first,
};
//put additional include files here
#include <GetNBox.h> 
#include <event_utils.h>
///Arvin 11/15 MOVE_TO_OERRMSG
/*
#define STR_WARN_DIFF_DATA_TYPE "Warning: Columns of worksheet have different data type and will be set to double after transpose."
#define STR_WARN_IGORE_SELECTION "Whole worksheet will be transposed. The current selection will not be considered. Continue?"
*/
///end MOVE_TO_OERRMSG
//put your own support static functions here
#define STR_NODE_SELECTION 		"select"
#define STR_NODE_EXCHANGE		"exchange"

enum{
	NEW_COLUMN,
	FIRST_COLUMN,
};

enum{
	INVALID,
	LONG_NAME,
	UNITS,
	COMMENTS,
	SAMPLE_RATE,
	PARAMETER,
	USER_DEFINE,
};

static bool _has_col_protected(Worksheet &wks)
{
	foreach(Column cc in wks.Columns)
	{
		if(cc.IsWriteProtected())
			return true;
	}
	
	return false;
}
static void _init_tree_show(TreeNode& trGetN)
{
	if(trGetN.select.nVal)
	{
		trGetN.select.Enable = true;
		trGetN.exchange.Enable = false;
		trGetN.type.Show = false;
		trGetN.col.Show = false;
	}
	
	if(trGetN.exchange.nVal)
	{
		trGetN.select.Enable = false;
		trGetN.exchange.Enable = true;
		trGetN.type.Show = true;
		///------ Folger 07/28/10 ORG-50 WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
		//trGetN.col.Show = true;
		_update_data_col_hide(trGetN);
		///------ End WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
	}
}

static void _update_tree_show_by_select(TreeNode& trGetN)
{
	int nSel = trGetN.select.nVal;
	if(nSel > 0)
	{
		trGetN.exchange.nVal = 0;
		trGetN.select.Enable = true;
		trGetN.exchange.Enable = false;
		trGetN.type.Show = false;
		trGetN.col.Show = false;
	}
	else if(nSel <= 0)
	{
		trGetN.exchange.Enable = true;
		if(trGetN.exchange.nVal)
		{
			trGetN.type.Show = true;
			///------ Folger 07/28/10 ORG-50 WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
			//trGetN.col.Show = true;
			_update_data_col_hide(trGetN);
			///------ End WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
			trGetN.select.Enable = false;
		}
		else
		{
			trGetN.type.Show = false;
			trGetN.col.Show = false;
			trGetN.select.Enable = true;
		}
	}
}

static void _update_tree_show_by_exchange(TreeNode& trGetN)
{
	int nExch = trGetN.exchange.nVal;
	if(nExch <= 0)
	{
		trGetN.select.Enable = true;
		trGetN.type.Show = false;
		trGetN.col.Show = false;
		if(trGetN.select.nVal)
			trGetN.exchange.Enable = false;
		else
			trGetN.exchange.Enable = true;	
	}
	else if(nExch > 0)
	{
		trGetN.select.nVal = 0;
		trGetN.select.Enable = false;
		trGetN.exchange.Enable = true;
		trGetN.type.Show = true;
		///------ Folger 07/28/10 ORG-50 WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
		//trGetN.col.Show = true;
		_update_data_col_hide(trGetN);
		///------ End WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
	}
}

///------ Folger 07/28/10 ORG-50 WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
static	void	_update_data_col_hide(TreeNode& trGetN)
{
	trGetN.col.Show = INVALID != trGetN.type.nVal;
}
///------ End WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS

///------ Folger 08/05/10 ORG-50-S1 SHOW_WARNING_MESSAGE_IF_INCLUDE_SAMPLE_RATE_EXCHANGED_WITH_DATA
BOOL	IS_SAMPLE_RATE_EXCHANGE_WITH_DATA(TreeNode& trGetN)
{
	return col_first == trGetN.col.nVal && (type_rate == trGetN.type.nVal || type_all == trGetN.type.nVal);
}
///------ End SHOW_WARNING_MESSAGE_IF_INCLUDE_SAMPLE_RATE_EXCHANGED_WITH_DATA

void wtranspose(const Worksheet& iw, const int& select, const int& exchange, const int& type, const int& col, Worksheet& ow)
{
	if(!iw.IsValid())
		XF_THROW(CER_NO_WORKSHEET);
	
	bool bRet = wks_copy(ow, iw);
	if(bRet)
	{	
		bool bSel = false, bInsertCol = false;
		int nLabelType = 0;
		
		if(select > 0)
			bSel = true;
		
		if(col == NEW_COLUMN)
			bInsertCol = true;
		
		switch(type)
		{
		case INVALID:
			nLabelType = RCLT_INVALID;
			break;
		case LONG_NAME:
			nLabelType = RCLT_LONG_NAME;
			break;
		case UNITS:
			nLabelType = RCLT_UNIT;
			break;
		case COMMENTS:
			nLabelType = RCLT_COMMENT;
			break;
			
		case SAMPLE_RATE:
			nLabelType = RCLT_SAMPLE_RATE;
			break;
			
		case PARAMETER:
			nLabelType = RCLT_PARAM;
			break;
			
		case USER_DEFINE:
			nLabelType = RCLT_UDL;
			break;
			
		///------ Folger 07/28/10 ORG-50 WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
		case type_all:
			nLabelType = RCLT_ALL_LABELS;
			break;
		///------ End ORG-50 WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
		}
		
		///------ Folger 07/28/10 ORG-50-P2 WTRANSPOSE_LOST_DATA_IF_INCLUDE_HIDDEN_LABEL_ROW
		if ( nLabelType > RCLT_INVALID )
		{
			Grid	grid;
			grid.Attach(ow);
			grid.ShowLabels(nLabelType);
		}
		///------ End WTRANSPOSE_LOST_DATA_IF_INCLUDE_HIDDEN_LABEL_ROW
		if(bSel)
		{
			vector<int> vR1, vC1, vR2, vC2;
			if(iw.GetSelectedRange(vR1, vC1, vR2, vC2))
				ow.SetSelectedRange(vR1, vC1, vR2, vC2);
		}
		int nRet = ow.Transpose(bSel, nLabelType, bInsertCol, true);
		if(nRet < 0)
			XF_THROW(XFERR_WKS_TRANSPOSE_FAILED);
	}	
		
}

//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 wtranspose_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return wtranspose_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	int nErr = CER_NO_ERROR;
	
	Worksheet wksTemp;
	string strwks = trGetN.iw.strVal;
	bool bRet = okxf_resolve_string_get_origin_object(strwks, &wksTemp);
	if(!bRet)
		nErr = CER_NO_WORKSHEET;
	
	if(wksTemp.GetSystemParam(0) & WP_SHEET_HIERARCHY)
		nErr = CER_HIERARCHY_WORKSHEET;
	
	if(_has_col_protected(wksTemp))
		nErr = XFERR_WORKSHEET_HAS_COL_LOCKED;
	
	if(GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID)
	{
		_init_tree_show(trGetN);
		_update_tree_show_by_select(trGetN);
		_update_tree_show_by_exchange(trGetN);
		///------ Folger 07/28/10 ORG-50 WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
		_update_data_col_hide(trGetN);
		///------ End WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
	}
	
	bool bIsChangingData1 = lstrcmpi(lpcszNodeName, STR_NODE_SELECTION) == 0? true:false;
	if(bIsChangingData1)
		_update_tree_show_by_select(trGetN);
	
	bool bIsChangingData2 = lstrcmpi(lpcszNodeName, STR_NODE_EXCHANGE) == 0? true:false;
	if(bIsChangingData2)
		_update_tree_show_by_exchange(trGetN);
	
	///------ Folger 07/28/10 ORG-50 WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
	if ( lstrcmpi(lpcszNodeName, "type") == 0 )
	{
		_update_data_col_hide(trGetN);
	}
	///------ End WTRANSPOSE_SUPPORT_ALL_LABEL_ROWS
	
	if(nErr != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErr;
	}
	///------ Folger 08/05/10 ORG-50-S1 SHOW_WARNING_MESSAGE_IF_INCLUDE_SAMPLE_RATE_EXCHANGED_WITH_DATA
	else
	{
		if ( IS_SAMPLE_RATE_EXCHANGE_WITH_DATA(trGetN) )
		{
			strErrMsg = XFWARN_SAMPLE_RATE_INVALID_TO_TRANSPOSE;
		}
	}
	///------ End SHOW_WARNING_MESSAGE_IF_INCLUDE_SAMPLE_RATE_EXCHANGED_WITH_DATA
	
	return true;

}

int wtranspose_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 wtranspose_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///------ Folger 08/05/10 ORG-50-S1 SHOW_WARNING_MESSAGE_IF_INCLUDE_SAMPLE_RATE_EXCHANGED_WITH_DATA
	if ( is_xf_before_execute_body(nGetNDialog, dwCntrl) )
	{
		if ( IS_SAMPLE_RATE_EXCHANGE_WITH_DATA(trGetN) )
		{
			warning_msg_box(XFWARN_SAMPLE_RATE_INVALID_TO_TRANSPOSE, false);
		}
	}
	///------ End SHOW_WARNING_MESSAGE_IF_INCLUDE_SAMPLE_RATE_EXCHANGED_WITH_DATA
}
//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 wtranspose_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
