/* --- HTML.CPP ---
/  
/ Written by Phil Himsworth, 2001-2002.
/
/*/

#include <windows.h>

#include "types.h"
#include "main.h"
#include "resource.h"
#include "wamessage.h"
#include "html.h"

extern int frames;


// --------------------------------------------------------------------------------
// *** HTTP / HTML FUNCTIONS ***

// Sends HTTP header
void OpenHTTPHeader(connection * conn, char * type, int length, char * filename)
{
	char openstring[200];
	prints(conn,"HTTP/1.0 200 OK\nConnection: close\n");

	if (StrComp(type,"text/html"))
		prints(conn,"Expires: -1\n");

	if (type != NULL)
	{
		wsprintf(openstring,"Content-type: %s\n",type);
		prints(conn,openstring);
	}
	if (length != 0)
	{
		wsprintf(openstring,"Content-length: %d\n",length);
		prints(conn,openstring);
	}

	if (filename != NULL)
	{
		wsprintf(openstring,"Content-Disposition: attachment; filename=\"%s\"\n",filename);
		prints(conn,openstring);
	}

	prints(conn,"\n");

}

// Sends HTML header
void OpenHtmlHeader(connection * conn)
{
	prints(conn,"<html>\n<head>\n<title>\nWinamp Web Interface\n</title>\n");
}


// Sends styles and ends header
void Style(connection * conn)
{
	prints(conn,"<link rel=STYLESHEET href=\"wawi.css\" type=\"text/css\">\n");
}

// Sends a header which refreshes to the given URL after a second
void RefreshHeader(connection * conn, char * url)
{
	prints(conn,"<meta http-equiv=\"Refresh\" content=\"1; url=");
	prints(conn,url);
	prints(conn,"\">\n");
}

// Close HTML Header
void CloseHeader(connection * conn)
{
	AboutSource(conn);
	prints(conn,"</head>\n");
}

// Opens HTML page body
void OpenPageBody(connection * conn)
{
	prints(conn,"<body bgcolor=\"#");
	prints(conn,"EEEEEE");
	prints(conn,"\">");
	//OpenTable(conn);
}

// Opens HTML header body
void OpenHeaderBody(connection * conn)
{
	prints(conn,"<body leftmargin=\"0\" rightmargin=\"0\" topmargin=\"0\" bgcolor=\"#000000\">\n");
	//OpenTable(conn);
}

// Closes HTML body
void CloseBody(connection * conn)
{
	//CloseTable(conn);
	prints(conn,"</body>\n</html>\n\n");
}


// Open page table
void LinkTable(connection * conn)
{
	prints(conn,"<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n  <tr>\n");
	prints(conn,"<td width=\"2%\" bgcolor=\"#000000\">&nbsp;</td>\n");
	prints(conn,"<td width=\"96%\" bgcolor=\"#000000\">");
	ImgLinkBar(conn);
	prints(conn,"</td>\n<td width=\"2%\" bgcolor=\"#000000\">&nbsp;</td>\n</tr>\n</table>\n");
}


// --------------------------------------------------------------------------------
// Sticks a dot in... separator between stuff
void dot(connection * conn)
{
	prints(conn,"&middot;");
}

// Nonbreaking space
void nbsp(connection * conn)
{
	prints(conn,"&nbsp;");
}


// --------------------------------------------------------------------------------
// Image
void Img(connection * conn, int img, char * alt, int dx, int dy)
{
	prints(conn,"<img border=\"0\" src=\"img?image=");
	printsi(conn,img);
	prints(conn,"\" alt=\"");
	prints(conn,alt);
	prints(conn,"\"");
	
	if (dx != 0 || dy != 0)
	{
		prints(conn," width=\"");
		printsi(conn,dx);
		prints(conn,"\" height=\"");
		printsi(conn,dy);
		prints(conn,"\"");
	}	
	prints(conn,">");
}


