/*	
	Now playing plugin for Sonique 2.0
    Copyright (C) 2003  Jerome Kuptz

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#define INITMID
#define _USE_MIA_
#include "MediaFilterNow.h"
#include "sq2_now_playing.h"
#include "Interface_Dialog.h"
#include "ioutils.h"
#include "mia.h"

// this is the mml that gets run when a user selects the settings page.  
// bits modded this mml to update the settings when the checkbox is clicked.
const char ConfigDialogue[] =
"<?mml version=\"0.6\" ?>"
"<mml>"
"	<interface	unique_name=\"teamsonique/sonique2_Now_prefs/1.0\" >"
"		<window name=\"NowPrefs\" offset=\"0,0,550,250\" alignment=\"0,0,0,0\" transparent=\"true\" >"
"			<ImageStyle name=\"background\"> <color_rect color=\"MML_Mood_Color3\" alignment=\"entire\" blendmode=\"COPY|RGB\" /> </ImageStyle>"
"			<window name=\"markup0\" type=\"textdisplay\" offset=\"30,5,60,15\" alignment=\"0,0,1,0\" transparent=\"true\" >"
"				<property font_color=\"MML_Mood_Color3\" />"
"				<script name=\"OnInit\"> SetText(\"Formatting Variables:\");</script>"
"			</window>"
"			<window name=\"markup1\" type=\"textdisplay\" offset=\"30,16,130,88\" alignment=\"0,0,0,0\" transparent=\"true\" >"
"				<property font_color=\"MML_Mood_Color3\" />"
"				<script name=\"OnInit\"> SetText(\"%a = artist\r\n%y = year\r\n%r = track\r\n%d = duration\r\n%e = extension\r\n%h = channel\r\n\r\n\");</script>"
"			</window>"
"			<window name=\"markup2\" type=\"textdisplay\" offset=\"130,16,230,88\" alignment=\"0,0,0,0\" transparent=\"true\" >"
"				<property font_color=\"MML_Mood_Color3\" />"
"				<script name=\"OnInit\"> SetText(\"%t = title\r\n%f = format\r\n%u = file url\r\n%v = cbr\r\n%n = filename\r\n\r\n\");</script>"
"			</window>"
"			<window name=\"markup3\" type=\"textdisplay\" offset=\"230,16,330,88\" alignment=\"0,0,0,0\" transparent=\"true\" >"
"				<property font_color=\"MML_Mood_Color3\" />"
"				<script name=\"OnInit\"> SetText(\"%l = album\r\n%g = genre\r\n%b = bitrate\r\n%s = samplerate\r\n%x = version\r\n\");</script>"
"			</window>"
"			<window name=\"enableCheckbox\" type=\"button\" offset=\"30,89,40,103\" alignment=\"0,0,0,0\" transparent=\"true\" hitstyle=\"ACTIVE\" >"
"				<property hover=\"true\"/>"
"				<imagestyle name=\"state0\"> <include source=\"MML_Mood_DlgCheckBoxState0\" /> </imagestyle>"
"				<imagestyle name=\"state1\"> <include source=\"MML_Mood_DlgCheckBoxState1\" /> </imagestyle>"
"				<script name=\"OnInit\">"
"					SetToggle(true);"
"					SetButtonState( :sonique.players.default.settings.NowPlaying.Get(\"enabled\") ); "
"				</script>"
"				<script name=\"OnClick\">  if( GetButtonState() ) { :sonique.players.default.settings.NowPlaying.Set(\"enabled\", 1); :sonique.players.default.settings.NowPlaying.Set(\"file\", filePath.GetText()); :sonique.players.default.settings.NowPlaying.Set(\"file\", filePath.GetText()); :sonique.players.default.settings.NowPlaying.Set(\"filefrmt\", filefrmt.GetText()); :sonique.players.default.settings.NowPlaying.Set(\"mirc_cmd\", mircCmd.GetText()); } else { :sonique.players.default.settings.NowPlaying.Set(\"enabled\", 0); }</script>"
"			</window>"
"			<window name=\"enableTitle\" type=\"textdisplay\" offset=\"45,89,60,103\" alignment=\"0,0,1,0\" transparent=\"true\" >"
"				<property font_color=\"MML_Mood_Color3\" />"
"				<script name=\"OnInit\"> SetText(:language.Translate(\"settings/generic.enable\")); </script>"
"			</window>"
"			<window name=\"fileCheckbox\" type=\"button\" offset=\"30,104,40,118\" alignment=\"0,0,0,0\" transparent=\"true\" hitstyle=\"ACTIVE\" >"
"				<property hover=\"true\"/>"
"				<imagestyle name=\"state0\"> <include source=\"MML_Mood_DlgCheckBoxState0\" /> </imagestyle>"
"				<imagestyle name=\"state1\"> <include source=\"MML_Mood_DlgCheckBoxState1\" /> </imagestyle>"
"				<script name=\"OnInit\">"
"					SetToggle(true);"
"					SetButtonState( :sonique.players.default.settings.NowPlaying.Get(\"usefile\") ); "
"				</script>"
"				<script name=\"OnClick\">if( GetButtonState() ) { :sonique.players.default.settings.NowPlaying.Set(\"usefile\", 1); :sonique.players.default.settings.NowPlaying.Set(\"file\", filePath.GetText()); :sonique.players.default.settings.NowPlaying.Set(\"file\", filePath.GetText()); :sonique.players.default.settings.NowPlaying.Set(\"filefrmt\", filefrmt.GetText()); } else { :sonique.players.default.settings.NowPlaying.Set(\"usefile\", 0); }</script>"
"			</window>"
"			<window name=\"fileTitle\" type=\"textdisplay\" offset=\"45,104,100,118\" alignment=\"0,0,0,0\" transparent=\"true\" >"
"				<property font_color=\"MML_Mood_Color3\" />"
"				<script name=\"OnInit\"> SetText(\"Write to File\");</script>"
"			</window>"
"			<window name=\"unicodeCheckbox\" type=\"button\" offset=\"110,104,120,118\" alignment=\"0,0,0,0\" transparent=\"true\" hitstyle=\"ACTIVE\" >"
"				<property hover=\"true\"/>"
"				<imagestyle name=\"state0\"> <include source=\"MML_Mood_DlgCheckBoxState0\" /> </imagestyle>"
"				<imagestyle name=\"state1\"> <include source=\"MML_Mood_DlgCheckBoxState1\" /> </imagestyle>"
"				<script name=\"OnInit\">"
"					SetToggle(true);"
"					SetButtonState( :sonique.players.default.settings.NowPlaying.Get(\"useunicode\") ); "
"				</script>"
"				<script name=\"OnClick\">if( GetButtonState() ) { :sonique.players.default.settings.NowPlaying.Set(\"useunicode\", 1); :sonique.players.default.settings.NowPlaying.Set(\"file\", filePath.GetText()); :sonique.players.default.settings.NowPlaying.Set(\"filefrmt\", filefrmt.GetText()); } else { :sonique.players.default.settings.NowPlaying.Set(\"useunicode\", 0); }</script>"
"			</window>"
"			<window name=\"unicodeTitle\" type=\"textdisplay\" offset=\"125,104,200,118\" alignment=\"0,0,0,0\" transparent=\"true\" >"
"				<property font_color=\"MML_Mood_Color3\" />"
"				<script name=\"OnInit\"> SetText(\"Use Unicode\");</script>"
"			</window>"
"			<window name=\"filePath\" type=\"EditBox\" offset=\"30,119,-20,139\" alignment=\"0,0,1,0\" transparent=\"true\" >"
"				<imagestyle name=\"background_inactive\">"
"					<color_fill alignment=\"entire\" color=\"MML_Mood_Color2\" />"
"				</imagestyle>"
"				<imagestyle name=\"background_active\">"
"					<color_fill alignment=\"entire\" color=\"MML_Mood_Color1\" />"
"				</imagestyle>"
"				<property font_color=\"MML_Mood_Color3\" cursor_on_color=\"MML_Mood_Color3\" cursor_off_color=\"0x00FFFFFF\"/>"
"				<script name=\"OnInit\"> if (:sonique.players.default.settings.NowPlaying.Get(\"file\") ) { SetText(:sonique.players.default.settings.NowPlaying.Get(\"file\"));} else { SetText(\"c:\\\\sq2_now.txt\"); }</script>"
"				<script name=\"OnDestroy\"> :sonique.players.default.settings.NowPlaying.Set(\"file\", GetText());</script>"
"			</window>"
"			<window name=\"filefrmt\" type=\"EditBox\" offset=\"30,140,-20,160\" alignment=\"0,0,1,0\" transparent=\"true\" >"
"				<imagestyle name=\"background_inactive\">"
"					<color_fill alignment=\"entire\" color=\"MML_Mood_Color2\" />"
"				</imagestyle>"
"				<imagestyle name=\"background_active\">"
"					<color_fill alignment=\"entire\" color=\"MML_Mood_Color1\" />"
"				</imagestyle>"
"				<property font_color=\"MML_Mood_Color3\" cursor_on_color=\"MML_Mood_Color3\" cursor_off_color=\"0x00FFFFFF\"/>"
"				<script name=\"OnInit\"> if (:sonique.players.default.settings.NowPlaying.Get(\"filefrmt\") ) { SetText(:sonique.players.default.settings.NowPlaying.Get(\"filefrmt\"));} else { SetText(\"Sonique %x »» [#%r][%a - %t[%l %y]] {%d %v %bKbps %e %sKHz %h}\"); }</script>"
"				<script name=\"OnDestroy\"> :sonique.players.default.settings.NowPlaying.Set(\"filefrmt\", GetText());</script>"
"			</window>"
"			<window name=\"mircCheckbox\" type=\"button\" offset=\"30,170,40,180\" alignment=\"0,0,0,0\" transparent=\"true\" hitstyle=\"ACTIVE\" >"
"				<property hover=\"true\"/>"
"				<imagestyle name=\"state0\"> <include source=\"MML_Mood_DlgCheckBoxState0\" /> </imagestyle>"
"		 		<imagestyle name=\"state1\"> <include source=\"MML_Mood_DlgCheckBoxState1\" /> </imagestyle>"
"				<script name=\"OnInit\">"
"					SetToggle(true);"
"					SetButtonState( :sonique.players.default.settings.NowPlaying.Get(\"usemirc\") ); "
"				</script>"
"				<script name=\"OnClick\"> if( GetButtonState() ) { :sonique.players.default.settings.NowPlaying.Set(\"usemirc\", 1); :sonique.players.default.settings.NowPlaying.Set(\"mirc_cmd\", mircCmd.GetText()); } else { :sonique.players.default.settings.NowPlaying.Set(\"usemirc\", 0); }</script>"
"			</window>"
"			<window name=\"mircTitle\" type=\"textdisplay\" offset=\"45,170,40,180\" alignment=\"0,0,1,0\" transparent=\"true\" >"
"				<property font_color=\"MML_Mood_Color3\" />"
"				<script name=\"OnInit\"> SetText(\"Send to mIRC\");</script>"
"			</window>"
"			<window name=\"mircCmd\" type=\"EditBox\" offset=\"30,185,-20,205\" alignment=\"0,0,1,0\" transparent=\"true\" >"
"				<imagestyle name=\"background_inactive\">"
"					<color_fill alignment=\"entire\" color=\"MML_Mood_Color2\" />"
"				</imagestyle>"
"				<imagestyle name=\"background_active\">"
"					<color_fill alignment=\"entire\" color=\"MML_Mood_Color1\" />"
"				</imagestyle>"
"				<property font_color=\"MML_Mood_Color3\" cursor_on_color=\"MML_Mood_Color3\" cursor_off_color=\"0x00FFFFFF\"/>"
"				<script name=\"OnInit\"> if (:sonique.players.default.settings.NowPlaying.Get(\"mirc_cmd\") ) { SetText(:sonique.players.default.settings.NowPlaying.Get(\"mirc_cmd\"));} else { SetText(\"/echo 14 -a Sonique %x »» [#%r][%a - %t[%l %y]] {%d %v %bKbps %e %sKHz %h}\"); }</script>"
"				<script name=\"OnDestroy\"> :sonique.players.default.settings.NowPlaying.Set(\"mirc_cmd\", GetText());</script>"
"			</window>"
"			<window name=\"updateSettings\" type=\"button\" offset=\"30,215,120,235\" alignment=\"0,0,0,0\" hitstyle=\"ACTIVE\" >"
"				<imagestyle name=\"state0\"> <include source=\"MML_Mood_Button_State0\"/> </imagestyle>"
"				<script name=\"OnInit\"> SetText(\"Apply changes\"); </script>"
"				<script name=\"OnClick\"> :sonique.players.default.settings.NowPlaying.Set(\"mirc_cmd\", mircCmd.GetText()); :sonique.players.default.settings.NowPlaying.Set(\"filefrmt\", filefrmt.GetText()); :sonique.players.default.settings.NowPlaying.Set(\"file\", filePath.GetText()); </script>"
"				<property hoverhint=\"apply plugin changes\" default_delay=\"150\" text=\"Apply\" font_color=\"MML_Mood_Color3\"/>"
"			</window>"
"		</window>"
"	</interface>"
"</mml>";

class NowPrefsDialog : public DialogQueryBase
{
public:
	virtual bool	__stdcall	GetDialogSource( DialogSource& toHere ) const
	{
		PackageMemoryPtr pPackage;
		if( !pPackage->SetRecord( (void*)ConfigDialogue, strlenof(ConfigDialogue), "NowPrefs.xml") )
			return false;

		toHere.wsInterfaceName	= "teamsonique/sonique2_Now_prefs/1.0";
		toHere.asSourceName		= "NowPrefs.xml";
		toHere.wsWindowName		= "NowPrefs";
		toHere.pPackage = pPackage;
		return true;
	}
	
	NowPrefsDialog() { 	MGENERIC_CONSTRUCTOR(); }
	

	IMPLEMENT_REFERENCECOUNT()
	IMPLEMENT_QUERYMODULE()
	
};

class SQ2_Audio_Now_dll : public MIFactory
{
public:
	// MIFactory
	virtual MRESULT	__stdcall	CreateInstance( const MID& mInterface, void** ppToHere,  MObject* pOuter ) ;

protected:
	// MGeneric
	IMPLEMENT_REFERENCECOUNT();
	IMPLEMENT_QUERYMODULE();

public:
	SQ2_Audio_Now_dll();
	~SQ2_Audio_Now_dll();
};
//*************************************************************************
// non-virtuals functions
//*************************************************************************
SQ2_Audio_Now_dll::SQ2_Audio_Now_dll()
{
	MGENERIC_CONSTRUCTOR();
}

SQ2_Audio_Now_dll::~SQ2_Audio_Now_dll()
{
}
//*************************************************************************
// non-virtuals functions
//*************************************************************************
MRESULT SQ2_Audio_Now_dll::CreateInstance( const MID& mInterface, void** ppToHere,  MObject* /*pOuter*/ )
{
	if( mInterface == MID_MEDIAFILTER_NOW || mInterface == MID_MEDIAFILTER)
	{
		*ppToHere = (MMediaFilter*)new NowFilter();
	}
	else if( mInterface == MID_DIALOG_QUERY_OBJECT )
	{
		DialogQueryBase* pDialogQuery = new NowPrefsDialog();
		*ppToHere = pDialogQuery;
	} 
	else
		return GetBasicError(ERR_BASIC_UNKNOWNOBJECT);

	if( *ppToHere )
	{
		((MGeneric*)(*ppToHere))->AddRef();
		return M_OK;
	}

	return GetBasicError(ERR_BASIC_OUTOFMEMORY);
}


