#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	unit_pixel,
	unit_percent,
};
enum{
	interpolate_normal,
	interpolate_resample,
	interpolate_bicubic,
};


#include <GetNBox.h>
#include <image_utils.h>

static int _size_image(BITMAPHANDLE* pLBmp, uint nW, uint nH, int interpolate)
{
	vector<int> vnFlags = {SIZE_NORMAL, SIZE_RESAMPLE, SIZE_BICUBIC};
	if(interpolate < 0 || interpolate >= vnFlags.GetSize())
		return INTOPTN_OUT_OF_RANGE;
	
	if(nW<=0 ||nH<=0)
		return XFERR_WIDTH_OR_HIGHT_INVALID;
	
	int nFlag = vnFlags[interpolate];
	
	//int nOrgW=pLBmp->Width;
	//int nOrgH=pLBmp->Height;
	//
	//nW=nOrgW*nW/100;
	//nH=nOrgH*nH/100;
	

			
	return L_SizeBitmap(pLBmp, nW, nH, nFlag);


}

static bool _update_settings(TreeNode& trGetN, string& strErrMsg, uint& nW, uint& nH, TreeNode& trNode = NULL)
{
	string strImgi = trGetN.img.strVal;
	Image img;
	okxf_resolve_string_get_origin_object(strImgi, &img);
	pBITMAPHANDLE phBmp = img.GetLBmp();
	
	int nOrgW=phBmp->Width;
	int nOrgH=phBmp->Height;
	
	int nUnit = trGetN.unit.nVal;// 1: percent; 0: pixel
	double dRatio;
	
	if(!trNode.IsValid())// for change theme
	{
		if(trGetN.w.dVal<0)
			trGetN.w.dVal = (trGetN.unit.nVal==1? 100 :nOrgW);
		if(trGetN.h.dVal<0)
			trGetN.h.dVal = (trGetN.unit.nVal==1? 100 :nOrgH);
	}

	if(trNode)
	{
		if(trNode.tagName == "lock" && trGetN.lock.nVal == 1)
		{

			if(nUnit == 0)
			{
				trGetN.h.dVal = (int)(nOrgH);
				trGetN.w.dVal = (int)(nOrgW);
			}
			else
			{
				trGetN.h.dVal = 100;
				trGetN.w.dVal = 100;
			}				
			
		}	
		
		if(trNode.tagName == "unit")
		{
			
			if(nUnit == 0)//pixel
			{
				trGetN.h.dVal = trGetN.h.dVal/100.0 * nOrgH;
				trGetN.w.dVal = trGetN.w.dVal/100.0 * nOrgW;
			}
			else
			{
				trGetN.h.dVal = trGetN.h.dVal / nOrgH * 100.0;
				trGetN.w.dVal = trGetN.w.dVal / nOrgW * 100.0;					
			}
		}		
		
		if(trGetN.lock.nVal==true)
		{

			if(trNode.tagName == "w")
			{
				if(nUnit == 0)
				{
					dRatio = (double)trGetN.w.dVal/(double)nOrgW; 
					trGetN.h.dVal = (int)(nOrgH * dRatio);
					trGetN.w.dVal = (int)(nOrgW * dRatio);
				}
				else
				{
					trGetN.h.dVal = trGetN.w.dVal;
				}
			}
			
			if(trNode.tagName == "h")
			{
				if(nUnit == 0)
				{
					dRatio = (double)trGetN.h.dVal/(double)nOrgH; 
					trGetN.h.dVal = (int)(nOrgH * dRatio);
					trGetN.w.dVal = (int)(nOrgW * dRatio);
				}
				else
				{
					trGetN.w.dVal = trGetN.h.dVal;
				}
			}
			


		}
		


	}
	
	
	if(trGetN.w.dVal<0 || trGetN.h.dVal <0 )
	{
		strErrMsg = CER_INVALID_SIZE;
	    return false;
	}	
	
	
	if(trGetN.unit.nVal == 1)//percent
	{
		nW = nOrgW * trGetN.w.dVal/100.;
		nH = nOrgH * trGetN.h.dVal/100.;
	}
	else
	{
		nW = trGetN.w.dVal;
		nH = trGetN.h.dVal;		
	}	
		

	return true;

}

