// DlgDdns.cpp : implementation file
//

#include "stdafx.h"
#include "MsSDKTest.h"
#include "DlgDdns.h"
#include "MsSDKTestDlg.h"
#include "MsIpcSDK.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgDdns dialog


CDlgDdns::CDlgDdns(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgDdns::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgDdns)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	memset(m_sTempMac, 0, sizeof(m_sTempMac));
	memset(m_sTempHostName, 0, sizeof(m_sTempHostName));

}


void CDlgDdns::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgDdns)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgDdns, CDialog)
	//{{AFX_MSG_MAP(CDlgDdns)
	ON_CBN_SELCHANGE(IDC_COMBO_DDNS_SERVER_NAME, OnSelchangeServerProvider)
	ON_BN_CLICKED(IDC_CHK_DDNS, OnChkDdnsEnable)
	ON_BN_CLICKED(IDC_CHK_EASY_DDNS, OnChkEasyDdns)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgDdns message handlers

BOOL CDlgDdns::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	OnBtnGetDdsn();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgDdns::OnOK() 
{
	// TODO: Add extra validation here
	if(!OnBtnSetDdns()){
		return;
	}
	CDialog::OnOK();
}

BOOL CDlgDdns::OnBtnGetDdsn() 
{
	Ms_Ipc_DDNS ddnsInfo = {0};
	LONG nResult = Ms_Ipc_GetDDNS(m_nUserId, &ddnsInfo);
	if(nResult)
	{
		CMsSDKTestDlg::OutputTips("Init DNS info failed. Please try again...");
		return FALSE;
	}
	UpdateData(TRUE);
	((CEdit*)GetDlgItem(IDC_EDIT_DDNS_USER_NAME))->LimitText(31);
	((CEdit*)GetDlgItem(IDC_EDIT_DDNS_PASSWORD))->LimitText(31);
	((CEdit*)GetDlgItem(IDC_EDIT_DDNS_HOST_NAME))->LimitText(16);

	((CButton*)GetDlgItem(IDC_CHK_DDNS))->SetCheck(ddnsInfo.bEnable);
	((CButton*)GetDlgItem(IDC_CHK_EASY_DDNS))->SetCheck(ddnsInfo.nEasyMode);
	//Init Server Provider
	CComboBox *pServerCb = (CComboBox*)GetDlgItem(IDC_COMBO_DDNS_SERVER_NAME);
	pServerCb->InsertString(0, "dyndns.org");
	pServerCb->InsertString(1, "freedns.afraid.org");
	pServerCb->InsertString(2, "www.no-ip.com");
	pServerCb->InsertString(3, "www.zoneedit.com");
	pServerCb->InsertString(4, "PlanetDDNS.com");
	memcpy(m_sTempMac, ddnsInfo.sDeviceMacAddr, min(sizeof(m_sTempMac), sizeof(ddnsInfo.sDeviceMacAddr)));
	
	Ms_Ipc_SystemInfo systemInfo = {0};
	int nVer1=0, nVer2=0, nVer3=0, nVer4=0;
	if(!Ms_Ipc_GetSystemInfo(m_nUserId, &systemInfo))
		sscanf(systemInfo.sSoftwareVersion, "%d.%d.%d.%d", &nVer1, &nVer2, &nVer3, &nVer4);
	if(!(ddnsInfo.sDeviceMacAddr[0] == '0' && ddnsInfo.sDeviceMacAddr[1] == '0' && ddnsInfo.sDeviceMacAddr[3] == '3'\
		&& ddnsInfo.sDeviceMacAddr[4] == '0' && ddnsInfo.sDeviceMacAddr[6] == '4' && (ddnsInfo.sDeviceMacAddr[7] == 'f' || ddnsInfo.sDeviceMacAddr[7] == 'F')) \
		|| (nVer1 != 0 && (nVer2 < 4 || (nVer2 == 4 && nVer4 < 73)))) //00:30:4f:xx:xx:xx is planet oem device, and planet server is after version4.x.73
	{
		pServerCb->DeleteString(4);
		((CButton*)GetDlgItem(IDC_CHK_EASY_DDNS))->SetCheck(FALSE);
	}
	//init end
	
	// Modified begin for old version compatibility
// 	pServerCb->SetCurSel(0);
// 	if(ddnsInfo.sDdnsServer[0] != '\0')
// 		pServerCb->SelectString(-1, ddnsInfo.sDdnsServer);
	if(strstr("freedns.afraid.org", ddnsInfo.sDdnsServer)){
		pServerCb->SetCurSel(1);
	}else if(strstr("www.no-ip.com", ddnsInfo.sDdnsServer) || ("no-ip.com", ddnsInfo.sDdnsServer)){ // the latter is for old web version
		pServerCb->SetCurSel(2);
	}else if(strstr("www.zoneedit.com", ddnsInfo.sDdnsServer) || strstr("zoneedit.com", ddnsInfo.sDdnsServer)){ // the latter is for old web version
		pServerCb->SetCurSel(3);
	}else if((strstr("PlanetDDNS.com", ddnsInfo.sDdnsServer) || strstr("planet", ddnsInfo.sDdnsServer)) && pServerCb->GetCount() == 5){ // the latter is for old web version
		pServerCb->SetCurSel(4);
	}else/* if(_tcsstr(_T("dyndns.org"), tsServer))*/{
		pServerCb->SetCurSel(0);
	}
	// end
	GetDlgItem(IDC_EDIT_DDNS_USER_NAME)->SetWindowText(ddnsInfo.sDdnsUserName);
	GetDlgItem(IDC_EDIT_DDNS_PASSWORD)->SetWindowText(ddnsInfo.sDdnsPassword);
	GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->SetWindowText(ddnsInfo.sDdnsHostName);
	GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->GetWindowText(m_sTempHostName, sizeof(m_sTempHostName));
	GetDlgItem(IDC_EDIT_DDNS_HASH)->SetWindowText(ddnsInfo.sDdnsHash);
	OnSelchangeServerProvider();
	OnChkEasyDdns();
	OnChkDdnsEnable();
	UpdateData(FALSE);
	return TRUE;
}

