// dll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "dll.h"
#include "..\intellect\nissdlle.h"

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

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CDllApp

BEGIN_MESSAGE_MAP(CDllApp, CWinApp)
	//{{AFX_MSG_MAP(CDllApp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDllApp construction

CDllApp::CDllApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDllApp object

CDllApp theApp;

class NissObjectDemo : public NissObjectDLLExt
{
private:
	CString Id;
public:
	NissObjectDemo(CoreInterface* core) : NissObjectDLLExt(core) {}
	virtual CString GetParentType()  { return "SLAVE"; }
	virtual CString GetObjectType()  { return "DEMO"; }
	virtual BOOL HasChild() { return TRUE; }
	virtual UINT HasSetupPanel()	 { return TRUE; }
	virtual int GetPos()  { return -1; }
	virtual CString GetPort() { return "1100"; }
	virtual CString GetProcessName() { return "demo"; }
	virtual CString GetDeviceType() { return ""; }
	virtual void OnInit(Msg& msg)
	{
		Id = msg.GetSourceId();

		OnChange(msg, msg);
	}
	virtual void OnCreate(Msg& msg)
	{
		msg.SetParam("port","COM1");
	}
	virtual void OnChange(Msg& msg, Msg& prev)
	{
		React react(msg.GetSourceType(),msg.GetSourceId(),"INIT");
		react.SetParam("port",msg.GetParam("port"));
		m_pCore->DoReact(react);
	}
	virtual void OnDelete(Msg& msg) 
	{
		React react(msg.GetSourceType(),msg.GetSourceId(),"EXIT");
		m_pCore->DoReact(react);
	}
	virtual void OnEnable(Msg& msg)  {};
	virtual void OnDisable(Msg& msg) {};
	virtual void OnPanelLoad(CWnd* pwnd,Msg& params)
	{
		CString s;

		s = params.GetParam("port");
		pwnd->GetDlgItem(IDC_PORT)->SetWindowText(s);
	}
	virtual void OnPanelSave(CWnd* pwnd,Msg& params)
	{
		CString s;

		pwnd->GetDlgItem(IDC_PORT)->GetWindowText(s);
		params.SetParam("port",s);
	}
	virtual void OnPanelButtonPressed(CWnd* pwnd,UINT id)
	{
		React react("DEMO",Id,"TEST");

		if(id==IDC_TEST)
		 m_pCore->DoReact(react);
	}
};

class NissObjectDemoDevice : public NissObjectDLLExt
{
public:
	NissObjectDemoDevice(CoreInterface* core) : NissObjectDLLExt(core) {}
	virtual CString GetParentType()  { return "DEMO"; }
	virtual CString GetObjectType()  { return "DEMO_DEVICE"; }
	virtual BOOL HasChild() { return FALSE; }
	virtual UINT HasSetupPanel()	 { return TRUE; }
	virtual int GetPos()  { return -1; }
	virtual CString GetPort() { return "1100"; }
	virtual CString GetProcessName() { return "demo"; }
	virtual CString GetDeviceType() { return ""; }
	virtual void OnInit(Msg& msg)
	{
		OnChange(msg, msg);
	}
	virtual void OnCreate(Msg& msg)
	{
		msg.SetParam("address","1");
	}
	virtual void OnChange(Msg& msg, Msg& prev)
	{
		React react(msg.GetSourceType(),msg.GetSourceId(),"SETUP_DEVICE");
		react.SetParam("address",msg.GetParam("address"));
		m_pCore->DoReact(react);
	}
	virtual void OnDelete(Msg& msg)
	{
		React react(msg.GetSourceType(),msg.GetSourceId(),"SETUP_DELDEVICE");
		react.SetParam("address",msg.GetParam("address"));
		m_pCore->DoReact(react);
	}
	virtual void OnEnable(Msg& msg)  {};
	virtual void OnDisable(Msg& msg) {};
	virtual void OnPanelLoad(CWnd* pwnd,Msg& params)
	{
		CString s;

		//s = params.GetSourceId();
		//pwnd->GetDlgItem(IDC_NUMBER)->SetWindowText(s);

		//s = params.GetParam("name");
		//pwnd->GetDlgItem(IDC_NAME)->SetWindowText(s);

		s = params.GetParam("address");
		pwnd->GetDlgItem(IDC_ADDRESS)->SetWindowText(s);
	}
	virtual void OnPanelSave(CWnd* pwnd,Msg& params)
	{
		CString s;

		//pwnd->GetDlgItem(IDC_NAME)->GetWindowText(s);
		//params.SetParam("name",s);

		pwnd->GetDlgItem(IDC_ADDRESS)->GetWindowText(s);
		params.SetParam("address",s);
	}
};

CNissObjectDLLExtArray* APIENTRY CreateNissObject(CoreInterface* core)
{
	CNissObjectDLLExtArray* ar = new CNissObjectDLLExtArray;

	ar->Add(new NissObjectDemo(core));
	ar->Add(new NissObjectDemoDevice(core));

	return ar;
}
