// XApiDlg.cpp : implementation file
//

#include "stdafx.h"
#include "XApi.h"
#include "XApiDlg.h"
#include "XApic.h"
#include "XApicmdr.h"
#include "XApiadj.h"

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

/////////////////////////////////////////////////////////////////////////////
// CXApiDlg dialog

CXApiDlg::CXApiDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CXApiDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CXApiDlg)
    m_acc = _T("");
    m_ip_adr = _T("");
    m_adjunct_ip_adr = _T("");
    //}}AFX_DATA_INIT
}

void CXApiDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CXApiDlg)
    DDX_Text(pDX, IDC_EDIT_ACC, m_acc);
    DDX_Text(pDX, IDC_EDIT_URL, m_ip_adr);
    DDX_Text(pDX, IDC_EDIT_URL_ADJUNCT, m_adjunct_ip_adr);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CXApiDlg, CDialog)
    //{{AFX_MSG_MAP(CXApiDlg)
    ON_WM_SHOWWINDOW()
    ON_BN_CLICKED(IDC_BUTTON_NEW_CLIENT, OnButtonNewClient)
    ON_BN_CLICKED(IDC_BUTTON_NEW_CDR, OnButtonNewCdr)
    ON_BN_CLICKED(IDC_BUTTON_NEW_ADJ, OnButtonNewAdj)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CXApiDlg message handlers

BOOL CXApiDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    return TRUE;
}

void CXApiDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
    CDialog::OnShowWindow(bShow, nStatus);
}


void CXApiDlg::OnButtonNewAdj()
{
    // Be sure that Xymphony "Ethernet/Adjunct/Adjunct server IP address" is set to the 
    // IP address of the PC running this sw

    UpdateData (TRUE);

    if (m_adjunct_ip_adr != "")
    {
        XApiadj * pXApiadj = new XApiadj;
        if (!pXApiadj)
        {
            return;
        }
        BOOL ret = pXApiadj->Create (IDD_XAPI_ADJUNCT, this);
        if(!ret)
        {
            delete pXApiadj;
            return;
        }

        // Xymphony side port number can be set by "Ethernet/Adjunct/Adjunct server port"
        pXApiadj->handle = XAPI_Adjunct_start (pXApiadj->m_hWnd, (LPCSTR)m_adjunct_ip_adr, Xapi_default_adjunct_port);

        if (pXApiadj->handle == 0)
        {
            delete pXApiadj;
            return;
        }
        pXApiadj->SetWindowText ("Adjunct " + m_adjunct_ip_adr);
        pXApiadj->ShowWindow(SW_SHOW);
    }
}

void CXApiDlg::OnButtonNewCdr()
{
    // Be sure that Xymphony "Ethernet/Cmdr/Xtools Cmdr client IP address" is set to the 
    // IP address of the PC running this sw

    UpdateData (TRUE);

    if (m_ip_adr != "")
    {
        XApicmdr * pXApicmdr = new XApicmdr;
        if (!pXApicmdr)
        {
            return;
        }
        BOOL ret = pXApicmdr->Create (IDD_XAPI_CMDR, this);
        if(!ret)
        {
            delete pXApicmdr;
            return;
        }

        // Xymphony side port number can be set by "Ethernet/Cmdr/XtoolsMonitor(UDP)Cmdr(TCP)port"
        pXApicmdr->handle = XAPI_Cmdr_start (pXApicmdr->m_hWnd, (LPCSTR)m_ip_adr, Xapi_default_cmdr_port);

        if (pXApicmdr->handle == 0)
        {
            delete pXApicmdr;
            return;
        }
        pXApicmdr->SetWindowText ("Cmdr " + m_ip_adr);
        pXApicmdr->ShowWindow(SW_SHOW);
    }
}

void CXApiDlg::OnButtonNewClient() 
{
    // Be sure that the user of access code a_c is either an analog subscriber or ISDN 4B1D
    // and "Common properties/Companion IP address" of this user is set to the 
    // IP address of the PC running this sw

    UpdateData (TRUE);

    unsigned a_c = StrToAcc (m_acc);

    if (a_c != 0xffffffff)
    {
        XApic * pXApic = new XApic;
        if (!pXApic)
        {
            return;
        }
        BOOL ret = pXApic->Create (IDD_XAPI_CLIENT, this);
        if(!ret)
        {
            delete pXApic;
            return;
        }
        pXApic->handle = XAPI_Client_start (pXApic->m_hWnd, a_c);
        if (pXApic->handle == 0)
        {
            delete pXApic;
            return;
        }
        pXApic->SetWindowText (AccToStrNoSpace (a_c));
        pXApic->ShowWindow(SW_SHOW);
    }
}
