// TouchSetupDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TouchBoard.h"
#include "TouchSetupDlg.h"
#include "TouchBoardDlg.h"
#include "afxdialogex.h"


// CTouchSetupDlg dialog

extern CTouchBoardDlg *g_pMainDlg;

IMPLEMENT_DYNAMIC(CTouchSetupDlg, CDialogEx)

CTouchSetupDlg::CTouchSetupDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CTouchSetupDlg::IDD, pParent)
{
	m_bSendLineIn = FALSE;
	ZeroMemory(m_ctAddr, sizeof(m_ctAddr));
	ZeroMemory(m_esAddr, sizeof(m_esAddr));
	ZeroMemory(m_psAddr, sizeof(m_psAddr));
}

CTouchSetupDlg::~CTouchSetupDlg()
{
}

void CTouchSetupDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CTouchSetupDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_SELECT1, &CTouchSetupDlg::OnBnClickedButtonSelect1)
	ON_BN_CLICKED(IDC_BUTTON_SELECT2, &CTouchSetupDlg::OnBnClickedButtonSelect2)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CTouchSetupDlg::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_MICVOL_MINUS, &CTouchSetupDlg::OnBnClickedButtonMicvolMinus)
	ON_BN_CLICKED(IDC_BUTTON_MICVOL_PLUS, &CTouchSetupDlg::OnBnClickedButtonMicvolPlus)
	ON_BN_CLICKED(IDC_BUTTON_LINEVOL_MINUS, &CTouchSetupDlg::OnBnClickedButtonLinevolMinus)
	ON_BN_CLICKED(IDC_BUTTON_LINEVOL_PLUS, &CTouchSetupDlg::OnBnClickedButtonLinevolPlus)
	ON_BN_CLICKED(IDC_CHECK_LINESEND, &CTouchSetupDlg::OnBnClickedCheckLinesend)
	ON_BN_CLICKED(IDCANCEL, &CTouchSetupDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


// CTouchSetupDlg message handlers


void CTouchSetupDlg::OnBnClickedButtonSelect1()
{
	// TODO: Add your control notification handler code here
	TCHAR szFilter[] = L"실행파일(*.exe)|*.exe|All Files(*.*)|*.*||";
	CString strPathName;
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);

	if(IDOK == dlg.DoModal()) {
		strPathName = dlg.GetPathName();
		GetDlgItem(IDC_EDIT_WIRELESS_EXEC)->SetWindowTextW(strPathName);
	}
	TRACE("pszPathname : %S", strPathName);
}


void CTouchSetupDlg::OnBnClickedButtonSelect2()
{
	// TODO: Add your control notification handler code here
	TCHAR szFilter[] = L"실행파일(*.exe)|*.exe|All Files(*.*)|*.*||";
	CString strPathName;
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);

	if(IDOK == dlg.DoModal()) {
		strPathName = dlg.GetPathName();
		GetDlgItem(IDC_EDIT_CALL_EXEC)->SetWindowTextW(strPathName);
	}
}


void CTouchSetupDlg::LoadSettings()
{
	TCHAR strPath[512] = {0, };
	CString strFilePath;
	HRESULT hr = SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, strPath);
	FILE *fp = NULL;

	strFilePath = strPath;
	strFilePath += L"\\touch_setting.txt";
	m_bSendTTS = FALSE;

	_wfopen_s(&fp, strFilePath, L"rt");
	if(fp)
	{
		char strLine[128 * 1024];
		char strData[128 * 1024];
		char *pResult;
		int isChecked;

		while(!feof(fp))
		{
			ZeroMemory(strLine, sizeof(strLine));
			pResult = fgets(strLine, sizeof(strLine), fp);
			if(!pResult)
				break;

			ZeroMemory(strData, sizeof(strData));
			if (strncmp(strLine, "CT_ADDRESS=", strlen("CT_ADDRESS=")) == 0) {
				INT32 value[4];
				sscanf_s(strLine, "CT_ADDRESS=%d.%d.%d.%d", &value[0], &value[1], &value[2], &value[3]);
				for (int i = 0; i < 4; i++)
					m_ctAddr[i] = value[i];
				//swscanf_s(strLine, L"CT_ADDRESS=%d.%d.%d.%d", &m_ctAddr[0], &m_ctAddr[1], &m_ctAddr[2], &m_ctAddr[3]);
			} else if (strncmp(strLine, "ES_ADDRESS=", strlen("ES_ADDRESS=")) == 0) {
					INT32 value[4];
					sscanf_s(strLine, "ES_ADDRESS=%d.%d.%d.%d", &value[0], &value[1], &value[2], &value[3]);
					for (int i = 0; i < 4; i++)
						m_esAddr[i] = value[i];
			} else if (strncmp(strLine, "PS_ADDRESS=", strlen("PS_ADDRESS=")) == 0) {
				INT32 value[4];
				sscanf_s(strLine, "PS_ADDRESS=%d.%d.%d.%d", &value[0], &value[1], &value[2], &value[3]);
				for (int i = 0; i < 4; i++)
					m_psAddr[i] = value[i];
			} else if(strncmp(strLine, "WIRELESS_PATH=", strlen("WIRELESS_PATH=")) == 0) {
				sscanf_s(strLine, "WIRELESS_PATH=%[^\r\n]", strData, _countof(strData));
				m_strWirelessPath = strData;
			} else if(strncmp(strLine, "CALL_PATH=", strlen("CALL_PATH=")) == 0) {
				sscanf_s(strLine, "CALL_PATH=%[^\r\n]", strData, _countof(strData));
				m_strCallPath = strData;
			} else if(strncmp(strLine, "SEND_TTS_MSG=", strlen("SEND_TTS_MSG=")) == 0) {
				sscanf_s(strLine, "SEND_TTS_MSG=%[^\r\n]", strData, _countof(strData));
				if(atoi(strData) == 1) {
					//m_bSendTTS = TRUE;
				}
			} else if(strncmp(strLine, "TTS_MSG=", strlen("TTS_MSG=")) == 0) {
				UTF8_CONVERSION_EX;
				sscanf_s(strLine, "TTS_MSG=%[^\r\n]", strData, _countof(strData));
				m_strTTSMessage = UTF82W_EX((char *)strData);
				m_strTTSMessage.Replace(L"|", L"\r\n");
			}
		}
		fclose(fp);

		TRACE(L"LOAD SETTINGS : %s\n", strFilePath);
	}
	else
	{
		for(INT32 i = 0;i < 4;i++)
			m_ctAddr[i] = 0;

		m_strWirelessPath = "";
		m_strCallPath = "";
	}
}

