// MemberInfoDlg_ValidationInterval.cpp : implementation file
//

#include "stdafx.h"
#include "AsCfgBuilder.h"
#include "MemberInfoDlg_ValidationInterval.h"
#include "XmlMemberInfo.h"
#include "AsUtils.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMemberInfoDlg_ValidationInterval dialog


CMemberInfoDlg_ValidationInterval::CMemberInfoDlg_ValidationInterval(CWnd* pParent /*=NULL*/)
	: CPropertyEditorDlg(CMemberInfoDlg_ValidationInterval::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMemberInfoDlg_ValidationInterval)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMemberInfoDlg_ValidationInterval::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMemberInfoDlg_ValidationInterval)
	DDX_Control(pDX, IDC_TXT_MIN_VALUE, txtMinValue_);
	DDX_Control(pDX, IDC_TXT_MAX_VALUE, txtMaxValue_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMemberInfoDlg_ValidationInterval, CPropertyEditorDlg)
	//{{AFX_MSG_MAP(CMemberInfoDlg_ValidationInterval)
		// NOTE: the ClassWizard will add message map macros here
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMemberInfoDlg_ValidationInterval message handlers
void CMemberInfoDlg_ValidationInterval::SetData(CXmlBaseElement* element)
{
	memberInfo_ = dynamic_cast<CXmlMemberInfo*>(element);
	
	DisplayInfo();
}

void CMemberInfoDlg_ValidationInterval::StoreData()
{
	if (IntervalValidationAllowed()) {
		double minValue;
		double maxValue;
		
		minValue = txtMinValue_.GetValue();
		maxValue = txtMaxValue_.GetValue();
		if (minValue >= maxValue)
			minValue = maxValue - 1;

		memberInfo_->GetValidationInfo()->GetIntervalInfo()->SetMinValue(minValue);
		memberInfo_->GetValidationInfo()->GetIntervalInfo()->SetMaxValue(maxValue);
	}
}

void CMemberInfoDlg_ValidationInterval::OnDataUpdate()
{
	DisplayInfo();
}

void CMemberInfoDlg_ValidationInterval::DisplayInfo()
{

	if (IntervalValidationAllowed()) {
		int precision = 0;
		
		if (memberInfo_->GetDataType() == enDataType_Double)
			precision = memberInfo_->GetDecimalPlaces();

		txtMinValue_.SetPrecision(precision);
		txtMinValue_.SetValue(memberInfo_->GetValidationInfo()->GetIntervalInfo()->GetMinValue());
		
		txtMaxValue_.SetPrecision(precision);
		txtMaxValue_.SetValue(memberInfo_->GetValidationInfo()->GetIntervalInfo()->GetMaxValue());

		txtMinValue_.EnableWindow(TRUE);
		txtMaxValue_.EnableWindow(TRUE);
	} else if (memberInfo_ == NULL) {
		txtMinValue_.SetWindowText(_T(""));
		txtMaxValue_.SetWindowText(_T(""));

		txtMinValue_.EnableWindow(FALSE);
		txtMaxValue_.EnableWindow(FALSE);
	} else {
		txtMinValue_.EnableWindow(FALSE);
		txtMaxValue_.EnableWindow(FALSE);
	}
}

bool CMemberInfoDlg_ValidationInterval::IntervalValidationAllowed()
{
	bool ret = true;
	enDataType dataType = enDataType_None;

	if (memberInfo_)
		dataType = memberInfo_->GetDataType();

	ret &= (memberInfo_ != NULL);
	ret &= (dataType == enDataType_Long || dataType == enDataType_Double);
		
	return ret;
}
