// SampleDlg.cpp : implementation file 6
//

#include "stdafx.h"

#include "Sample.h"
#include "SampleDlg.h"
#include "response.h"
#include "..\..\Lib\iidk.h"
#pragma comment(lib, "..\\..\\Lib\\MSVC\\iidk.lib")


#include "msg.h"
#include <atlbase.h>
#include "tools.h"

#define IP    "IP"
#define PORT  "PORT"
#define ID    "ID"
#define MSG_1 "MSG_1"
#define MSG_2 "MSG_2"




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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CString	m_CompileInfo;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	m_CompileInfo = _T("");
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Text(pDX, IDC_STATIC1, m_CompileInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSampleDlg dialog

CSampleDlg::CSampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSampleDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSampleDlg)
	DDX_Control(pDX, IDC_STATIC2, m_TimeInfo);
	DDX_Control(pDX, IDC_LIST2, m_ListBoxOwn);
	DDX_Control(pDX, IDC_COMBO3, m_ComboPort);
	DDX_Control(pDX, IDC_COMBO2, m_ComboAdd);
	DDX_Control(pDX, IDC_COMBO1, m_Combo);
	DDX_Control(pDX, IDC_BUTTON1, m_Button1);
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Control(pDX, IDC_IPADDRESS1, m_IP);
	DDX_Control(pDX, IDC_EDIT2, m_Id);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSampleDlg, CDialog)
	//{{AFX_MSG_MAP(CSampleDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_LBN_DBLCLK(IDC_LIST1, OnDblclkList1)
	ON_WM_CLOSE()
	ON_WM_MOUSEMOVE()
	ON_LBN_DBLCLK(IDC_LIST2, OnDblclkList2)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSampleDlg message handlers
CListBox list_log;
BOOL CSampleDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	CString str;
	GetWindowText(str);
	str += CString(" [Compiled on ") + __DATE__ + "," + __TIME__ + "]";
	SetWindowText(str);

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	m_ComboPort.AddString("900");
	m_ComboPort.AddString("1030");
	m_ComboPort.AddString("1012");
    m_ComboPort.AddString("1111");



	m_IP.SetWindowText("127.0.0.1");
	m_Id.SetWindowText("12"); 
//-------------------------------------------------------------------------------------------
	m_Combo.AddString("POS|1|MSG1|param0<1>,param1<3>");
	m_Combo.AddString("POS|1|MSG2|param0<Это еще текст!>,param1<er>");
//-------------------------------------------------------------------------------------------
	m_Combo.AddString("CAM|1|REC");
	m_Combo.AddString("CAM|1|REC_STOP");
	m_Combo.AddString("CAM|1|ARM");
	m_Combo.AddString("CAM|1|DISARM");
	m_Combo.AddString("CAM|1|START_VIDEO|register_only<1>");
	m_Combo.AddString("CAM|1|SETUP|params<1>,param0_name<rec_priority>,param0_val<2>");
	m_Combo.AddString("CAM|1|SETUP|params<1>,param0_name<priority>,param0_val<2>");
	m_Combo.AddString("CAM|1|ADD_SUBTITLES|params<1>,param0_name<command>,"
					   "param0_val<{E4{TЭтот текст только через 1030 порт\r>");
	m_Combo.AddString("CAM|1|ADD_SUBTITLES|params<1>,param0_name<command>,"
					   "param0_val<{A>");
	m_Combo.AddString("CAM|1|ADD_SUBTITLES|command<{E2{TЭтот текст только через 900 порт\r>");
//-------------------------------------------------------------------------------------------
	m_Combo.AddString("TELEMETRY|1|CLEAR_PRESET|params<1>,param0_name<preset>,param0_val<1>");
	m_Combo.AddString("TELEMETRY|1|SET_PRESET|params<1>,param0_name<preset>,param0_val<1>");
	m_Combo.AddString("TELEMETRY|1|GO_PRESET|params<1>,param0_name<preset>,param0_val<1>");
