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

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

// demoSetGlobalGuaranteedIpAddressesDlg 对话框

IMPLEMENT_DYNAMIC(demoSetGlobalGuaranteedIpAddressesDlg, CDialogEx)

demoSetGlobalGuaranteedIpAddressesDlg::demoSetGlobalGuaranteedIpAddressesDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_SET_GLOBAL_GUARANTEEDIP_DIALOG, pParent)
{

}

demoSetGlobalGuaranteedIpAddressesDlg::~demoSetGlobalGuaranteedIpAddressesDlg()
{
}

void demoSetGlobalGuaranteedIpAddressesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT_GLOBAL_DOMAIN, m_domainEdit);
    DDX_Control(pDX, IDC_EDIT_GLOBAL_GUARANTEEDIPS, m_guaranteedIpsEdit);
    DDX_Control(pDX, IDC_EDIT_GLOBAL_DOMAIN1, m_domain1Edit);
    DDX_Control(pDX, IDC_EDIT_GLOBAL_GUARANTEEDIPS1, m_guaranteedIps1Edit);
    DDX_Control(pDX, IDC_EDIT_GLOBAL_DOMAIN2, m_domain2Edit);
    DDX_Control(pDX, IDC_EDIT_GLOBAL_GUARANTEEDIPS2, m_guaranteedIps2Edit);
}

BEGIN_MESSAGE_MAP(demoSetGlobalGuaranteedIpAddressesDlg, CDialogEx)
    ON_BN_CLICKED(IDOK, &demoSetGlobalGuaranteedIpAddressesDlg::OnBnClickedButtonOk)
END_MESSAGE_MAP()

void demoSetGlobalGuaranteedIpAddressesDlg::OnBnClickedButtonOk()
{
    hwmsdkagent::HwmGuaranteedIpAddressesParam param{};
    param.addressListSize = 3;
    param.addressList = new hwmsdkagent::HwmGuaranteedIpAddress[param.addressListSize]{};
    if (param.addressList == nullptr)
    {
        return;
    }

    // 第一组设置
    // 域名
    CString domainCString;
    m_domainEdit.GetWindowText(domainCString);
    string domainString = CTools::UNICODE2UTF(domainCString.GetString());

    // 保底ip列表
    CString guaranteedIpsCString;
    m_guaranteedIpsEdit.GetWindowText(guaranteedIpsCString);
    string guaranteedIpsString = CTools::UNICODE2UTF(guaranteedIpsCString.GetString());

    errno_t copyRet = strncpy_s(param.addressList[0].domain, domainString.c_str(), sizeof(param.addressList[0].domain)-1);
    copyRet += strncpy_s(param.addressList[0].guaranteedIps, guaranteedIpsString.c_str(), sizeof(param.addressList[0].guaranteedIps)-1);

    // 第二组设置
    // 域名
    CString domain1CString;
    m_domain1Edit.GetWindowText(domain1CString);
    string domain1String = CTools::UNICODE2UTF(domain1CString.GetString());

    // 保底ip列表
    CString guaranteedIps1CString;
    m_guaranteedIps1Edit.GetWindowText(guaranteedIps1CString);
    string guaranteedIps1String = CTools::UNICODE2UTF(guaranteedIps1CString.GetString());

    copyRet += strncpy_s(param.addressList[1].domain, domain1String.c_str(), sizeof(param.addressList[1].domain)-1);
    copyRet += strncpy_s(param.addressList[1].guaranteedIps, guaranteedIps1String.c_str(), sizeof(param.addressList[1].guaranteedIps)-1);

    // 第三组设置
    // 域名
    CString domain2CString;
    m_domain2Edit.GetWindowText(domain2CString);
    string domain2String = CTools::UNICODE2UTF(domain2CString.GetString());

    // 保底ip列表
    CString guaranteedIps2CString;
    m_guaranteedIps2Edit.GetWindowText(guaranteedIps2CString);
    string guaranteedIps2String = CTools::UNICODE2UTF(guaranteedIps2CString.GetString());

    copyRet += strncpy_s(param.addressList[2].domain, domain2String.c_str(), sizeof(param.addressList[2].domain)-1);
    copyRet += strncpy_s(param.addressList[2].guaranteedIps, guaranteedIps2String.c_str(), sizeof(param.addressList[2].guaranteedIps)-1);

    if (copyRet != 0)
    {
        AfxMessageBox(_T("copy error"));
    }

    hwmsdk::HwmErrCode ret = hwmsdkagent::SetGlobalGuaranteedIpAddresses(&param);

    // 释放内存空间
    if (param.addressList != nullptr)
    {
        delete[] param.addressList;
        param.addressList = nullptr;
    }

    if (ret != hwmsdk::HWM_COMMON_SUCCESS)
    {
        CTools::OutputRetStr("SetGlobalGuaranteedIpAddresses error");
        return;
    }
    
    CDialogEx::OnOK();
}

// demoSetGlobalGuaranteedIpAddressesDlg 消息处理程序
