#include <windows.h>
#include <winsock.h>

#include "main.h"
#include "resource.h"

char szAppName[] = "Wawi Tray";
char szWndClass[] = "WawiTrayApp";

char AboutText[] = "Winamp Web Interface\n\nRemote SysTray Client\n\nŠ 2002 Phil Himsworth\n\nWeb:  http://www.flippet.org\nEmail: contact@flippet.net";

HINSTANCE inst = NULL;
HWND hwnd = NULL;
HWND ConfigWnd = NULL;
HWND VolumeWnd = NULL;

UINT timer_id;

bool done = false;

char username[20];
char password[20];
char host[255];

char title[255];

bool use_auth = false;

int port;
int default_action;
int update_delay;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{

	inst = hInstance;

	WNDCLASSEX wc;

	wc.cbSize = sizeof(wc);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = inst;
	wc.hIcon = NULL;
	wc.hCursor = NULL;
	wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
	wc.lpszMenuName = NULL;			//MAKEINTRESOURCE(IDR_MENU);
	wc.lpszClassName = szWndClass;
	wc.hIconSm = NULL;

	if (RegisterClassEx(&wc)==0)
		MessageBox(NULL,"RegisterClassEx error!", szAppName,MB_OK);


	hwnd = CreateWindowEx(	0,
							szWndClass,
							szAppName,
							WS_CAPTION | WS_SYSMENU,
							CW_USEDEFAULT,
							CW_USEDEFAULT,
							200,
							100,
							NULL,
							NULL,
							inst,
							NULL);


	// Shows the window. Used only for message debugging
	//ShowWindow(hwnd,nShowCmd);

	StartWinSock();

	config_read();

	int default_icon = GetIconID();

	if (systray_add(hwnd,1030,LoadIcon(inst, MAKEINTRESOURCE(default_icon)),szAppName) == 0)
		MessageBox(hwnd,"systray_add error",szAppName,MB_OK);

	/*
	UINT SetTimer(
	  HWND hWnd,              // handle of window for timer messages
	  UINT nIDEvent,          // timer identifier
	  UINT uElapse,           // time-out value
	  TIMERPROC lpTimerFunc   // address of timer procedure
	);
	*/

	timer_id = SetTimer(hwnd,TIMER_ID,update_delay*1000,NULL);


	//HANDLE titlethread;
	//DWORD titlethread_id;

	//titlethread = CreateThread(NULL,0,TitleThread,0,0,&titlethread_id);

	MSG msg;
	ZeroMemory(&msg,sizeof(msg));

	while(done == false)
	{
		GetMessage(&msg,hwnd,0,0);
		DispatchMessage(&msg);
	}


	systray_del(hwnd,1030);
	return 0;

}

void SendDefaultAction()
{
	switch(default_action)
	{
	case WA_PREV:
		WASend("/prev");
		break;
	case WA_PLAY:
		WASend("/play");
		break;
	case WA_PAUSE:
		WASend("/pause");
		break;
	case WA_STOP:
		WASend("/stop");
		break;
	case WA_NEXT:
		WASend("/next");
		break;
	}
}

int GetIconID()
{
	switch(default_action)
	{
	case WA_PREV:
		return IDI_PREV;
		break;
	case WA_PLAY:
		return IDI_PLAY;
		break;
	case WA_PAUSE:
		return IDI_PAUSE;
		break;
	case WA_STOP:
		return IDI_STOP;
		break;
	case WA_NEXT:
		return IDI_NEXT;
		break;
	default:
		return 0;
	}
}



LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	UINT mousemsg;
	HMENU menu;
	char path[MAX_PATH];

	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		done = true;
		break;

	case WM_USER+3:
		mousemsg = (UINT)lParam;
		switch(mousemsg)
		{
		case WM_LBUTTONDOWN:
			SendDefaultAction();
			break;

		case WM_RBUTTONDOWN:
			menu = GetSubMenu(LoadMenu(inst,MAKEINTRESOURCE(IDR_MENU)),0);
			POINT pt;
			GetCursorPos(&pt);
			SetForegroundWindow(hwnd);
			SetMenuItemBitmaps(menu,IDM_PREV,MF_BYCOMMAND,LoadBitmap(inst,MAKEINTRESOURCE(IDB_PREV)),NULL);
			SetMenuItemBitmaps(menu,IDM_PLAY,MF_BYCOMMAND,LoadBitmap(inst,MAKEINTRESOURCE(IDB_PLAY)),NULL);
			SetMenuItemBitmaps(menu,IDM_PAUSE,MF_BYCOMMAND,LoadBitmap(inst,MAKEINTRESOURCE(IDB_PAUSE)),NULL);
			SetMenuItemBitmaps(menu,IDM_STOP,MF_BYCOMMAND,LoadBitmap(inst,MAKEINTRESOURCE(IDB_STOP)),NULL);
			SetMenuItemBitmaps(menu,IDM_NEXT,MF_BYCOMMAND,LoadBitmap(inst,MAKEINTRESOURCE(IDB_NEXT)),NULL);

			TrackPopupMenu(menu,0,pt.x,pt.y,0,hwnd,NULL);
			//SetMenuDefaultItem(menu,IDM_NEXT,FALSE);
			PostMessage(hwnd,WM_NULL,0,0);
			break;

		default:
			break;
		}
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDM_QUIT:
			done = true;
			DestroyWindow(hwnd);
			break;
		case IDM_ABOUT:
			MessageBox(hwnd,AboutText,szAppName,MB_OK | MB_ICONINFORMATION);
			break;
		case IDM_CONFIG:
			if (!ConfigWnd)
			{
				NewConfig();
				ConfigWnd = NULL;
			}
			else
				SetActiveWindow(ConfigWnd);
			break;

		case IDM_VOLUME:
			if (!VolumeWnd)
			{
				DialogBox(inst,MAKEINTRESOURCE(IDD_VOLUME),hwnd,VolumeWndProc);
				VolumeWnd = NULL;
			}
			else
				SetActiveWindow(VolumeWnd);
			break;

		case IDM_BROWSE:
			wsprintf(path,"http://%s:%d/winamp?page=browse",host,port);
			//MessageBox(hwnd,path,"Browse Path",MB_OK | MB_TOPMOST);
			ShellExecute(hwnd,"open",path,NULL,NULL,0);
			break;

		case IDM_PLAYLIST:
			wsprintf(path,"http://%s:%d/winamp?page=list",host,port);
			//MessageBox(hwnd,path,"Browse Path",MB_OK | MB_TOPMOST);
			ShellExecute(hwnd,"open",path,NULL,NULL,0);
			break;

		// Winamp Control Items
		case IDM_PREV:
			WASend("/prev");
			break;
		case IDM_PLAY:
			WASend("/play");
			break;
		case IDM_PAUSE:
			WASend("/pause");
			break;
		case IDM_STOP:
			WASend("/stop");
			break;
		case IDM_NEXT:
			WASend("/next");
			break;
			

		}
		break;

	case WM_TIMER:
		if ((UINT)wParam == timer_id)
		{
			MakeRequest(host,port,"/smalltitle",false,false,true);
			if (StrComp(title,""))
				wsprintf(title,"%s",szAppName);
			systray_modify(hwnd,1030,LoadIcon(inst, MAKEINTRESOURCE(GetIconID())),title);
		}
		break;


	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
		break;
	}

	return 0;

}








int systray_modify(HWND hwnd, UINT uID, HICON hIcon, LPSTR lpszTip)
{
	NOTIFYICONDATA tnid;
	tnid.cbSize = sizeof(NOTIFYICONDATA);
	tnid.hWnd = hwnd;
	tnid.uID = uID;
	tnid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
	tnid.uCallbackMessage = WM_USER+3;
	tnid.hIcon = hIcon;
	lstrcpyn(tnid.szTip, lpszTip, sizeof(tnid.szTip)-1);
	return (Shell_NotifyIcon(NIM_MODIFY, &tnid));
}



int systray_add(HWND hwnd, UINT uID, HICON hIcon, LPSTR lpszTip)
{
	NOTIFYICONDATA tnid;
	tnid.cbSize = sizeof(NOTIFYICONDATA);
	tnid.hWnd = hwnd;
	tnid.uID = uID;
	tnid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
	tnid.uCallbackMessage = WM_USER+3;
	tnid.hIcon = hIcon;
	lstrcpyn(tnid.szTip, lpszTip, sizeof(tnid.szTip)-1);
	return (Shell_NotifyIcon(NIM_ADD, &tnid));
}

int systray_del(HWND hwnd, UINT uID)
{
	NOTIFYICONDATA tnid;
	tnid.cbSize = sizeof(NOTIFYICONDATA);
	tnid.hWnd = hwnd;
	tnid.uID = uID;
	return (Shell_NotifyIcon(NIM_DELETE, &tnid));
}