//-------------------------------------------------------------------------------------------
	m_Combo.AddString("DIALOG|operator|RUN");
	m_Combo.AddString("DIALOG|operator|CLOSE_ALL");
	m_Combo.AddString("DIALOG|telemetry|SETUP|params<2>,"
		              "param0_name<x>,param0_val<50>,param1_name<y>,param1_val<7>");
	m_Combo.AddString("DIALOG|telemetry|RUN|params<2>,param0_name<address>,param0_val<1>,param1_name<speed>,param1_val<5>");
//-------------------------------------------------------------------------------------------
	m_Combo.AddString("MONITOR|1|KEY_PRESSED|params<1>,param0_name<key>,param0_val<STOP>");
    m_Combo.AddString("MONITOR|1|REPLACE|params<1>,param0_name<cam>,param0_val<1>");
//-------------------------------------------------------------------------------------------
    m_Combo.AddString("ATM|1|EJECT_CARD|param0<1>,param1<2>");
    m_Combo.AddString("ATM|1|INSERT_CARD|param0<1>,param1<2>");
    m_Combo.AddString("ATM|1|CAPTURE_CARD|param0<1>,param1<2>");
    m_Combo.AddString("ATM|1|OPEN_DOOR|param0<1>,param1<2>");
    m_Combo.AddString("ATM|1|CLOSE_DOOR|param0<1>,param1<2>");
    m_Combo.AddString("ATM|1|BILL_TAKEN|param0<1>,param1<2>");
    m_Combo.AddString("ATM|1|DUET_TRX_ERROR|param0<1>,param1<2>");
    m_Combo.AddString("ATM|1|DUET_OPER|param0<1>,param1<2>");
    m_Combo.AddString("ATM|1|DUET_OPER_END|param0<1>,param1<2>");
//-------------------------------------------------------------------------------------------

	int count = m_Combo.GetCount();
	CString s;
	for(int i=0;i<count;i++)
	{
			m_Combo.GetLBText(i,s);
			m_ComboAdd.AddString(s);

	}
    ///////////////////////////////////////////////////////////////////////////
	CRegKey key;
	
	if (key.Open(HKEY_CURRENT_USER,"Software\\Iss\\IIDK\\",KEY_QUERY_VALUE) == ERROR_SUCCESS)
	{

		char st[200];
	    ZeroMemory(st,sizeof(st));
  	    DWORD size = sizeof(st);

		if (key.QueryValue(st,PORT,&size)==ERROR_SUCCESS)
			m_ComboPort.SetWindowText(st);
		size = sizeof(st);
		if (key.QueryValue(st,IP,&size)==ERROR_SUCCESS)
			m_IP.SetWindowText(st);
		size = sizeof(st);
	    if (key.QueryValue(st,MSG_1,&size)==ERROR_SUCCESS)
			m_Combo.SetWindowText(st);
		size = sizeof(st);
		if (key.QueryValue(st,MSG_2,&size)==ERROR_SUCCESS)
			m_ComboAdd.SetWindowText(st);
		size = sizeof(st);
		if (key.QueryValue(st,ID,&size)==ERROR_SUCCESS)
			m_Id.SetWindowText(st);
		///
		key.Close();
	}
	///////////////////////////////////////////////////////////////////////////

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSampleDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.m_CompileInfo = CString("Compiled on ") + __DATE__ + "," + __TIME__;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSampleDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


CString port;
CString ip;
CString id;

BOOL IsConnect = false;


void CSampleDlg::OnButton1() 
{
	
	// TODO: Add your control notification handler code here
	CResponse *res = new CResponse(m_TimeInfo.m_hWnd);
	if (!IsConnect)
	{
  	  m_List.ResetContent();
	  m_ListBoxOwn.ResetContent();
      
	  m_ComboPort.GetWindowText(port);
	  m_IP.GetWindowText(ip);
	  m_Id.GetWindowText(id);

	  port = port.IsEmpty() ? "900" : port;
	  if ( port == "1012") id = "";


	  IsConnect = Connect(ip,port,id,MyFunc);
	  delete res;
	  if (!IsConnect)	
	  {
		  AfxMessageBox("Error");
	  }
	}
	else
	if (IsConnect)
	{
		Disconnect(id);
		delete res;
		IsConnect = false;
	}
	m_Button1.SetWindowText( IsConnect? "Disconnect" : "Connect");
}