// --------------------------------------------------------------------------------
// Image link
void ImgLink(connection * conn, char * link, int img, char * alt, int dx, int dy, int target)
{
	prints(conn,"<a href=\"");
	prints(conn,link);
	prints(conn,"\"");

	switch(frames)
	{
	case 1:		if (target == T_TOP)
					prints(conn," target=\"_top\"");
				break;
	case 2:		if (target == T_MAIN || target == T_BOTTOM)
					prints(conn," target=\"main\"");
				if (target == T_TOP)
					prints(conn," target=\"_top\"");
				break;
	case 3:		if (target == T_MAIN)
					prints(conn," target=\"main\"");
				if (target == T_BOTTOM)
					prints(conn," target=\"bottom\"");
				if (target == T_TOP)
					prints(conn," target=\"_top\"");
				break;
	default:	break;
	}


	prints(conn,"><img border=\"0\" src=\"img?image=");
	printsi(conn,img);
	prints(conn,"\" alt=\"");
	prints(conn,alt);
	prints(conn,"\">");
	
	if (dx != 0 || dy != 0)
	{
		prints(conn," width=\"");
		printsi(conn,dx);
		prints(conn,"\" height=\"");
		printsi(conn,dy);
		prints(conn,"\">");
	}	
	prints(conn,"</a>");
}

// --------------------------------------------------------------------------------
// Text Link
void Link(connection * conn, char * link, char * text, int target)
{
	prints(conn,"<a href=\"");
	prints(conn,link);
	prints(conn,"\"");

	switch(frames)
	{
	case 2:		if (target == T_MAIN || target == T_BOTTOM)
					prints(conn," target=\"main\"");
				break;
	case 3:		if (target == T_MAIN)
					prints(conn," target=\"main\"");
				if (target == T_BOTTOM)
					prints(conn," target=\"bottom\"");
				break;
	default:	break;
	}

	prints(conn,">");
	prints(conn,text);
	prints(conn,"</a>");
}

// --------------------------------------------------------------------------------
// Function source for javascript popup about window
void AboutSource(connection * conn)
{
	prints(conn,"<Script language=\"JavaScript\">\nfunction about()\n{\n   window.open(\"about\",\"newWin\",\"width=300,height=150\");\n}\n</script>\n\n");
}

// --------------------------------------------------------------------------------
// Displays the volume controls and link bar - Graphics version
void ImgLinkBar(connection * conn)
{
	prints(conn,"<p align=center>\n");

	// Play control links
	ImgLink(conn,"/prev",IDR_IMG_PREV,"Previous Track",0,0,T_BOTTOM);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	//nbsp(conn);
	ImgLink(conn,"/play",IDR_IMG_PLAY,"Play",0,0,T_BOTTOM);
	//nbsp(conn);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	ImgLink(conn,"/pause",IDR_IMG_PAUSE,"Pause",0,0,T_BOTTOM);
	//nbsp(conn);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	ImgLink(conn,"/stop",IDR_IMG_STOP,"Stop",0,0,T_BOTTOM);
	//nbsp(conn);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	ImgLink(conn,"/stopslow",IDR_IMG_STOPSLOW,"Stop with fadeout",0,0,T_BOTTOM);
	//nbsp(conn);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	ImgLink(conn,"/next",IDR_IMG_NEXT,"Next Track",0,0,T_BOTTOM);

	Img(conn,IDR_IMG_BLACK,"",8,30);

	// Volume control links
	ImgLink(conn,"/vol?volume=0",IDR_VOL_00,"0/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=1",IDR_VOL_01,"1/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=2",IDR_VOL_02,"2/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=3",IDR_VOL_03,"3/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=4",IDR_VOL_04,"4/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=5",IDR_VOL_05,"5/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=6",IDR_VOL_06,"6/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=7",IDR_VOL_07,"7/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=8",IDR_VOL_08,"8/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=9",IDR_VOL_09,"9/10",0,0,T_BOTTOM);
	ImgLink(conn,"/vol?volume=10",IDR_VOL_10,"10/10",0,0,T_BOTTOM);

	Img(conn,IDR_IMG_BLACK,"",8,30);

	// Page navigation links
	ImgLink(conn,"/main",IDR_IMG_MAIN,"Main",0,0,T_MAIN);
	//nbsp(conn);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	ImgLink(conn,"/list",IDR_IMG_PLAYLIST,"Playlist",0,0,T_MAIN);

	//nbsp(conn);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	ImgLink(conn,"/browse?path=%5c",IDR_IMG_BROWSE,"Music Collection",0,0,T_MAIN);

	//nbsp(conn);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	prints(conn,"<script language=\"JavaScript\">\nvar shortcut = new String(document.location);\nvar len = shortcut.lastIndexOf('/');\nshortcut = shortcut.slice(0,len+1);\n");
	prints(conn,"document.write(\"<a href=\\\"javascript:void(weblog=open('\" + shortcut + \"','','width=600,height=360,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1'))\\\"><img src=/img?image=\");");
	prints(conn,"\ndocument.write(");
	printsi(conn,IDR_IMG_POPUP);
	prints(conn,");");
	prints(conn,"\ndocument.write(\" border=\\\"0\\\" alt=\\\"Winamp Web Interface Popup Window\\\"></a>\");\n</script>");

	Img(conn,IDR_IMG_BLACK,"",2,30);
	ImgLink(conn,"/admin",IDR_IMG_ADMIN,"Wawi Admin",0,0,T_MAIN);

	//nbsp(conn);
	Img(conn,IDR_IMG_BLACK,"",2,30);
	ImgLink(conn,"javascript:about()",IDR_IMG_ABOUT,"About Winamp Web Interface",0,0,T_NONE);
	prints(conn,"</p>\n");


}

