// demoSetServerAddressDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "resource.h"
#include "demoSetServerAddressDlg.h"
#include "afxdialogex.h"
#include "demoTools.h"
#include "HwmSdkAgent.h"

// demoSetServerAddressDlg 对话框

IMPLEMENT_DYNAMIC(demoSetServerAddressDlg, CDialogEx)

demoSetServerAddressDlg::demoSetServerAddressDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_SET_SERVER_ADDRESS_DIALOG, pParent)
{

}

demoSetServerAddressDlg::~demoSetServerAddressDlg()
{
}

void demoSetServerAddressDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT_SET_SERVER_ADDRESS, m_serverEdit);
    DDX_Control(pDX, IDC_EDIT_SERVER_PORT, m_portEdit);
}

// demoLoginDlg 消息处理程序

BOOL demoSetServerAddressDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // 从配置文件读取上次登录帐号
    CString temp = _T("");
    CTools::getIniConfigParam(_T("LoginConfig"), _T("loginServer"), temp);
    m_serverEdit.SetWindowText(temp);
    CTools::getIniConfigParam(_T("LoginConfig"), _T("loginPort"), temp);
    m_portEdit.SetWindowText(temp);

    return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

BEGIN_MESSAGE_MAP(demoSetServerAddressDlg, CDialogEx)
    ON_BN_CLICKED(IDC_BUTTON_SET_SERVER_ADDRESS_OK, &demoSetServerAddressDlg::OnBnClickedButtonOk)
END_MESSAGE_MAP()


// demoSetServerAddressDlg 消息处理程序


void demoSetServerAddressDlg::OnBnClickedButtonOk()
{
    CString serverCString;
    CString portCString;
    m_serverEdit.GetWindowText(serverCString);
    m_portEdit.GetWindowText(portCString);

    hwmsdkagent::HwmServerInfo serverInfo;
    memset(&serverInfo, 0, sizeof(hwmsdkagent::HwmServerInfo));

    string server = CTools::UNICODE2UTF(serverCString.GetString());
    strncpy_s(serverInfo.serverAddr, server.c_str(), sizeof(serverInfo.serverAddr) - 1);
    serverInfo.serverPort = _ttoi(portCString);
    int ret = hwmsdkagent::SetServerAddress(&serverInfo);
    if (hwmsdk::HWM_COMMON_SUCCESS != ret)
    {
        CTools::OutputRetStr("Set server failed");
        return;
    }

    //接口调用成功后将登录信息保存到配置文件，下次启动读取
    CTools::writeIniConfigParam(_T("LoginConfig"), _T("loginServer"), serverCString);
    CTools::writeIniConfigParam(_T("LoginConfig"), _T("loginPort"), portCString);

    // TODO: 在此添加控件通知处理程序代码
    CDialogEx::OnOK();
}