void CSampleDlg::OnButton2() 
{
	if (IsConnect)
	{
		CString msg;
		m_Combo.GetWindowText(msg);
		if (port == "1030") 
		{ 
			Msg f;
		    f.StringToMsg(msg);
			msg = ActiveXMsg(msg) + "," + f.ParamsToString();
		}
		m_ListBoxOwn.AddString(msg);
		SendMsg(id,msg);	
	}
  }


void CSampleDlg::OnButton3() 
{
	if (IsConnect)
	{
		CString msg;
		m_ComboAdd.GetWindowText(msg);
		if (port == "1030") 
		{ 
			Msg f;
		    f.StringToMsg(msg);
			msg = ActiveXMsg(msg) + "," + f.ParamsToString();
		}
		m_ListBoxOwn.AddString(msg);
		SendMsg(id,msg);
	}
}


// CALLBACK messages from Inspector+

void _stdcall CSampleDlg::MyFunc(LPCTSTR str)
{
	HWND parent = AfxGetApp()->m_pMainWnd->m_hWnd;
	::SendMessage((HWND)::GetDlgItem(parent,IDC_LIST1),LB_ADDSTRING,0,(LPARAM)str);

	::SendMessage((HWND)::GetDlgItem(parent,IDC_LIST2),WM_VSCROLL,MAKEWORD(SB_BOTTOM,0),0);
	::SendMessage((HWND)::GetDlgItem(parent,IDC_LIST1),WM_VSCROLL,MAKEWORD(SB_BOTTOM,0),0);

	if ( strcmp(str,"DISCONNECTED")==0)
	{
		if (IsConnect)
		{
			Disconnect(id);
			IsConnect = false; 
			::SendMessage((HWND)::GetDlgItem(parent,IDC_BUTTON1)
				          ,WM_SETTEXT,0,(LPARAM)"Connect");
		}
	}
}

void CSampleDlg::OnDblclkList1() 
{
	CString s;
	m_List.GetText(m_List.GetCurSel(),s);
	MessageBox(s);
}

void CSampleDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if (IsConnect)
	{
		Disconnect(id);
		IsConnect = false;
	}

	///////////////////////////////////////////////////////////////////////////
	CRegKey key;
	if (key.Create(HKEY_CURRENT_USER,"Software\\Iss\\IIDK\\") == ERROR_SUCCESS)
	{
		CString str;
		m_ComboPort.GetWindowText(str);
		key.SetValue(str,PORT);
		m_Combo.GetWindowText(str);
		key.SetValue(str,MSG_1);
		m_ComboAdd.GetWindowText(str);
		key.SetValue(str,MSG_2);
        m_IP.GetWindowText(str);
		key.SetValue(str,IP);
		m_Id.GetWindowText(str);
	    key.SetValue(str,ID);
		///
		key.Close();
		
	}
	///////////////////////////////////////////////////////////////////////////
	CDialog::OnClose();
}

void CSampleDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    const int SC_DRAGMOVE = 0xF012;
	if(nFlags & MK_LBUTTON)
	{
	   ReleaseCapture();
	   SendMessage(WM_SYSCOMMAND, SC_DRAGMOVE, 0);
	}
	
	CDialog::OnMouseMove(nFlags, point);
}


BOOL CSampleDlg::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == 27) 
	{
		SendMessage(WM_CLOSE,0,0);
		return TRUE;
	} else

  
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == 13) 
	{
      char lpClassName[MAX_PATH];
	  GetClassName(pMsg->hwnd,lpClassName,sizeof(lpClassName));
 
	  if ( _stricmp(lpClassName,"Button") != 0 )
	  {
		 return TRUE;
	  }
	} 
	return CDialog::PreTranslateMessage(pMsg);
}

void CSampleDlg::OnDblclkList2() 
{
	CString s;
	m_ListBoxOwn.GetText(m_ListBoxOwn.GetCurSel(),s);
	MessageBox(s);
}

void CSampleDlg::OnButton4() 
{
}

void CSampleDlg::OnButton5() 
{
}



void CSampleDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnTimer(nIDEvent);
}