void imgResize(const int& lock, const int& unit, double& w, double& h, const Image& img, Image& oimg, const int& interpolate)
{
	oimg.SetLBmp(img.GetLBmp(), TRUE);
	pBITMAPHANDLE phBmp = oimg.GetLBmp();
	
	int nOrgW=phBmp->Width;
	int nOrgH=phBmp->Height;	
	
	if(lock)
	{
		if( (unit == 0 && (int)(abs(w/nOrgW - h/nOrgH))>0) || (unit == 1 && abs(w-h)>0) )
			XF_THROW(XFERR_WIDTH_AND_HIGHT_NOT_MATCH);
	}
	
	if(w<0 || h <0)
		XF_THROW(CER_INVALID_SIZE);	
	
	uint nW, nH;
	if(unit == 1)//percent
	{
		nW = nOrgW * w / 100.;
		nH = nOrgH * h / 100.;
	}
	else
	{
		nW = w;
		nH = h;		
	}
	
	int nRet = _size_image(phBmp, nW, nH, interpolate);
	
	
	
	if(nRet == ERROR_NO_MEMORY_FROM_LT)
		XF_THROW(XF_ERR_NOT_ENOUGH_MEM);
	
	if(nRet < MAX_ERROR_CODE_FROM_LT)
		XF_THROW_EX(XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR, nRet);	
	
	
	if(nRet !=  SUCCESS)
		XF_THROW(nRet);
	
	
	
}

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

}

int imgResize_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 imgResize_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	///Sandy 2007-5-24
	if(nGetNDialog == 0 && trGetN.lock.nVal && (trGetN.w.dVal<0 || trGetN.h.dVal<0))
	{
		string strImgi = trGetN.img.strVal;
		Image img;
		okxf_resolve_string_get_origin_object(strImgi, &img);
		pBITMAPHANDLE phBmp = img.GetLBmp();
		
		int nOrgW=phBmp->Width;
		int nOrgH=phBmp->Height;
		
		int nUnit = trGetN.unit.nVal;// 1: percent; 0: pixel
		double dRatio;		
		
		if(trGetN.w.dVal<0)
		{
			if(nUnit == 0)
			{
				dRatio = (double)trGetN.h.dVal/(double)nOrgH; 
				trGetN.w.dVal = (int)(nOrgW * dRatio);
			}
			else
			{
				trGetN.w.dVal = trGetN.h.dVal;
			}
		}
		
		if(trGetN.h.dVal<0)
		{
			if(nUnit == 0)
			{
				dRatio = (double)trGetN.w.dVal/(double)nOrgW; 
				trGetN.h.dVal = (nOrgH * dRatio);
			}
			else
			{
				trGetN.h.dVal = trGetN.w.dVal;
			}
		}		
		
	
	
	}
}
//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 imgResize_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}


