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

#ifndef KMLLOGWRITER_H_
#define KMLLOGWRITER_H_

#include "LogWriter.h"
#include "data/Location.h"
#include "logger/Syslog.h"
#include "utils/FlexArray.h"

class OutStream;

/**
 *  Defines a LogWriter that can convert Location and depth triples into
 *  write kml format text (see http://en.wikipedia.org/wiki/Kml )
 *
 *  See FileLogWriter for output to the filesystem.
 *
 *  \ingroup logging
 */
class KmlLogWriter : public LogWriter
{
public:
    /// Constructor.
    KmlLogWriter( Str vehicleName, bool emitTimestamps = true, bool doDepth = true );

    /// Destructor.
    virtual ~KmlLogWriter();

    /// Write an entry to the text log
    virtual unsigned int write( const LogEntry *entry, const Unit* unit = NULL );

    virtual unsigned int writeHeader();

    virtual unsigned int writeFooter();

    void setColor( const char* color )
    {
        color_ = color;
    }

    static char** GetKmlArgs( bool doKmlSimple = false )
    {
        return ( char** )KmlArgs_ + ( doKmlSimple ? 1 : 0 );
    }

    static char** GetKmlUnits( bool doKmlSimple = false )
    {
        return ( char** )KmlUnits_ + ( doKmlSimple ? 1 : 0 );
    }

    static float* GetKmlErrors( bool doKmlSimple = false )
    {
        return ( float* )KmlErrors_ + ( doKmlSimple ? 1 : 0 );
    }
protected:

    Str vehicleName_;

    class SyslogItem
    {
    public:
        Timestamp timestamp_;
        Str coordinates_;
        Syslog::Severity severity_;
        Str message_;
        SyslogItem( const Timestamp& timestamp, const char* coordinates,
                    const Syslog::Severity severity, const Str& message )
            : timestamp_( timestamp ),
              coordinates_( coordinates ),
              severity_( severity ),
              message_( message )
        {}
    };

    FlexArray<SyslogItem*> syslogItems_;

    unsigned int writeSyslogItems( const char* latestCoordinates );

private:

    bool emitTimestamps_;
    bool doDepth_;
    bool haveFirstLocation_;
    bool haveFirstDepth_;
    bool haveNewLatitude_;
    bool haveNewLongitude_;
    bool haveNewDepth_;
    bool started_;

    Timestamp lastTimestamp_;

    Timestamp currentTimestamp_;

    unsigned int lineCount_;

    Location location_;
    double lastLatitude_;
    double lastLongitude_;

    double depth_;

    const char* color_;

    static const char* KmlArgs_[3];

    static const char* KmlUnits_[3];

    static float KmlErrors_[3];

    static const Location ZERO_LOCATION;

};

#endif /*KMLLOGWRITER_H_*/
