#pragma once

#include "dllinterface.h"
#include "..\Utilities\list.h"
#include "..\Utilities\XString.h"
#include <dsound.h>

class SoundManager;
class DXSound;
class DXStreamingSound;
class WaveFile;


//-----------------------------------------------------------------------------
// Typing macros 
//-----------------------------------------------------------------------------
#define WAVEFILE_READ   1
#define WAVEFILE_WRITE  2

#define DXUT_StopSound(s)         { if(s) s->Stop(); }
#define DXUT_PlaySound(s)         { if(s) s->Play( 0, 0 ); }
#define DXUT_PlaySoundLooping(s)  { if(s) s->Play( 0, DSBPLAY_LOOPING ); }

//-----------------------------------------------------------------------------
// Name: class CSoundManager
// Desc: 
//-----------------------------------------------------------------------------
class DXFUNCTIONS_API DXSoundDevice
{
protected:
	friend DXSound;
	friend DXStreamingSound;
	friend WaveFile;

    IDirectSound8* m_pDS;
	LList<DXSound> m_Sounds;
	LList<DXStreamingSound> m_StreamingSounds;
	
	void Release(DXSound*s);
	void Release(DXStreamingSound*s);

public:
    DXSoundDevice();
    ~DXSoundDevice();

    void Initialize( HWND hWnd, DWORD dwCoopLevel );
    LPDIRECTSOUND8 GetDirectSound() { return m_pDS; }
    void SetPrimaryBufferFormat( DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
    LPDIRECTSOUND3DLISTENER Get3DListenerInterface(void);

    DXSound* Create(XString WaveFileName, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
    //DXSound* CreateFromMemory(BYTE* pbData, ULONG ulDataSize, LPWAVEFORMATEX pwfx, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
    DXStreamingSound* CreateStreaming(XString WaveFileName, DWORD dwCreationFlags, GUID guid3DAlgorithm, DWORD dwNotifyCount, DWORD dwNotifySize, HANDLE hNotifyEvent );
};


//-----------------------------------------------------------------------------
// Name: class DXSound
// Desc: Encapsulates functionality of a DirectSound buffer.
//-----------------------------------------------------------------------------
class DXFUNCTIONS_API DXSound
{
protected:
	friend DXSoundDevice;
	friend ListElement<DXSound>;
	friend LList<DXSound>;

	DXSoundDevice*		 m_Device;
    LPDIRECTSOUNDBUFFER* m_apDSBuffer;
    DWORD                m_dwDSBufferSize;
    WaveFile*            m_pWaveFile;
    DWORD                m_dwNumBuffers;
    DWORD                m_dwCreationFlags;

    bool RestoreBuffer(LPDIRECTSOUNDBUFFER pDSB);

	DXSound(DXSoundDevice * device, LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, DWORD dwNumBuffers, WaveFile* pWaveFile, DWORD dwCreationFlags );
    virtual ~DXSound(void);

public:
    void FillBufferWithSound( LPDIRECTSOUNDBUFFER pDSB, BOOL bRepeatWavIfBufferLarger );
    LPDIRECTSOUNDBUFFER GetFreeBuffer(void);
	LPDIRECTSOUNDBUFFER GetBuffer(DWORD dwIndex)
	{
		if( m_apDSBuffer == NULL || dwIndex >= m_dwNumBuffers )
			return NULL;
		return m_apDSBuffer[dwIndex];
	}

	LPDIRECTSOUND3DBUFFER Get3DBufferInterface(DWORD dwIndex)
	{
		if( dwIndex >= m_dwNumBuffers )
			return NULL;

		LPDIRECTSOUND3DBUFFER pDS3DBuffer = NULL;

		m_apDSBuffer[dwIndex]->QueryInterface( IID_IDirectSound3DBuffer,(VOID**)&pDS3DBuffer );

		return pDS3DBuffer;
	}

    void Play( DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lVolume = 0, LONG lFrequency = -1, LONG lPan = 0 );
    void Play3D( LPDS3DBUFFER p3DBuffer, DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lFrequency = 0 );

    void Stop(void)
	{
		if(m_apDSBuffer)
		{
			for(DWORD i=0;i<m_dwNumBuffers;++i)
				m_apDSBuffer[i]->Stop();
		}
	}

    void Reset(void)
	{
		if(m_apDSBuffer)
		{
			for(DWORD i=0;i<m_dwNumBuffers;++i)
				m_apDSBuffer[i]->SetCurrentPosition(0);
		}
	}

    bool IsSoundPlaying(void);
	WaveFile*GetWaveFile(void){return m_pWaveFile;}

	DWORD GetBufferCount(void){return m_dwNumBuffers;}
	void SetBufferCount(DWORD numbuffers);
};


//-----------------------------------------------------------------------------
// Name: class CStreamingSound
// Desc: Encapsulates functionality to play a wave file with DirectSound.  
//       The Create() method loads a chunk of wave file into the buffer, 
//       and as sound plays more is written to the buffer by calling 
//       HandleWaveStreamNotification() whenever hNotifyEvent is signaled.
//-----------------------------------------------------------------------------
class DXFUNCTIONS_API DXStreamingSound : public DXSound
{
protected:
	friend ListElement<DXStreamingSound>;
	friend LList<DXStreamingSound>;
	friend DXSoundDevice;

    DWORD m_dwLastPlayPos;
    DWORD m_dwPlayProgress;
    DWORD m_dwNotifySize;
    DWORD m_dwNextWriteOffset;
    BOOL  m_bFillNextNotificationWithSilence;

    DXStreamingSound(DXSoundDevice * device, LPDIRECTSOUNDBUFFER pDSBuffer, DWORD dwDSBufferSize, WaveFile* pWaveFile, DWORD dwNotifySize );
    ~DXStreamingSound(void);

public:
    void HandleWaveStreamNotification( BOOL bLoopedPlay );
    void Reset(void);
};


//-----------------------------------------------------------------------------
// Name: class CWaveFile
// Desc: Encapsulates reading or writing sound data to or from a wave file
//-----------------------------------------------------------------------------
class DXFUNCTIONS_API WaveFile

{
public:
	friend DXSoundDevice;
	friend DXSound;
	friend DXStreamingSound;

    WAVEFORMATEX* m_pwfx;        // Pointer to WAVEFORMATEX structure
    HMMIO         m_hmmio;       // MM I/O handle for the WAVE
    MMCKINFO      m_ck;          // Multimedia RIFF chunk
    MMCKINFO      m_ckRiff;      // Use in opening a WAVE file
    DWORD         m_dwSize;      // The size of the wave file
    MMIOINFO      m_mmioinfoOut;
    DWORD         m_dwFlags;
    BOOL          m_bIsReadingFromMemory;
    BYTE*         m_pbData;
    BYTE*         m_pbDataCur;
    ULONG         m_ulDataSize;
    CHAR*         m_pResourceBuffer;
	XString		  m_FileName;

protected:
    void ReadMMIO(void);
    void WriteMMIO( WAVEFORMATEX *pwfxDest );

    WaveFile(void);
    ~WaveFile(void);

public:
    void Open(XString strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags );
    void OpenFromMemory(BYTE* pbData, ULONG ulDataSize, WAVEFORMATEX* pwfx, DWORD dwFlags );
    void Close(void);

    void Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead );
    void Write(UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote );

	DWORD GetSize(){return m_dwSize;}
    void ResetFile(void);
    WAVEFORMATEX* GetFormat() { return m_pwfx; };

	XString GerFileName(void){return m_FileName;}
};
