/******************************************************************************\
 *  This sample is supplied as is with no implied warranty.  
 *  It is designed to assist you in using the Cisco AnyConnect VPN API. 
 *  It is assumed that you will build a production application and 
 *  refer to this sample as a reference only.
 \*****************************************************************************/

#include "stdafx.h"
#include "Gui.h"
#include "MainDialog.h"
#include "GUIClientImpl.h"
#include <list>
#include "PromptDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using std::list;
typedef list<PromptEntry *> PromptEntryList;

/**
 * @file
 *
 * The complete source for this file is available in the example directories
 * contained with the API bundle.
 *
 */

MainDialog :: MainDialog(Gui* pParent /*=NULL*/)
            : CDialog(MainDialog::IDD, NULL),
              m_pParent(pParent),
              m_hostName(_T("")),
              m_pApiImpl(NULL) { }

void MainDialog :: DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_CBString(pDX, IDC_COMBO1, CString(m_hostName.c_str()));
}

BEGIN_MESSAGE_MAP(MainDialog, CDialog)
	ON_WM_SYSCOMMAND()
    ON_CBN_SELCHANGE(IDC_COMBO1, &MainDialog::OnCbnSelchangeCombo1)
    ON_BN_CLICKED(IDOK, &MainDialog::OnBnClickedOk)
    ON_MESSAGE(MF_NEWEVENT, &MainDialog::OnEventAvailable)
    ON_BN_CLICKED(IDCANCEL, &MainDialog::OnBnClickedCancel)
    ON_BN_CLICKED(IDC_DISCONNECT, &MainDialog::OnBnClickedDisconnect)
END_MESSAGE_MAP()


// MainDialog message handlers

BOOL MainDialog :: OnInitDialog()
{
    m_pApiImpl = new GUIClientImpl(this);
    m_hostName = m_pApiImpl->getDefaultHostName();   

	CDialog::OnInitDialog();
    m_statusBar = new CStatusBarCtrl();
    m_statusBar->Create(WS_CHILD|WS_VISIBLE,CRect(0,0,0,0), this, IDC_STATUS_BAR);
    m_statusBar->SetSimple( true );
   
    if (!m_pApiImpl->attach())
    {
        ::AfxMessageBox(_T("Failed to contact vpn service"));
        PostMessage(WM_QUIT, 0, 0);
        exit(1);
    }
	return TRUE;  
}

void MainDialog :: OnSysCommand(UINT nID, LPARAM lParam)
{
		CDialog::OnSysCommand(nID, lParam);
}

LRESULT MainDialog :: OnEventAvailable(WPARAM wparam, LPARAM lparam)
{
    m_pApiImpl->ProcessEvents();
    return 0;
}

void MainDialog :: OnBnClickedOk()
{
    UpdateData(true);
    if(!m_pApiImpl->isConnected())
        m_pApiImpl->connect(m_hostName);
    else
        showStatusbarText(_T("Connected"));
}

void MainDialog :: OnBnClickedCancel()
{
    OnCancel();

}

void MainDialog :: OnBnClickedDisconnect()
{
    if(m_pApiImpl->isConnected())
        m_pApiImpl->disconnect();
    else
        showStatusbarText(_T("Disconnected"));
}

void MainDialog :: OnCbnSelchangeCombo1()
{
    // TODO: Add your control notification handler code here
}


void MainDialog :: HandleEventAvailable()
{
    ::PostMessage( this->m_hWnd , MF_NEWEVENT, 0, 0);
}

void MainDialog :: HandlePromptCB( ConnectPromptInfo &ConnectPrompt )
{
    m_pPromptInfo = &ConnectPrompt;
    
    // get the list of PromptEntry received.
    //
    ConnectPrompt.getListPromptNames(m_promptNames);

    PromptDialog pdlg(this);
    m_promptDialog = &pdlg;

    INT_PTR nResponse = m_promptDialog->DoModal();
    if (nResponse == IDOK)
	{

	}
	else if (nResponse == IDCANCEL)
	{
        ::AfxMessageBox(_T("Connection cancelled"));
	}

}

void MainDialog :: showStatusbarText (const tstring &text)
{
    m_statusBar->SetText( text.c_str(), 255, 0);    
}