BOOL CDlgDdns::OnBtnSetDdns() 
{
	char sValue[32] = {0};
	Ms_Ipc_DDNS ddnsInfo = {0};
	UpdateData(TRUE);
	ddnsInfo.bEnable = (BYTE)((CButton*)GetDlgItem(IDC_CHK_DDNS))->GetCheck();
	if(((CComboBox*)GetDlgItem(IDC_COMBO_DDNS_SERVER_NAME))->GetCurSel() == 4)
	{
		ddnsInfo.bPlnt = ddnsInfo.bEnable;
		ddnsInfo.nEasyMode = (BYTE)((CButton*)GetDlgItem(IDC_CHK_EASY_DDNS))->GetCheck();
	}
	// Modified begin for old version compatibility
//	GetDlgItem(IDC_COMBO_DDNS_SERVER_NAME)->GetWindowText(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer));
	Ms_Ipc_SystemInfo systemInfo = {0};
	int nVer1=0, nVer2=0, nVer3=0, nVer4=0;
	if(!Ms_Ipc_GetSystemInfo(m_nUserId, &systemInfo))
		sscanf(systemInfo.sSoftwareVersion, "%d.%d.%d.%d", &nVer1, &nVer2, &nVer3, &nVer4);
	int nCurSelIndex = ((CComboBox*)GetDlgItem(IDC_COMBO_DDNS_SERVER_NAME))->GetCurSel();
	if(nVer1 != 0 && (nVer2 < 4 || (nVer2 == 4 && nVer4 < 83))) // 4.x.79(included) 以前的按老版本处理, next published version after 79 maybe 85
	{
		if(nCurSelIndex == 1){
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "freedns.afraid.org");
		}else if(nCurSelIndex == 2){
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "no-ip.com");
		}else if(nCurSelIndex == 3){
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "zoneedit.com");
		}else if(nCurSelIndex == 4){
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "planet");
		}else/* if(nCurSelIndex == 0)*/{
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "dyndns.org");
		}
	}else{
		if(nCurSelIndex == 1){
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "freedns.afraid.org");
		}else if(nCurSelIndex == 2){
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "www.no-ip.com");
		}else if(nCurSelIndex == 3){
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "www.zoneedit.com");
		}else if(nCurSelIndex == 4){
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "PlanetDDNS.com");
		}else/* if(nCurSelIndex == 0)*/{
			_snprintf(ddnsInfo.sDdnsServer, sizeof(ddnsInfo.sDdnsServer)-1, "dyndns.org");
		}
	}
	// end
	GetDlgItem(IDC_EDIT_DDNS_USER_NAME)->GetWindowText(ddnsInfo.sDdnsUserName, sizeof(ddnsInfo.sDdnsUserName));
	GetDlgItem(IDC_EDIT_DDNS_PASSWORD)->GetWindowText(ddnsInfo.sDdnsPassword, sizeof(ddnsInfo.sDdnsPassword));
	GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->GetWindowText(ddnsInfo.sDdnsHostName, sizeof(ddnsInfo.sDdnsHostName));
	GetDlgItem(IDC_EDIT_DDNS_HASH)->GetWindowText(ddnsInfo.sDdnsHash, sizeof(ddnsInfo.sDdnsHash));
	if(ddnsInfo.bEnable || (ddnsInfo.bPlnt && !ddnsInfo.nEasyMode))
	{
		if(ddnsInfo.sDdnsUserName[0] == '\0' || ddnsInfo.sDdnsPassword[0] == '\0' || ddnsInfo.sDdnsHostName[0] == '\0')
		{
			AfxMessageBox("UserName, Password or Host Name is invalid.");
			return FALSE;
		}
	}
	LONG nResult = Ms_Ipc_SetDDNS(m_nUserId, &ddnsInfo);	
	if(nResult){
		AfxMessageBox("Failed.");
		return FALSE;
	}else{
		CMsSDKTestDlg::OutputTips("Set DDNS Successfully.");
		return TRUE;
	}
