// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   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
// -------------------------- END HEADER -------------------------------------

#pragma once

#include <map>
#include <string>

#include "../soundmanager/soundmanager.h"

class World;

namespace Threads {
    class Thread;
    class Mutex;
};

/// This lets us handle cleaning up old sound resources. Also owns the
/// main SoundManager.
class SoundResourceManager {
	
	public:
	
		SoundResourceManager(World *world);
		~SoundResourceManager(void);
		
        /// Add a sound to be played in the sound thread.
        void queueSound(const std::string &fileName, float volume = 1);

        /// Set the music to be played. Setting this to "" will stop
        /// the music.
        void queueMusic(const std::string &musicName);

        // All of these just wrap to the same things in SoundManager,
        // but also lock/unlock the mutex for it.

        /// Get the current SFX volume.
        float getSoundVolume();

        /// Get the current music volume.
        float getMusicVolume();

        /// Set the SFX volume (0 to 1).
        void setSoundVolume(float v);

        /// Set the music volume (0 to 1).
        void setMusicVolume(float v);

	private:
	
		/// Clean up old sounds.
		void cleanOldSounds(unsigned int maxAge = 9000);

		/// Main SoundManager.
		Sound::SoundManager soundManager;
		
		/// Get a sound by its filename. Don't store this value, just
		/// use it to play something immediately. (Unlike texture
		/// manager indexes that never change.)
		unsigned int getSound(const std::string &fileName);

		std::map<std::string, unsigned int> soundsByName;
		std::map<unsigned int, std::string> namesByNumber;
		std::map<unsigned int, unsigned int> soundAges;

        class QueuedSound {
            public:
                float volume;
                std::string fileName;
        };

        std::vector<QueuedSound> queuedSounds;
        std::string queuedMusic;
        std::string currentMusic;

        void update(void);		
        friend void soundUpdateFunc(SoundResourceManager *soundResourceManager);
        bool soundUpdateFuncQuit;
        Threads::Thread *soundUpdateThread;
        Threads::Mutex *soundManagerMutex;
        Threads::Mutex *soundQueueMutex;

		World *world;
};
