/*
* [U]NITED [G]AMING [C]HEATS Client Hook
*
* Copyright (c) LIQUID     2001-2002
* Copyright (c) ZOOLABUS   2001-2002
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* 
* GNU license doesn't cover Engine directory.
* Content of Engine directory is copyrighted (c) 1999, 2000, by Valve LLC
* and it is licensed under Valve's proprietary license (see original HL SDK).
*/

#include "stdafx.h"
#include <windows.h>
#include "winamp.h"
#include "cvar.h"
#include "client.h"
#include <assert.h>

//================================================================================
Winamp winamp;

//================================================================================
Winamp::Winamp(void)
{
	hwnd=0;
	crossfade=1.0f;
	paused=0;
	playing=1;
}

//================================================================================
void Winamp::setfracvol(void)
{
	float f = crossfade;
	f = f*f;
	int wvol = cvar.wa_vol1 + (DWORD)(f*(cvar.wa_vol2-cvar.wa_vol1));
	assert(hwnd);
	PostMessage(hwnd,WM_USER,wvol,122);
	float hlvol = cvar.hl_vol1 + (cvar.hl_vol2-cvar.hl_vol1)*(1.0f-f);
	char buf[64]; sprintf(buf,"volume %f",hlvol);
	gEngfuncs.pfnClientCmd(buf);
}

//================================================================================
void Winamp::fadein(void)
{
    if(crossfade==1.0 || !playing) return;
	if(paused) command("pause");
	crossfade += cvar.wa_fadeinspeed;
	if(crossfade>1.0f) crossfade = 1.0f;
	setfracvol();
}

//================================================================================
void Winamp::fadeout(void)
{
	if(crossfade==0.0f)
	{
		if(!paused)
		{
			if(!cvar.wa_vol1)
			{
				command("pause");// pause
				command("back" );
			}
		}
		return;
	}
	crossfade -= cvar.wa_fadeoutspeed;
	if(crossfade<0) crossfade = 0;
	setfracvol();
}

//================================================================================
void Winamp::frame(void)
{
	if(searchForWindowEventCounter.get()) { searchWindow(); }

	if(hwnd)
	{
		int n = fadeEventCounter.get();
		if(cvar.wa_autovolume)
		{
			for(int i=0;i<n;i++)
			{
				if(!me.alive)  
				{
					fadein();
				}
				else 
				{
					fadeout();
				}
			}
		}
		if(titleUpdateEventCounter.get()&&playing) { updateTitle(); }
	}
	else
	{
		title.erase();
	}
}

//================================================================================
void Winamp::searchWindow()
{
	HWND old = hwnd;
	hwnd = FindWindow("Winamp v1.x",NULL); 
    
	if(old!=hwnd)
	{
		if(hwnd) 
		{ 
			if(!old) { Con_Echo("Winamp found. Controls available."); }
			else     { Con_Echo("Found another Winamp instance.");    }
		}
		else     { Con_Echo("WinAmp window lost."              ); }
	}
}

//================================================================================
void Winamp::updateTitle()
{
	assert(hwnd);
	char this_title[2048],*p;
	int success = GetWindowText(hwnd,this_title,sizeof(this_title)); 
	if(!success) { title.erase(); return; }

	p = this_title+strlen(this_title)-8;
	while (p >= this_title)
	{
		if (!strnicmp(p,"- Winamp",8)) break;
		p--;

	}
	if (p >= this_title) p--;
	while (p>=this_title && *p==' ') p--;
	*++p=0;

	char* pos = this_title;
	while(*pos){ if(*pos<' '||*pos>127){ *pos='-';} ++pos;}

	title = this_title;
}

//================================================================================
void Winamp::command(const string& subcommand, const string& arg1, const string& arg2, const string& arg3)
{
	// hwnd independent:
	if(0) {}
	else if (subcommand=="1") { cvar.wa_active=1; }
	else if (subcommand=="0") { cvar.wa_active=0; }
	
	if(!cvar.wa_active)       { Con_Echo("&rtype \"winamp 1\" first!"); return; }
	if(!hwnd)                 { Con_Echo("&rWinamp has to be running"); return; }

	// hwnd dependent:
	#define CHECK_COMMAND(name,number)  } else if(subcommand==name) { PostMessage(hwnd,WM_COMMAND,number,0); 
	if(0){
	CHECK_COMMAND("play"   ,40045) paused=false; playing=true;
	CHECK_COMMAND("pause"  ,40046) if(playing) paused=!paused;
	CHECK_COMMAND("stop"   ,40047) paused=false; playing=false;title.erase();
	CHECK_COMMAND("forward",40148) 
	CHECK_COMMAND("back"   ,40144)
	CHECK_COMMAND("next"   ,40048)
	CHECK_COMMAND("prev"   ,40044)
	CHECK_COMMAND("close"  ,40001)
	} 

	if(0) {}
	else if(subcommand=="repeat" ) { PostMessage(hwnd,WM_USER,atoi(arg1.c_str()),253); }
	else if(subcommand=="shuffle") { PostMessage(hwnd,WM_USER,atoi(arg1.c_str()),252); }
	else if(subcommand=="volume" ) { PostMessage(hwnd,WM_USER,atoi(arg1.c_str()),122); }

}