//*************************************************************************
// Main Exports
//*************************************************************************
extern "C" __declspec (dllexport) 
MRESULT RegisterMIA( bool bRegister, MMiaControl* pControl, const wchar_t* wcPath )
{
	//GFID_MEDIA_MISC  - change it from a DSP to a MISC plugin once the support is back in place.
	if( SUCCEEDED(pControl->RegisterIFactory(bRegister, FID_SQ2_AUDIO_NOW, wcPath, "Now Playing")) &&
		SUCCEEDED(pControl->RegisterGenericFactory(bRegister, GFID_MEDIAFILTER_DSP, FID_SQ2_AUDIO_NOW, "Now Playing")) )
		return M_OK;

	return GetBasicError(ERR_BASIC_UNKNOWNERROR);
}

extern "C" __declspec (dllexport) 
MRESULT MIFactory(const MID& thisFID,MIFactory** ppToHere)
{
	if( thisFID == FID_SQ2_AUDIO_NOW )
	{
		SQ2_Audio_Now_dll* pNowAudio = new SQ2_Audio_Now_dll;
		pNowAudio->AddRef();
		*ppToHere = pNowAudio;
		return M_OK;
	}
	
	return GetBasicError(ERR_BASIC_UNKNOWNOBJECT);
}


static int32 g_nAttachCount;
extern "C" __declspec (dllexport)
uint32 DLLAttach( int32 nAttach )
{
	if( nAttach == 1 )
	{
		// first attach event?
		if( AtomicIncrement( (long*)&g_nAttachCount ) == 1 )
		{
			if( !InitMIA() )
				return false;
		}
	}
	if( nAttach == -1 )
	{
		// last detach
		if( AtomicDecrement( (long*)&g_nAttachCount ) == 0 )
		{
			DeInitMIA();
		}
	}
		
	return true; // it worked
}
