#include "DialogForm.h"

DialogForm::DialogForm(SCStudyInterfaceRef sc) : Study(&sc) {}

DialogForm::~DialogForm() {
	delete _windowGuiFactory;
}

void DialogForm::DoInit() {

	RECT r = { 100,100,1100,600 };

	// either create a new window or draw to existing study 
	if (OwnWindow->GetYesNo()) {
		// the gui factory is initialised with the HWND of either the new window or the sc.ChartWindowHandle as the target for rendering
		_window = GuiControl->CreateWindowFrame(r);
		_windowGuiFactory = new WindowsGUIFactory(_window->Hwnd(), _sc);
	} else {
		_windowGuiFactory = GuiFactory;
	}

	// create common controls
	_txt1 = new EditBox(r.left + 10, r.top + 10, 100, 30, "Label1", _windowGuiFactory);
	_txt2 = new EditBox(r.left + 10, r.top + 40, 100, 30, "Label2", _windowGuiFactory);
	_btn = new Button(r.left + 100, r.top + 100, 80, 30, "Button text", _windowGuiFactory);
}

void DialogForm::DoCleanUp() {
	delete _btn;
	delete _txt1, _txt2;
}

// Setup of study input values
void DialogForm::SetReferences(SCPtr sc_) {
	_sc = sc_;

	StudyRef = &(_sc->Input[0]);
	StudyInputVal = &(_sc->Input[1]);
	OwnWindow = &(_sc->Input[2]]);
}

// setup of sc defaults
void DialogForm::DoSetDefaults() {
	_sc->GraphName = "Window Meter";
	_sc->StudyDescription = "Window meter control";
	_sc->AutoLoop = 0;
	_sc->GraphRegion = 0;

	StudyRef->Name = "Study to control";
	StudyRef->SetChartStudyValues(_sc->ChartNumber, 3);

	StudyInputVal->Name = "Input parameter Id";
	StudyInputVal->SetDescription("Input parameter number from the input page(ln:8)");
	StudyInputVal->SetInt(7);
	
	OwnWindow->Name = "Draw in new window?";
	OwnWindow->SetDescription("Draw dialog components to a new window or directly onto study");
	OwnWindow->SetYesNo(0);
}

// Entry point into study execution
void DialogForm::DoStudy() {

	// Check button is pressed
	if (_btn->IsClicked()) {
		// update the study text value with the contents of _txt1
		_sc->SetChartStudyInputString(StudyRef->GetChartNumber(), StudyRef->GetStudyID(), StudyInputVal->GetInt(), _txt1->GetText());
	}

}