// XApicmdr.cpp : implementation file
//

#include "stdafx.h"
#include "XApi.h"
#include "XApicmdr.h"

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

/////////////////////////////////////////////////////////////////////////////
// XApicmdr dialog


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

    handle = 0;
}


void XApicmdr::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(XApicmdr)
    //}}AFX_DATA_MAP
    DDX_Control(pDX, IDC_LIST_CMDR, m_list);
}


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

/////////////////////////////////////////////////////////////////////////////
// XApicmdr message handlers

void XApicmdr::PostNcDestroy()
{
    if (handle)
    {
        XAPI_Cmdr_stop (handle);
        handle = 0;
    }

    CDialog::PostNcDestroy();
    delete this;
}

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

void XApicmdr::XapiCmdrStateChange (PCMDR pcmdr)
{
    if (pcmdr->ConnectionState != Cmdr_ConnectionState_connected)
    {
        displayStr = pcmdr->disconnection_cause;
        m_list.ResetContent ();
        m_list.AddString (displayStr);
        m_list.EnableWindow (FALSE);
    }
}

void XApicmdr::XapiCmdrRecord (XAPI_cmdr_recordS * record)
{
    CString mStr;

    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 += (char*)record->xipadr;
    displayStr += ' ';

    switch (record->type)
    {
    case 'A':
        {  // Call records
            displayStr.Insert (0, "A");

            switch (record->Cflg.ForwardingUsed)
            {
            case Fu_LastNumberRedial:
                {
                    displayStr += "PlDynm";
                    break;
                }
            case Fu_UserSpeedDialMemory:
                {
                    displayStr += "PlUser";
                    break;
                }
            case Fu_HotLine:
                {
                    displayStr += "PlSHot";
                    break;
                }
            case Fu_UserHotLine:
                {
                    displayStr += "PlUHot";
                    break;
                }
            case Fu_RedialOnIncompleteDialling:
                {
                    displayStr += "PlIcmp";
                    break;
                }
            case Fu_RedialOnDestinationBusy:
                {
                    displayStr += "PlBusy";
                    break;
                }
            case Fu_RedialOnAlertingTimeout:
                {
                    displayStr += "PlAlert";
                    break;
                }

            case Fu_Deflected:
                {
                    displayStr += "UCdefl";
                    break;
                }
            case Fu_UserUnconditionalForwarding:
                {
                    displayStr += "UCdivU";
                    break;
                }
            case Fu_UserBusyForwarding:
                {
                    displayStr += "UCdivB";
                    break;
                }
            case Fu_UserNoanswerForwarding:
                {
                    displayStr += "UCdivN";
                    break;
                }
            case Fu_SysUnconditionalForwarding:
                {
                    displayStr += "SCdivU";
                    break;
                }
            case Fu_SysBusyForwarding:
                {
                    displayStr += "SCdivB";
                    break;
                }
            case Fu_SysNoanswerForwarding:
                {
                    displayStr += "SCdivN";
                    break;
                }
            case Fu_H323XferToUser:
                {
                    displayStr += "H323Xu";
                    break;
                }
            case Fu_H323XferToDigitSequence:
                {
                    displayStr += "H323Xf";
                    break;
                }

            case Fu_CallXfer:
                {
                    displayStr += "UCxfrA";
                    break;
                }
            case Fu_CallBack:
                {
                    displayStr += "CalBck";
                    break;
                }
            case Fu_UserRouteForwarding:
                {
                    displayStr += "UCdivR";
                    break;
                }
            case Fu_CallXferBusy:
                {
                    displayStr += "UCxfrB";
                    break;
                }
            case Fu_CallXferAlrting:
                {
                    displayStr += "UCxfrN";
                    break;
                }
            case Fu_AltRouting:
                {
                    displayStr += "AltRtg";
                    break;
                }
            
            }

            displayStr += ' ';
            mStr.Format ("%i", record->duration);
            displayStr += mStr;

            displayStr += ' ';
            mStr.Format (" %i %i", record->rpuls, record->ppuls);
            displayStr += mStr;

            displayStr += ' ';
            displayStr += AccToStrNoSpace (record->cport);

            displayStr += ' ';
            displayStr += AccToStrNoSpace (record->pport);

            displayStr += ' ';
            displayStr += AccToStrNoSpace (record->uport);

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

            displayStr += ' ';
            displayStr += (char*)record->digits.xdata;

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

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

            break;
        }
    case 'B':
        { // System related (information and warning) records
            displayStr.Insert (0, "B");
            displayStr += (char*)record->info;
            break;
        }
    case 'C':
        { // User operated Service records
            displayStr.Insert (0, "C");

            displayStr += AccToStrNoSpace (record->cport);

            displayStr += ' ';
            displayStr += AccToStrNoSpace (record->uport);

            displayStr += ' ';
            displayStr += (char*)record->info;
            break;
        }
    case 'D':
        { // Malicious call records
            displayStr.Insert (0, "D");

            displayStr += AccToStrNoSpace (record->cport);

            displayStr += ' ';
            displayStr += AccToStrNoSpace (record->uport);

            break;
        }
    case 'E':
        { // Room service reports (for hotel applications)
            displayStr.Insert (0, "E");

            displayStr += AccToStrNoSpace (record->cport);

            mStr.Format (" %i %i", record->rpuls, record->ppuls);
              // Room report (default 8311), item number and quantity

            displayStr += mStr;
            break;
        }
    default:
        { // unknown record
            return;
        }
    }

    m_list.AddString (displayStr);
}

BOOL XApicmdr::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_CMDR_STATE_CHANGE:
                {
                    XapiCmdrStateChange ((PCMDR)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_CMDR_RECORD:
                {
                    XapiCmdrRecord ((XAPI_cmdr_recordS *)pMsg->lParam);
                    break;
                }
            }
            return TRUE;
        }
    }
    return CDialog::PreTranslateMessage(pMsg);
}