void CTouchSetupDlg::SaveSettings()
{
	TCHAR strPath[512] = {0, };
	CString strFilePath;
	CString strInfo;
	UINT isChecked;
	HRESULT hr = SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, strPath);
	FILE *fp = NULL;
	BYTE addr[4];
	BOOL isIpChanged = false;

	strFilePath = strPath;
	strFilePath += L"\\touch_setting.txt";

	CIPAddressCtrl *pCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS_CT);
	pCtrl->GetAddress(addr[0], addr[1], addr[2], addr[3]);
	for(INT32 i = 0;i < 4;i++) {
		if(addr[i] != m_ctAddr[i])
			isIpChanged = true;
		
		m_ctAddr[i] = addr[i];
	}

	pCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS_ES);
	pCtrl->GetAddress(addr[0], addr[1], addr[2], addr[3]);
	for (INT32 i = 0; i < 4; i++) {
		if (addr[i] != m_esAddr[i])
			isIpChanged = true;

		m_esAddr[i] = addr[i];
	}

	pCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS_PS);
	pCtrl->GetAddress(addr[0], addr[1], addr[2], addr[3]);
	for (INT32 i = 0; i < 4; i++) {
		if (addr[i] != m_psAddr[i])
			isIpChanged = true;

		m_psAddr[i] = addr[i];
	}

	//pCtrl->GetAddress(m_ctAddr[0], m_ctAddr[1], m_ctAddr[2], m_ctAddr[3]);
	CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT_CALL_EXEC);
	pEdit->GetWindowTextW(m_strCallPath);
	pEdit = (CEdit *)GetDlgItem(IDC_EDIT_WIRELESS_EXEC);
	pEdit->GetWindowTextW(m_strWirelessPath);

	pEdit = (CEdit *)GetDlgItem(IDC_EDIT_TTS);
	pEdit->GetWindowTextW(m_strTTSMessage);

	CButton *pBox = (CButton *)GetDlgItem(IDC_CHECK_TTS);
	if(pBox->GetCheck()) {
		m_bSendTTS = TRUE;
	} else {
		m_bSendTTS = FALSE;
	}

	if(m_bSendTTS) {
		if(m_strTTSMessage.GetLength() > 0) {
			char strTTSMessage[128 * 1024];
			UTF8_CONVERSION_EX;
			CString strMsg = m_strTTSMessage;
			strMsg.Replace(L"\r\n", L".");
			sprintf(strTTSMessage, "BYPASS\r\nREQUEST_TTS_MESSAGE=0|%s\r\n", W2UTF8_EX(strMsg));
			g_pMainDlg->SocketSendExceptionHandle(strTTSMessage, strlen(strTTSMessage));
		}
	}

	_wfopen_s(&fp, strFilePath, L"wt");
	if(fp)
	{
		fprintf(fp, "CT_ADDRESS=%d.%d.%d.%d\r\n", m_ctAddr[0], m_ctAddr[1], m_ctAddr[2], m_ctAddr[3]);
		fprintf(fp, "ES_ADDRESS=%d.%d.%d.%d\r\n", m_esAddr[0], m_esAddr[1], m_esAddr[2], m_esAddr[3]);
		fprintf(fp, "PS_ADDRESS=%d.%d.%d.%d\r\n", m_psAddr[0], m_psAddr[1], m_psAddr[2], m_psAddr[3]);
		fprintf(fp, "WIRELESS_PATH=%S\r\n", m_strWirelessPath);
		fprintf(fp, "CALL_PATH=%S\r\n", m_strCallPath);
		
		if(m_bSendTTS)
			fprintf(fp, "SEND_TTS_MSG=1\r\n");
		if(m_strTTSMessage.GetLength() > 0) {
			UTF8_CONVERSION_EX;
			CString strMsg = m_strTTSMessage;
			strMsg.Replace(L"\r\n", L"|");
			fprintf(fp, "TTS_MSG=%s\r\n", W2UTF8_EX(strMsg));
		}
		fclose(fp);

		TRACE(L"SAVE SETTINGS : %s\n", strFilePath);
		MessageBox(L"설정이 저장되었습니다");
		ShowWindow(SW_HIDE);
	}

	AfxGetMainWnd()->PostMessage(WM_TBD_SETUP_SAVE, isIpChanged);
}


