/** \file
 *
 *  Contains the LcmMsgWriter class definition.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#ifndef LCMMSGWRITER_H_
#define LCMMSGWRITER_H_

#include "LcmMessageWriter.h"
#include "data/DataElement.h"
#include "utils/FastMap.h"


/**
 * LcmMsgWriter defines a set of tools needed to copy and publish LRAUV data on
 * a designated channel using the Tethys LCM API.
 *
 *  \ingroup data
 */
class LcmMsgWriter
{
private:
    /// use URI codes as keys for LcmMessageWriter arrays
    typedef lrauv_lcm_tools::LcmMessageWriter<unsigned short> LcmMsg;

    /// Holds typed scalar data
    typedef union
    {
        int ival;
        float fval;
        double dval;
        unsigned char bval;
    } ScalarVal;

public:

    LcmMsgWriter( const Str& channelName );

    ~LcmMsgWriter();

    /// Creates a LrauvLcmMessage data array entry for a DataElement
    bool add( const DataElement& dataElement );

    /// Populates the LCM message array with data
    bool setValue( const DataElement& dataElement );

    /// Handles the data assignment from a DataValue to the keyed LrauvLcmMessage array
    bool set( unsigned short key, const DataValue& dataVal );

    /// Publishes the component's LCM messages
    bool publish( void );

    /// Returns a refrence to the channel's LcmMessageWriter
    const LcmMsg& getLcmMsgWriter( void )
    {
        return msg_;
    }

protected:

    /// Matches DataElement data type to LcmMessageWriter's DataType
    lrauv_lcm_tools::DataType mapElementType( const DataElement& element );

    /// Matches the lrauv-app's BinaryDataType to LcmMessageWriter's DataType
    lrauv_lcm_tools::DataType mapDataType( const BinaryDataType& dataType );

    /// Matches the lrauv-app's BlobType to LcmMessageWriter's DataType
    lrauv_lcm_tools::DataType mapBlobDataType( const BlobType& blobType );

    /// Matches ElementURI dimensions to LcmMessageWriter's dimensions
    void mapDimensions( const ElementURI& uri, lrauv_lcm_tools::Dim& dim );

    /// Parses the variable data name from a URI string
    const char* uriName( Str uri );

    /// Holds the channel's timestamp
    Timestamp timestamp_;

    /// Holds the channel's name
    const Str channelName_;

    /// Holds the channel's lrauv_lcm_tools::LcmMessageWriter
    LcmMsg msg_;

};

#endif /* LCMMSGWRITER_H_ */
