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

#ifndef SYSLOGCOMPONENT_H_
#define SYSLOGCOMPONENT_H_

#include "../logger/Service.h"
#include "component/Behavior.h"
#include "logger/Syslog.h"
#include "utils/FlexArray.h"

class DataReader;
class MissionItem;
class MissionNode;
class Module;
class SendDestination;

/**
 *  Behavior that is embedded in scripted code within a mission file.
 *
 *  \ingroup missionScript
 */
class SyslogComponent: public Behavior
{
public:
    static SyslogComponent* Instance( MissionNode* node, const Str& itemName, MissionItem* parent, const Module* module, Logger& logger );
    virtual ~SyslogComponent();
    virtual void initialize();
    virtual void run();
    virtual bool runIfUnsatisfied();
    virtual void uninitialize();

protected:

    /// Protected constructor
    SyslogComponent( const Str& name, Syslog::Severity severity, Service::ServiceType serviceType, SendDestination* destination );

    enum SyslogPartType
    {
        SYSLOG_PART_TYPE_STR,
        SYSLOG_PART_TYPE_DATAREADER
    };

    class SyslogPart
    {
    public:
        SyslogPart( DataReader* dataReaderPart, const Unit* unit )
            : syslogPartType_( SYSLOG_PART_TYPE_DATAREADER ),
              strPart_( NULL ),
              dataReaderPart_( dataReaderPart ),
              unit_( unit )
        {};
        SyslogPart( Str* strPart )
            : syslogPartType_( SYSLOG_PART_TYPE_STR ),
              strPart_( strPart ),
              dataReaderPart_( NULL ),
              unit_( NULL )
        {};
        ~SyslogPart()
        {
            if( NULL != strPart_ )
            {
                delete strPart_;
            }
            //if ( NULL != dataReaderPart_ )
            //{
            //    delete dataReaderPart_;
            //}
        }
        SyslogPartType getSyslogPartType()
        {
            return syslogPartType_;
        }
        Str* getStrPart()
        {
            return strPart_;
        }
        DataReader* getDataReaderPart()
        {
            return dataReaderPart_;
        }
        const Unit* getUnit()
        {
            return unit_;
        }
    private:
        // Note that the copy constructor below is private and not given a body.
        // Any attempt to call it will return a compiler error.
        SyslogPart( const SyslogPart& old ); // disallow copy constructor

        SyslogPartType syslogPartType_;
        Str* strPart_;
        DataReader* dataReaderPart_;
        const Unit* unit_;
    };

    void addPart( SyslogPart* syslogPart );

    /// The contents of the syslog entry;
    FlexArray<SyslogPart*> syslogParts_;

    Syslog::Severity severity_;

    Service::ServiceType serviceType_;

    SendDestination* destination_;

private:
    // Note that the copy constructor below is private and not given a body.
    // Any attempt to call it will return a compiler error.
    SyslogComponent( const SyslogComponent& old ); // disallow copy constructor
};

#endif /* SYSLOGCOMPONENT_H_ */