// this is called when the GetNImageBox dialog is open
// bUpdateGrid = false when entering this function
// you need to set bUpdateGrid = true if you need to modify trGetN data and want to see the
// grid updated, for example
// trGetN.angle.nVal = 0; // force rotation to init to 0
// bUpdateGrid = true;
// bOKEnable is default = true, you may add code to check trGetN and decide that the OK button should be disabled, then set this to false
// example: 
// if(0==trGetN.angle.nVal) // no change, no point to click Ok to do nothing
//    bOKEnable = false; 
static void GetNImageBox_OnInit(TreeNode& trGetN, ImageControlBase* pimgCntrl, bool& bUpdateGrid, bool& bOKEnable, string& strErrMsg)
{

		
	pBITMAPHANDLE pPreviewBitmap;
	pPreviewBitmap=pimgCntrl->GetBitmapHandlePtr();
	
	///Sandy 2007-5-24 initialize w and h
	int nOrgW=pPreviewBitmap->Width;
	int nOrgH=pPreviewBitmap->Height;	
	
	if(trGetN.w.dVal<0)
		trGetN.w.dVal = (trGetN.unit.nVal==1? 100 :nOrgW);
	if(trGetN.h.dVal<0)
		trGetN.h.dVal = (trGetN.unit.nVal==1? 100 :nOrgH);
	//end
	
	uint nW, nH;
	bOKEnable = _update_settings(trGetN, strErrMsg, nW, nH);
	if(!bOKEnable)
		return;
	

	
	int nRet = _size_image(pPreviewBitmap, nW, nH, trGetN.interpolate.nVal);
	if( SUCCESS != nRet)
	{
		//strErrMsg = nRet;
		bOKEnable = false;
		
		if(nRet == ERROR_NO_MEMORY_FROM_LT)
		    strErrMsg = XF_ERR_NOT_ENOUGH_MEM;		
		else if(nRet < MAX_ERROR_CODE_FROM_LT)
		{
			strErrMsg = XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR;
			strErrMsg += nRet;
		}
		else
			strErrMsg = nRet;
	

	
	}
	

	//int nW = pimgCntrl->GetImageWidth();
	//int nH = pimgCntrl->GetImageHeight();
	//if(nW > 0 && nH > 0)
	//{
		//trGetN.w.nVal = 100;
		//trGetN.h.nVal = 100;
		//bUpdateGrid = true;
	//}
	bUpdateGrid = true;
}

// this is called whenever any controls in the grid is modified by the user
// upon entering function
// bUpdatePreview = true;// to update the preview image pPreviewBitmap
// bUpdateGrid = false; // event triggered by grid change, so default no need to update again
// if you want to modify other controls in the grid, then you need to set bUpdateGrid to true
// bOKEnable is default = true, you may add code to check trGetN and decide that the OK button should be disabled, then set this to false
static void GetNImageBox_OnChange(BITMAPHANDLE* pPreviewBitmap, TreeNode& trGetN, TreeNode& trNode, ImageControlBase* pimgCntrl, bool& bUpdatePreview, bool& bUpdateGrid, bool& bOKEnable, string& strErrMsg)
{

	
	//if(trGetN.lock.nVal==true)
	//{
		//if(trGetN.w.DataID==trNode.DataID)
			//trGetN.h.nVal = trGetN.w.nVal ;
		//
		//if(trGetN.h.DataID==trNode.DataID)
			//trGetN.w.nVal = trGetN.h.nVal ;
//
	//}
	uint nW, nH;
	bOKEnable = _update_settings(trGetN, strErrMsg, nW, nH, trNode);
	if(!bOKEnable)
		return;	
	
	bUpdateGrid = true;
	
	int nRet = _size_image(pPreviewBitmap, nW, nH, trGetN.interpolate.nVal);	

	if( SUCCESS != nRet)
	{
		//strErrMsg = nRet;
		bOKEnable = false;
		
		if(nRet == ERROR_NO_MEMORY_FROM_LT)
		    strErrMsg = XF_ERR_NOT_ENOUGH_MEM;

		else if(nRet < MAX_ERROR_CODE_FROM_LT)
		{
			strErrMsg = XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR;
			strErrMsg += nRet;
		}
		else
			strErrMsg = nRet;	
	

	}
	else
		bUpdatePreview = true;
	
}
	
int GetNImageBox_imgResize(TreeNode& trGetN, int nRow, int nCol, TreeNode& trNode, DWORD dwCntrl, int nType, WndContainer& theDlg, ImageControlBase* pimgCntrl)
{
	return xf_GetNImageBox_event_handler(trGetN, nRow, nCol, trNode, dwCntrl, nType, pimgCntrl,
								GetNImageBox_OnInit, GetNImageBox_OnChange);	
}
