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

#include "stdafx.h"
#include "SATHelper.h"
#include "DlgService.h"
#include "afxdialogex.h"
#include "SATClient.h"


// CDlgService 对话框

IMPLEMENT_DYNAMIC(CDlgService, CDialogEx)

CDlgService::CDlgService(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DLG_SERVICE, pParent)
{

}

CDlgService::~CDlgService()
{
}

void CDlgService::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_DEVICES, m_listDevices);
	DDX_Control(pDX, IDC_LIST_TASK, m_listTask);
	DDX_Control(pDX, BTN_MODIFY_RUNNER_NAME, m_btnModify);
	DDX_Control(pDX, BTN_ADD_ANDROID, m_btnAddAndroid);
}


BEGIN_MESSAGE_MAP(CDlgService, CDialogEx)
	ON_WM_PAINT()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(BTN_MODIFY_RUNNER_NAME, &CDlgService::OnBnClickedModifyRunnerName)
	ON_BN_CLICKED(BTN_ADD_ANDROID, &CDlgService::OnBnClickedAddAndroid)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CDlgService 消息处理程序


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

	// TODO:  在此添加额外的初始化
	// 做为子窗口，这里设置透明度没有任何效果;
	// SetLayeredWindowAttributes(RGB(1, 2, 3), 0, LWA_COLORKEY/*|LWA_ALPHA*/);
	InitCtrl();
	FillData2Ctrl();
	// 5秒刷新一次;
	SetTimer(0, 15000, NULL);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CDlgService::OnPaint()
{
	CPaintDC dc(this); // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CDialogEx::OnPaint()
	CRect rc;
	GetClientRect(rc);
	CBrush   brush;
	brush.CreateSolidBrush(0xFFEFD5);
	dc.FillRect(&rc, &brush);
}


HBRUSH CDlgService::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何特性
	if (nCtlColor == CTLCOLOR_STATIC)
	{
		pDC->SetBkMode(TRANSPARENT);//设置背景透明
		//pDC->SetTextColor(RGB(255, 255, 0));//设置字体为黄色
		return (HBRUSH)::GetStockObject(NULL_BRUSH);
	}
	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}

void CDlgService::InitCtrl()
{
	// 初始化设备列表;
	m_listDevices.InsertColumn(0, "No");
	m_listDevices.SetColumnWidth(0, 25);
	m_listDevices.InsertColumn(1, "类型");
	m_listDevices.SetColumnWidth(1, 45);
	m_listDevices.InsertColumn(2, "名称");
	m_listDevices.SetColumnWidth(2, 130);
	m_listDevices.InsertColumn(3, "状态");
	m_listDevices.SetColumnWidth(3, 55);
	m_listDevices.SetExtendedStyle(m_listDevices.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);

	// 初始化任务列表;
	m_listTask.InsertColumn(0, "No");
	m_listTask.SetColumnWidth(0, 25);
	m_listTask.InsertColumn(1, "任务ID");
	m_listTask.SetColumnWidth(1, 130);
	m_listTask.InsertColumn(2, "状态");
	m_listTask.SetColumnWidth(2, 55);
	m_listTask.InsertColumn(3, "结果");
	m_listTask.SetColumnWidth(3, 55);
	m_listTask.SetExtendedStyle(m_listTask.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);

	// 修改按钮默认样式;
	m_btnModify.m_bTransparent = FALSE;
	m_btnModify.m_bDontUseWinXPTheme = TRUE;
	// 设置背景色;
	m_btnModify.SetFaceColor(RGB(0x87,0xCE,0xFA), TRUE);
	// 消除单击时黑色内圈;
	m_btnModify.m_bDrawFocus = FALSE;
	// 设置按钮扁平样式;
	m_btnModify.m_nFlatStyle = CMFCButton::FlatStyle::BUTTONSTYLE_FLAT;
	// 设置鼠标形状;
	m_btnModify.SetMouseCursorHand();

	m_btnAddAndroid.m_bTransparent = FALSE;
	m_btnAddAndroid.m_bDontUseWinXPTheme = TRUE;
	// 设置背景色;
	m_btnAddAndroid.SetFaceColor(RGB(0x87, 0xCE, 0xFA), TRUE);
	// 消除单击时黑色内圈;
	m_btnAddAndroid.m_bDrawFocus = FALSE;
	// 设置按钮扁平样式;
	m_btnAddAndroid.m_nFlatStyle = CMFCButton::FlatStyle::BUTTONSTYLE_FLAT;
	// 设置鼠标形状;
	m_btnAddAndroid.SetMouseCursorHand();
}

void CDlgService::FillData2Ctrl()
{
	SetDlgItemText(IDC_EDIT_RUNNER_NAME, Global::g_Config.strActuator.c_str());
}


void CDlgService::OnBnClickedModifyRunnerName()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strActuator;
	GetDlgItemText(IDC_EDIT_RUNNER_NAME, strActuator);

	Global::g_Config.strActuator = strActuator.GetString();
	WritePrivateProfileString(_T("SATService"), _T("Actuator"), strActuator, Global::g_szConfig);
	CSATClient::GetInstance()->TCPLogin(Global::g_Config.strSATUserName, Global::g_Config.strSATPassword, Global::g_Config.strActuator);
}


void CDlgService::OnBnClickedAddAndroid()
{
	// TODO: 在此添加控件通知处理程序代码
	CIPAddressCtrl* pIP = (CIPAddressCtrl*)GetDlgItem(IDC_IP_ANDROID);
	// 获取IP地址;
	BYTE nf1, nf2, nf3, nf4;
	pIP->GetAddress(nf1, nf2, nf3, nf4);
	CString str;
	str.Format("%d.%d.%d.%d", nf1, nf2, nf3, nf4);//这里的nf得到的值是IP值了
	if (str.IsEmpty())
		return;

	CSATClient::GetInstance()->TCPAddDevice(str.GetString());
}


void CDlgService::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (nIDEvent == 0)
	{
		CSATClient::GetInstance()->TCPQueryDevices(FALSE);
		Sleep(500);

		int nIndex = 0;
		m_listDevices.DeleteAllItems();
		for ( std::vector<SATPROTO::Device>::iterator it = SATData::devices.begin(); it != SATData::devices.end(); it++ )
		{
			m_listDevices.InsertItem(nIndex, "1");
			if ( it->nType == 0 )
				m_listDevices.SetItemText(nIndex, 1, "虚拟");
			else if ( it->nType == 1 )
				m_listDevices.SetItemText(nIndex, 1, "USB");
			else if ( it->nType == 2 )
				m_listDevices.SetItemText(nIndex, 1, "网络");
			m_listDevices.SetItemText(nIndex, 2, it->szName);

			if ( it->nStatus == 0 )
				m_listDevices.SetItemText(nIndex, 3, "空闲");
			else
				m_listDevices.SetItemText(nIndex, 3, "繁忙");
		}
	}

	CDialogEx::OnTimer(nIDEvent);
}
