#include "stdafx.h"
#include "NetworkInfoWindow.h"

NetworkInfoWindow::NetworkInfoWindow()
{

}

NetworkInfoWindow::~NetworkInfoWindow()
{

}

int NetworkInfoWindow::Create(HWND parent)
{
	Load("NetworkInfoWindow");
	Wireless.Load("NetworkInfoWindow::Wireless");
	NetworkAdapter.Load("NetworkInfoWindow::NetworkAdapter");
	LocalMAC.Load("NetworkInfoWindow::LocalMAC");
	HubMAC.Load("NetworkInfoWindow::HubMAC");
	SignalStrength.Load("NetworkInfoWindow::SignalStrength");
	LocationName.Load("NetworkInfoWindow::LocationName");
	IPAddress.Load("NetworkInfoWindow::IPAddress");

	StaticWindow::Create(parent);
	Wireless.Create(hWnd);
	NetworkAdapter.Create(hWnd);
	LocalMAC.Create(hWnd);
	HubMAC.Create(hWnd);
	SignalStrength.Create(hWnd);
	LocationName.Create(hWnd);
	IPAddress.Create(hWnd);

	Wireless.Disable();
	NetworkAdapter.Disable();
	LocalMAC.Disable();
	HubMAC.Disable();
	SignalStrength.Disable();
	IPAddress.Disable();
	LocationName.Disable();

	return 1;
}

void NetworkInfoWindow::OnTimer()
{
	Wireless.SetValue(theApp.Wireless);
	NetworkAdapter.SetText(ConvertToWideString(theApp.NetworkAdapter.data()));

	TCHAR buffer[1024];
	std::wstring childloc,parentloc;
	childloc = theApp.CurrentLocation.LocationName;
	parentloc = theApp.GetDisplayName(theApp.CurrentLocation);
	if(childloc!=parentloc)
		_stprintf(buffer,L"%s (%s)",childloc.data(),parentloc.data());
	else
		_stprintf(buffer,L"%s",childloc.data());
	LocationName.SetText(buffer);
	
	if(theApp.MacAddress.length>0){
		int i;
		_stprintf(buffer,L"%2x",theApp.MacAddress[0]);
		LocalMAC.SetText(buffer);
		for(i=0;i<theApp.MacAddress.length;i++){
			_stprintf(buffer,L":%2x",theApp.MacAddress[i]);
			LocalMAC.AppendText(buffer);
		}
	} else
		LocalMAC.SetText(L"unknown");

	IPAddress.SetText(theApp.IPAddress.data());

	if(theApp.Wireless){

		_stprintf(buffer,L"%2x:%2x:%2x:%2x:%2x:%2x",
			theApp.HubMAC.M1,
			theApp.HubMAC.M2,
			theApp.HubMAC.M3,
			theApp.HubMAC.M4,
			theApp.HubMAC.M5,
			theApp.HubMAC.M6);

		HubMAC.SetText(buffer);
		_stprintf(buffer,L"%d %%\n",theApp.SignalStrength);
		SignalStrength.SetText(buffer);
	} else {
		HubMAC.SetText(L"unknown");
		SignalStrength.SetText(L"?");
	}
}

void NetworkInfoWindow::Show()
{
	SkinnableWindow::Show();
	if(hWnd){
		SetTimer(hWnd,0,500,NULL);
		OnTimer();
	}
}

void NetworkInfoWindow::Hide()
{
	SkinnableWindow::Hide();
	if(hWnd)
		KillTimer(hWnd,0);
}