// --------------------------------------------------------------------------------
// Displays the volume controls and link bar - Noframes version
void LinkBar(connection * conn)
{
	prints(conn,"<p align=center>\n");

	// Volume control links
	prints(conn,"Volume: \n");
	for (int i=0;i<=10;i++)
	{
		prints(conn,"<a href=/vol?volume=");
		printsi(conn,i);
		prints(conn,">");
		printsi(conn,i);
		prints(conn,"</a>\n");
		if (i != 10)
			dot(conn);

	}
	prints(conn,"<br>\n");

	// Play control links
	prints(conn,"<a href=\"/prev\">Previous</a>\n");
	dot(conn);
	prints(conn," <a href=\"/play\">Play</a>\n");
	dot(conn);
	prints(conn," <a href=\"/pause\">Pause</a>\n");
	dot(conn);
	prints(conn," <a href=\"/stop\">Stop</a>\n");
	dot(conn);
	prints(conn," <a href=\"/next\">Next</A>\n");

	prints(conn,"<br><a href=\"/main\">Main</a>\n");
	dot(conn);
	prints(conn," <a href=\"/list\">Playlist</a>\n");

	dot(conn);
	prints(conn," <a href=\"/browse?path=%5c\">Music Collection</a>\n");
	dot(conn);
	prints(conn," <a href=\"/admin\">Admin</a>\n");

	prints(conn,"</p>\n");
}

// --------------------------------------------------------------------------------
// Retrieves and formats the information about the current song
void FullSongInfo(connection * conn)
{
	char tbuffer[255];

	int status = SendMessage(WAwnd(),WM_USER, 0, WAU_STATUS);
	int pos = SendMessage(WAwnd(),WM_USER, 0, WAU_GETPLPOS)+1;
	char * trackname = (char*)SendMessage(WAwnd(),WM_USER, pos-1, WAU_GETTITLE);

	//MessageBox(NULL,"Still going!","gen_httpSrv debug",MB_OK);
	switch(status)
	{
		case 1:		if (trackname==NULL)
						wsprintf(tbuffer,"Winamp is playing");
					else
						wsprintf(tbuffer,"Playing track %d - %s - ", pos,trackname);
					prints(conn,tbuffer);
					PrintTrackTime(conn);
					break;
		case 3:		if (trackname==NULL)
						wsprintf(tbuffer,"Winamp is paused");
					else
						wsprintf(tbuffer,"Paused in track %d - %s - ", pos,trackname);
						prints(conn,tbuffer);
						PrintTrackTime(conn);
						break;
		default:	if (trackname==NULL)
						wsprintf(tbuffer,"Winamp is stopped");
					else
						wsprintf(tbuffer,"Winamp is stopped at track %d - %s", pos, trackname );	
					prints(conn,tbuffer);
					break;
	}	
}

// --------------------------------------------------------------------------------
// Retrieves and formats a tiny string of the current song information (for /smalltitle)
void SongInfo(connection * conn)
{

	int status = SendMessage(WAwnd(),WM_USER, 0, WAU_STATUS);
	int pos = SendMessage(WAwnd(),WM_USER, 0, WAU_GETPLPOS)+1;
	char * trackname = (char*)SendMessage(WAwnd(),WM_USER, pos-1, WAU_GETTITLE);

	//MessageBox(NULL,"Still going!","gen_httpSrv debug",MB_OK);
	switch(status)
	{
		case 1:		if (trackname!=NULL)
					{
						//wsprintf(tbuffer,"%s", pos,trackname);
						prints(conn,trackname);
					}
					break;
		default:	break;
	}	
}
