/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */
#ifndef KEOS_MEDIAMANAGER_H
#define KEOS_MEDIAMANAGER_H

#include "KeosPrerequisites.h"
#include "KeosSingleton.h"
#include "KeosGenHierarchies.h"
#include "KeosFile.h"
#include "KeosUtil.h"
#include "KeosLoader.h"
#include "KeosException.h"
#include "KeosString.h"

namespace Keos
{
    //! List of the taken care media
    typedef TYPELIST_3(CModel, CImage, IShaderBase) MediaList;

    //! Media manager model
    template <class T>
    struct CMediaHolder
    {
        typedef std::map<String, CSmartPtr<ILoader<T> > > TLoadersMap;
        TLoadersMap m_Loaders;
    };

    //-----------------------------------------------------------------------
    /** Media manager.
     *  Inherit from a CMediaHolder for every managed media
     */
    class KEOS_EXPORT CMediaManager : public CSingleton<CMediaManager>, public CScatteredHierarchy<MediaList, CMediaHolder>
    {
    friend CSingleton<CMediaManager>;

    public :

        /** Add a search path directory for the media.
         */
        void AddSearchPath(const String& strPath);

        /** Register a new T loader.
         *  @param pLoader Pointer on the loader.
         *  @param strExtensions Extensions associated to the loader.
         */
        template <class T> void RegisterLoader(ILoader<T>* pLoader, const String& strExtensions);

        /** Load a T object from a file.
         *  @param Filename Path of the file.
         *  @return Pointer on T loaded.
         */
        template <class T> T* LoadMediaFromFile(const CFile& Filename) const;

        /** Save a T object in a file.
         *  @param pObject Pointer on the object to be saved.
         *  @param Filename Path of the file.
         */
        template <class T> void SaveMediaToFile(const T* pObject, const CFile& Filename) const;

        /** Find a file in the search path directories.
         */
        CFile FindMedia(const CFile& Filename) const;

    private :

        /** Default constructor.
         */
        CMediaManager();

        /** Destructor.
         */
        ~CMediaManager();

        /** Look for the loader corresponding to a given extension.
         *  @param Filename File.
         *  @return Reference on the loader.
         */
        template <class T> ILoader<T>& FindLoader(const CFile& Filename) const;

        //! List of the search paths
        std::set<String> m_Paths;
    };

    //=======================================================================
    // CMediaManager inline functions
    //=======================================================================
    
    //-----------------------------------------------------------------------
    template <class T>
    inline void CMediaManager::RegisterLoader(ILoader<T>* pLoader, const String& strExtensions)
    {
        // Recovery of the extensions
        std::vector<String> Ext;
        CStringUtil::Split(strExtensions, Ext, " /\\*.,;|-_\t\n'\"");

        // Add the extensions
        CSmartPtr<ILoader<T> > Ptr = pLoader;
        for (std::vector<std::string>::iterator i = Ext.begin(); i != Ext.end(); ++i)
            CMediaHolder<T>::m_Loaders[CStringUtil::ToLower(*i)] = Ptr;
    }

    //-----------------------------------------------------------------------
    template <class T>
    inline T* CMediaManager::LoadMediaFromFile(const CFile& Filename) const
    {
        // Search for the file in the registered directories
        CFile MediaPath = FindMedia(Filename);

        // Search the appropriate loader
        return FindLoader<T>(MediaPath).LoadFromFile(MediaPath.Fullname());
    }

    //-----------------------------------------------------------------------
    template <class T>
    inline void CMediaManager::SaveMediaToFile(const T* pObject, const CFile& Filename) const
    {
        // Search the appropriate loader
        FindLoader<T>(Filename).SaveToFile(pObject, Filename.Fullname());
    }

    //-----------------------------------------------------------------------
    template <class T>
    inline ILoader<T>& CMediaManager::FindLoader(const CFile& Filename) const
    {
        // Search the extension in the map
        typename CMediaHolder<T>::TLoadersMap::const_iterator It = CMediaHolder<T>::m_Loaders.find(CStringUtil::ToLower(Filename.Extension()));

        // If we find the extension, we return the loader
        if ((It != CMediaHolder<T>::m_Loaders.end()) && It->second)
            return *It->second;

        LOADINGFAILED_EXCEPT(Filename.Fullname(), "No loader takes care of this file format", "CMediaManager::FindLoader");
    }

} // namespace Keos


#endif // KEOS_MEDIAMANAGER_H
