/*
 *  TnGenericResourceProxy.h
 *
 *  To enable logging, use "TnGenericResourceProxy".
 *
 *  The job of the generic resource proxy is to look at the resource
 *  request and decide whether it is file resource or a network
 *  resource. And pass it along to the appropriate sub-proxy.
 *
 *  (It's not clear to me that these two kinds of resources should be
 *  handled by the same proxy, but that's the way things work now.)
 *
 *  Note that network resource fetches are asynchronous, while file
 *  resource fetches are synchronous.
 *
 *  http://spaces.telenav.com:8080/display/eng/Generic+Resource+Proxy
 */

#ifndef __TN_GENERIC_RESOURCE_PROXY_H__
#define __TN_GENERIC_RESOURCE_PROXY_H__

#include <string>

#include <RenderEngine/ITnMapResourceProxy.h>
#include <GenericProxy/TnFileResourceProxy.h>
#include <GenericProxy/TnCachingNetworkResourceProxy.h>


class TnGenericResourceProxy : public ITnMapResourceProxy
{
public:
    typedef Tn::Foundation::shared_ptr<TnGenericResourceProxy> ptr;
    
    /* Constructor takes:
     *
     *   a filename translation function which just
     *   turns around and passes it to the File Resource Proxy;
     *
     */

    static ptr Create(TnFileNameTranslatorFunc = 0, size_t numThreads = 4);

    ~TnGenericResourceProxy();

    void AddCache(TnCache::ptr const &);

    virtual bool PrefetchSupported(std::string const & cache) const;
    virtual void RequestResourcePrefetch(std::string const & url, std::string const & cacheName, uint32_t priority);
    virtual bool RequestResourceSynchronously(ITnMapResourceData::ptr const & data);

    virtual void RequestResource(ITnMapResourceData::ptr const & data, uint32_t priority);
    virtual void CancelRequest(ITnMapResourceData::ptr const & data);

    /* In development mode, the file proxy will first try to find the
     * resource stripped of its path. This is to compensate for the
     * fact that iOS does not (yet?) support uploading resource files
     * to the device in subdirectories. 
     */

    void SetDevMode(bool);

    /* These just turn around and call the corresponding member
     * functions in the Network Resource Proxy.
     */
    void EnableHttpProxy(const std::string & url, int port, const std::string & name = "", const std::string & password = "");
    void DisableHttpProxy();
    
private:


    TnGenericResourceProxy(TnFileNameTranslatorFunc, size_t numThreads);

    TnFileResourceProxy::ptr m_fileResourceProxy;

    TnCachingNetworkResourceProxy::ptr m_networkResourceProxy;

    bool m_supportReported;
};

#endif // __TN_GENERIC_RESOURCE_PROXY_H__


