// XApiadj.cpp : implementation file
//

#include "stdafx.h"
#include "xapi.h"
#include "XApiadj.h"

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

/////////////////////////////////////////////////////////////////////////////
// XApiadj dialog


XApiadj::XApiadj(CWnd* pParent /*=NULL*/)
	: CDialog(XApiadj::IDD, pParent)
{
    //{{AFX_DATA_INIT(XApiadj)
    m_digits = _T("");
    m_pulses = 0;
    //}}AFX_DATA_INIT

    handle = 0;
}


void XApiadj::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(XApiadj)
    DDX_Text(pDX, IDC_EDITDIGITS, m_digits);
    DDX_Text(pDX, IDC_EDITPULSES, m_pulses);
    //}}AFX_DATA_MAP
    DDX_Control(pDX, IDC_LIST_ADJUNCT, m_list);
}


BEGIN_MESSAGE_MAP(XApiadj, CDialog)
    //{{AFX_MSG_MAP(XApiadj)
    ON_WM_CLOSE()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// XApiadj message handlers

void XApiadj::PostNcDestroy()
{
    if (handle)
    {
        XAPI_Adjunct_stop (handle);
        handle = 0;
    }

    CDialog::PostNcDestroy();
    delete this;
}

void XApiadj::OnClose() 
{
    CDialog::OnClose();
    CDialog::DestroyWindow ();
}


void XApiadj::XapiAdjunctStateChange (PADJUNCT padjunct)
{
    if (padjunct->ConnectionState != Adjunct_ConnectionState_connected)
    {
        displayStr = padjunct->disconnection_cause;
        m_list.ResetContent ();
        m_list.AddString (displayStr);
        m_list.EnableWindow (FALSE);
    }
}

void XApiadj::XapiAdjunctRecord (XAPI_adjunct_recordS * record)
{
    UpdateData (TRUE);

    displayStr.Format (" %i/%i/%i %i:%i:%i ",\
    record->time.year, record->time.month, record->time.day,\
    record->time.hour, record->time.minute, record->time.second);
    displayStr += ' ';

    displayStr += AccToStrNoSpace (record->cport);

    displayStr += ' ';
    displayStr += (char*)record->rdata;

    displayStr += ' ';
    displayStr += (char*)record->cdata;

    m_list.AddString (displayStr);

    XAPI_Adjunct_response (handle, record->refno, (LPCSTR)m_digits, m_pulses);
}

BOOL XApiadj::PreTranslateMessage(MSG* pMsg)
{
    switch (pMsg->message)
    {
    case WM_KEYFIRST:
        {
            if ((pMsg->wParam == 0xd)\
             || (pMsg->wParam == 0x1b))
            { // enter or escape terminates program, disable it
                return TRUE;
            }
            break;
        }
    case XAPILNK_WM:
        {
            switch (pMsg->wParam)
            {
            case XAPILNK_WPARAM_ADJUNCT_STATE_CHANGE:
                {
                    XapiAdjunctStateChange ((PADJUNCT)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_ADJUNCT_RECORD:
                {
                    XapiAdjunctRecord ((XAPI_adjunct_recordS *)pMsg->lParam);
                    break;
                }
            }
            return TRUE;
        }
    }
    return CDialog::PreTranslateMessage(pMsg);
}