//	return nResult;
}

void CDlgDdns::OnSelchangeServerProvider() 
{
	// TODO: Add your control notification handler code here
	CComboBox *pServerCb = (CComboBox*)GetDlgItem(IDC_COMBO_DDNS_SERVER_NAME);
	int nIndex = pServerCb->GetCurSel();
	if(nIndex == 1)
	{
		GetDlgItem(IDC_STATIC_HASH)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_EDIT_DDNS_HASH)->ShowWindow(SW_SHOW);
	}else{
		GetDlgItem(IDC_STATIC_HASH)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_EDIT_DDNS_HASH)->ShowWindow(SW_HIDE);
	}
	if(nIndex != 4)
	{
		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->GetWindowText(m_sTempHostName, sizeof(m_sTempHostName));
		GetDlgItem(IDC_CHK_EASY_DDNS)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATIC_HOSENAME)->SetWindowText("Host Name:");
		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->SetWindowText(m_sTempHostName);
 		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->EnableWindow(TRUE);
	}
	else
	{
		if(((CButton*)GetDlgItem(IDC_CHK_EASY_DDNS))->GetCheck())
		{
			GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->GetWindowText(m_sTempHostName, sizeof(m_sTempHostName));
			char sPlanetEasyName[64] = {0};
			_snprintf(sPlanetEasyName, sizeof(sPlanetEasyName)-1, "pl%c%c%c%c%c%c.planetddns.com",\
				m_sTempMac[9], m_sTempMac[10], m_sTempMac[12], m_sTempMac[13], m_sTempMac[15], m_sTempMac[16]);

			GetDlgItem(IDC_STATIC_HOSENAME)->SetWindowText("Easy Domain Name:");
			GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->SetWindowText(sPlanetEasyName);
			GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->EnableWindow(FALSE);
		}
		GetDlgItem(IDC_CHK_EASY_DDNS)->ShowWindow(SW_SHOW);
	}
}

void CDlgDdns::OnChkDdnsEnable() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_DDNS))->GetCheck())
	{
		GetDlgItem(IDC_COMBO_DDNS_SERVER_NAME)->EnableWindow(TRUE);
		GetDlgItem(IDC_EDIT_DDNS_USER_NAME)->EnableWindow(TRUE);
		GetDlgItem(IDC_EDIT_DDNS_PASSWORD)->EnableWindow(TRUE);
		GetDlgItem(IDC_EDIT_DDNS_HASH)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_EASY_DDNS)->EnableWindow(TRUE);
		if(((CComboBox*)GetDlgItem(IDC_COMBO_DDNS_SERVER_NAME))->GetCurSel() == 4 && ((CButton*)GetDlgItem(IDC_CHK_EASY_DDNS))->GetCheck())
			GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->EnableWindow(FALSE);
		else
			GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->EnableWindow(TRUE);
	}
	else
	{
		GetDlgItem(IDC_COMBO_DDNS_SERVER_NAME)->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_DDNS_USER_NAME)->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_DDNS_PASSWORD)->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_DDNS_HASH)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHK_EASY_DDNS)->EnableWindow(FALSE);
	}
}

void CDlgDdns::OnChkEasyDdns() 
{
	// TODO: Add your control notification handler code here
	if(((CButton*)GetDlgItem(IDC_CHK_EASY_DDNS))->GetCheck())
	{
		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->GetWindowText(m_sTempHostName, sizeof(m_sTempHostName));
		char sPlanetEasyName[64] = {0};
		_snprintf(sPlanetEasyName, sizeof(sPlanetEasyName)-1, "pl%c%c%c%c%c%c.planetddns.com",\
			m_sTempMac[9], m_sTempMac[10], m_sTempMac[12], m_sTempMac[13], m_sTempMac[15], m_sTempMac[16]);

		GetDlgItem(IDC_STATIC_HOSENAME)->SetWindowText("Easy Domain Name:");
		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->SetWindowText(sPlanetEasyName);
		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->EnableWindow(FALSE);
	}
	else{
		GetDlgItem(IDC_STATIC_HOSENAME)->SetWindowText("Host Name:");
		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->SetWindowText(m_sTempHostName);
		GetDlgItem(IDC_EDIT_DDNS_HOST_NAME)->EnableWindow(TRUE);
	}
}
