// zcloudDlg.cpp : implementation file
//

#include "stdafx.h"
#include "zcloudDlg.h"
#include "zlgcanDemo.h"
#include "zlgcan/zlgcan.h"


// zcloudDlg dialog

IMPLEMENT_DYNAMIC(zcloudDlg, CDialog)

zcloudDlg::zcloudDlg(CWnd* pParent /*=NULL*/)
: CDialog(zcloudDlg::IDD, pParent)
{

}

zcloudDlg::~zcloudDlg()
{
}

void zcloudDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_DEVICE, m_deviceList);
    DDX_Control(pDX, IDC_EDIT_HTTP_ADDR, m_httpAddr);
    DDX_Control(pDX, IDC_EDIT_HTTP_PORT, m_httpPort);
    DDX_Control(pDX, IDC_EDIT_MQTT_ADDR, m_mqttAddr);
    DDX_Control(pDX, IDC_EDIT_MQTT_PORT, m_mqttPort);
    DDX_Control(pDX, IDC_EDIT_USERNAME, m_username);
    DDX_Control(pDX, IDC_EDIT_PASSWORD, m_password);
}


BEGIN_MESSAGE_MAP(zcloudDlg, CDialog)
    ON_BN_CLICKED(IDC_BUTTON_CONNECT, &zcloudDlg::OnBnClickedButtonConnect)
    ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, &zcloudDlg::OnBnClickedButtonDisconnect)
    ON_NOTIFY(NM_CLICK, IDC_LIST_DEVICE, &zcloudDlg::OnNMClickListDevice)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_DEVICE, &zcloudDlg::OnLvnItemchangedListDevice)
END_MESSAGE_MAP()

BOOL zcloudDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    m_httpAddr.SetWindowText(TEXT("can.zlgcloud.com"));
    m_httpPort.SetWindowText(TEXT("443"));
    m_mqttAddr.SetWindowText(TEXT("can.zlgcloud.com"));
    m_mqttPort.SetWindowText(TEXT("8143"));
    m_username.SetWindowText(TEXT("15201297129"));
    m_password.SetWindowText(TEXT("cidi1234"));

    LONG lStyle = GetWindowLong(m_deviceList.m_hWnd, GWL_STYLE);
    lStyle &= ~LVS_TYPEMASK;
    lStyle |= LVS_REPORT;
    SetWindowLong(m_deviceList.m_hWnd, GWL_STYLE, lStyle);

    DWORD dwStyle = m_deviceList.GetExtendedStyle();
    dwStyle |= LVS_EX_FULLROWSELECT;
    dwStyle |= LVS_EX_GRIDLINES;;
    m_deviceList.SetExtendedStyle(dwStyle);

    m_deviceList.InsertColumn(0, TEXT("设备ID"), LVCFMT_LEFT, 200);
    m_deviceList.InsertColumn(1, TEXT("状态"), LVCFMT_LEFT, 80);

    GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(FALSE);
    GetDlgItem(IDOK)->EnableWindow(FALSE);

    return 0;
}

// zcloudDlg message handlers

void zcloudDlg::OnBnClickedButtonConnect()
{
    
}

void zcloudDlg::OnBnClickedButtonDisconnect()
{
    // TODO: Add your control notification handler code here
    m_deviceList.DeleteAllItems();
    m_deviceIndices.RemoveAll();

    m_httpAddr.EnableWindow(TRUE);
    m_httpPort.EnableWindow(TRUE);
    m_mqttAddr.EnableWindow(TRUE);
    m_mqttPort.EnableWindow(TRUE);
    m_username.EnableWindow(TRUE);
    m_password.EnableWindow(TRUE);
    m_deviceList.EnableWindow(FALSE);
    GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(TRUE);
    GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(FALSE);
    GetDlgItem(IDOK)->EnableWindow(FALSE);
}

void zcloudDlg::OnNMClickListDevice(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    // TODO: Add your control notification handler code here
    POSITION pos = m_deviceList.GetFirstSelectedItemPosition();
    GetDlgItem(IDOK)->EnableWindow(pos != NULL);
    if (pos != NULL)
    {
        int index = m_deviceList.GetNextSelectedItem(pos);
        m_deviceIndex = m_deviceIndices.GetAt(index);
    }
    *pResult = 0;
}

unsigned zcloudDlg::GetDeviceIndex() const
{
    return m_deviceIndex;
}


void zcloudDlg::OnLvnItemchangedListDevice(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}
