/*
 *  TnProtoBufToTrafficDataProxyTSI.h
 *
 *  Created by Thant on July 15, 2013.
 *  Copyright 2013 TeleNav. All rights reserved.
 *
 */

#ifndef TN_PROTOBUF_TO_TRAFFIC_DATA_PROXY_TSI_H
#define TN_PROTOBUF_TO_TRAFFIC_DATA_PROXY_TSI_H

#include <foundation/SharedPtr.h>
#include <foundation/WeakPtr.h>

#include <foundation/Util/TnPruningMap.h>

#include <RenderEngine/ITnMapTrafficProxy.h>

#include <GenericProxy/ITnTrafficProtoBufProxy.h>
#include <GenericProxy/TnTrafficID2LatLon.h>
#include <GenericProxy/TnTrafficSpeedIcon.h>


class TnProtoBufToTrafficDataProxyTSI
    : public ITnMapTrafficProxy,
      public boost::enable_shared_from_this<TnProtoBufToTrafficDataProxyTSI>
{
public:

    typedef Tn::Foundation::shared_ptr<TnProtoBufToTrafficDataProxyTSI> ptr;
    typedef Tn::Foundation::weak_ptr<TnProtoBufToTrafficDataProxyTSI> wptr;


    // To prevent creating an instance on the stack given
    // enable_shared_from_this.
    static TnProtoBufToTrafficDataProxyTSI::ptr Create(ITnTrafficProtoBufProxy::ptr const &,
                                                       TnTrafficID2LatLon::ptr const &,
                                                       eTnSpeedUnits defaultUnits);

    virtual bool PrefetchSupported() const;
    virtual void RequestTilePrefetch(TnMapTileId const & id);

    virtual void RequestTile(TnMapTrafficData::ptr const & data);

    virtual void CancelRequest(TnMapTrafficData::ptr const & data);

    void setUnits(eTnSpeedUnits);
    eTnSpeedUnits getUnits();

    /*  Traffic speed icons are only created for traffic segments with
     *  free-flow speeds greater than what is set here.
     *
     *  The threshold is set in either miles per hour or kilometers
     *  per hour depending on what the current setting of units is.
     */

    void setTSIFreeFlowThreshold(double threshold);

    /*  Maximum number of traffic speed icons to create per
     *  tile. Defaults to one.
     */

    void setTSICount(uint32_t tsi_count);
    
    virtual void SetTrafficConditions(const std::vector<std::string> & tmcIds, const std::vector<std::string> & conditions);

private:

    TnProtoBufToTrafficDataProxyTSI(ITnTrafficProtoBufProxy::ptr const &,
                                    TnTrafficID2LatLon::ptr const &,
                                    eTnSpeedUnits defaultUnits);

    ITnTrafficProtoBufProxy::ptr m_protoBufProxy;

    typedef TnPruningMap<TnMapTrafficData::ptr, ITnTrafficProtoBufData::wptr> PendingRequests;
    PendingRequests m_pendingRequests;
    TnTrafficID2LatLon::ptr m_tid2LatLon;
    eTnSpeedUnits m_units;
    double m_tsi_threshold;          // In meters per second.
    uint32_t m_tsi_count;
    
    Tn::Foundation::mutex m_mutex;
    std::vector<std::string> m_tmcIds;
    std::vector<std::string> m_tcConditions;
};



#endif // TN_PROTOBUF_TO_TRAFFIC_DATA_PROXY_TSI_H