void CTouchSetupDlg::UpdateVariableToCtrl()
{
	CIPAddressCtrl *pCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS_CT);
	pCtrl->SetAddress(m_ctAddr[0], m_ctAddr[1], m_ctAddr[2], m_ctAddr[3]);
	CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT_CALL_EXEC);
	pEdit->SetWindowTextW(m_strCallPath);
	pEdit = (CEdit *)GetDlgItem(IDC_EDIT_WIRELESS_EXEC);
	pEdit->SetWindowTextW(m_strWirelessPath);
	pEdit = (CEdit *)GetDlgItem(IDC_EDIT_TTS);
	pEdit->SetWindowTextW(m_strTTSMessage);

	pCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS_ES);
	pCtrl->SetAddress(m_esAddr[0], m_esAddr[1], m_esAddr[2], m_esAddr[3]);
	pCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS_PS);
	pCtrl->SetAddress(m_psAddr[0], m_psAddr[1], m_psAddr[2], m_psAddr[3]);


	CButton *pBox = (CButton *)GetDlgItem(IDC_CHECK_TTS);
	if(m_bSendTTS)
		pBox->SetCheck(TRUE);
}

void CTouchSetupDlg::OnBnClickedButtonSave()
{
	// TODO: Add your control notification handler code here

	SaveSettings();
}




void CTouchSetupDlg::OnBnClickedButtonMicvolMinus()
{
	// TODO: Add your control notification handler code here
	char strMessage[256];
	sprintf(strMessage, "MIC_VOLUME=MINUS\r\n\r\n");
	g_pMainDlg->SocketSendExceptionHandle(strMessage, strlen(strMessage));

}


void CTouchSetupDlg::OnBnClickedButtonMicvolPlus()
{
	// TODO: Add your control notification handler code here
	char strMessage[256];
	sprintf(strMessage, "MIC_VOLUME=PLUS\r\n\r\n");
	g_pMainDlg->SocketSendExceptionHandle(strMessage, strlen(strMessage));
}


void CTouchSetupDlg::OnBnClickedButtonLinevolMinus()
{
	// TODO: Add your control notification handler code here
	char strMessage[256];
	sprintf(strMessage, "LINEIN_VOLUME=MINUS\r\n\r\n");
	g_pMainDlg->SocketSendExceptionHandle(strMessage, strlen(strMessage));
}


void CTouchSetupDlg::OnBnClickedButtonLinevolPlus()
{
	// TODO: Add your control notification handler code here
	char strMessage[256];
	sprintf(strMessage, "LINEIN_VOLUME=PLUS\r\n\r\n");
	g_pMainDlg->SocketSendExceptionHandle(strMessage, strlen(strMessage));
}


BOOL CTouchSetupDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class

	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
    {     
        if (GetDlgItem(IDC_EDIT_TTS) == GetFocus())
        {
			CEdit* edit = (CEdit*)GetDlgItem(IDC_EDIT_TTS);
			int nLen = edit->GetWindowTextLength();
			edit->SetSel( nLen, nLen );
			edit->ReplaceSel(_T("\r\n"));
        }
    }

	return CDialogEx::PreTranslateMessage(pMsg);
}


void CTouchSetupDlg::OnBnClickedCheckLinesend()
{
	// TODO: Add your control notification handler code here

	CButton *pBox = (CButton *)GetDlgItem(IDC_CHECK_LINESEND);
	CWnd *pWnd = (CWnd *)g_pMainDlg->GetDlgItem(IDC_STATIC_LINEIN);
	if(pBox->GetCheck()) {
		m_bSendLineIn = TRUE;
		pWnd->ShowWindow(SW_SHOW);
	} else {
		m_bSendLineIn = FALSE;
		pWnd->ShowWindow(SW_HIDE);
	}
}


void CTouchSetupDlg::OnBnClickedCancel()
{
	// TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
	CDialogEx::OnCancel();
}
