#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	arc_radians,
	arc_degree,
};
#include <math.h>

//put additional include files here

//put your own support static functions here
//enum {
	//CC_INVAILD_DATA_SIZE  = 1,	
	//CC_INVAILD_DATA_TYPE,
//};

//enum {
	//RADIANS = 0,
	//DEGREE,
//};

static bool check_type(int nType)
{
	switch(nType)
	{
		case FSI_CHAR:
		case FSI_TEXT:
		case FSI_MIXED:
		case FSI_COMPLEX:
		{
			return false;
		}				
	}
	return true;	
}

void make_Complex_from_amp_and_pha(matrix& ma, matrix& mp, matrix<complex>& mc, int nArc)
{	
	int nCols, nRows;
	
	nCols = ma.GetNumCols();
	nRows = ma.GetNumRows();
	
	vector va(nCols*nRows), vp(nCols*nRows), vc_re(nCols*nRows), vc_im(nCols*nRows);
	
	ma.GetAsVector(va);
	mp.GetAsVector(vp);
	
	if(nArc == arc_degree)
	{			
		va = (va * PI)/180.0;	
		vp = (vp * PI)/180.0;
	}
	
	vc_re = va * cos(vp);
	vc_im = va * sin(vp);
	
	
	matrix mc_re(nRows, nCols), mc_im(nRows, nCols);
	mc_re.SetByVector(vc_re);
	mc_im.SetByVector(vc_im);	
	
	mc.MakeComplex(mc_re,mc_im);
}


void map2c(const MatrixObject& am, const MatrixObject& pm, MatrixObject& cm, const int& arc)
{
	if(!am.IsValid())
	{
		XF_THROW_EX(CER_INVALID_MATOBJ2, "am");
		return;
	}
	
	if(!pm.IsValid())
	{
		XF_THROW_EX(CER_INVALID_MATOBJ2, "pm");
		return;
	}
	
	if(!check_type(am.GetInternalDataType()) || !check_type(pm.GetInternalDataType()))
	{
		XF_THROW(CER_INVALID_VEC_MAT_TYPE);
		return;
	}
	
	if((arc !=  arc_radians) && (arc != arc_degree))
	{
		XF_THROW(CER_INVALID_ARC);
		return;
	}
	
	
	matrix& ma = am.GetDataObject();
	matrix& mp = pm.GetDataObject();	
									
	int nRows = ma.GetNumRows();
	int nCols = ma.GetNumCols();
	
	if(ocmath_count(NANUM, nRows*nCols, ma) || ocmath_count(NANUM, nRows*nCols, mp))
	{
		XF_THROW(CER_DIFF_NANUM_NUM);
		return;
	}
	
	if(nCols != mp.GetNumCols() && nRows != mp.GetNumRows())
	{
		XF_THROW(CER_DIFF_MAT_SIZE);
		return;
	}
	
	cm.SetNumRows(nRows);
	cm.SetNumCols(nCols);	
	
	cm.SetInternalData(FSI_COMPLEX);
	matrix<complex>& mout = cm.GetDataObject();
	mout = ma;	
	
	make_Complex_from_amp_and_pha(ma, mp, mout, arc);	
	
}

//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 map2c_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return map2c_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int map2c_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 map2c_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 map2c_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
