/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2008.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: src/sgx/video/videomanager.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/video/video.hpp"

CSGXVideoManager	*CSGXVideoManager::m_pSingleton;

CSGXVideoManager	*
CSGXVideoManager::get()
{
	if (!m_pSingleton) {
		m_pSingleton = new CSGXVideoManager();
	}
	return m_pSingleton;
}

CSGXVideoManager::CSGXVideoManager() : m_pDefaultManager(NULL) {
}

tBOOL
CSGXVideoManager::registerObject(const sgxString &id, CVideoFormatManager *pManager)
{
    return m_VideoProtocols.insert(ProtocolMap::value_type(id, pManager)).second ? TRUE : FALSE;
}

tBOOL	
CSGXVideoManager::unregisterObject(const sgxString &id)
{
	m_VideoProtocols.erase(id);
	return FALSE;
}

CVideoFormatManager *
CSGXVideoManager::setDefaultManager(CVideoFormatManager *pManager)
{
	CVideoFormatManager *pPrev = m_pDefaultManager;
	m_pDefaultManager = pManager;
	return pPrev;
}

CVideo *
CSGXVideoManager::createVideo(const sgxString &name, const CVideo::CMovieParameters &params)
{
	int n = name.find_first_of(':');
	if (n == -1) {
		if (m_pDefaultManager) {
			return m_pDefaultManager->createVideo(name, params);
		}
		return NULL;
	}
	sgxString protocol = name.substr(0, n);
	sgxString subname = name.substr(n+1);

    ProtocolMap::iterator it = m_VideoProtocols.find(protocol);

    if (it == m_VideoProtocols.end()) {
        return NULL;
    }

	return (*it->second).createVideo(subname, params);
}
