#ifndef SERIAL_MESSAGE_H
#define SERIAL_MESSAGE_H

#include <Vector.h>
#include <ArduinoLog.h>

#include "types.h"
#include "Component.h"

class CommandMessage;
/**
 * @class SerialMessage
 * @brief Represents a serial message component.
 * 
 * The SerialMessage class is a component that handles serial communication.
 * It provides methods for reading, parsing, and handling command messages.
 */
class SerialMessage : public Component
{
public:
    
    /**
     * @brief Constructs a SerialMessage object.
     * 
     * @param _stream The serial stream used for communication.
     * @param _owner The owner component of this SerialMessage.
     */
    SerialMessage(Stream &_stream, Component *_owner) : Component("SerialMessage", 0, Component::COMPONENT_DEFAULT, (Component *)_owner),
                                                        stream(_stream)
    {
        setFlag(OBJECT_RUN_FLAGS::E_OF_DEBUG);
        lastRead = 0;
    }

    /**
     * @brief Reads a command message from the serial stream.
     * 
     * @return A pointer to the CommandMessage object read from the stream.
     */
    CommandMessage *read();

    /**
     * @brief Parses a string into a CommandMessage object.
     * 
     * @param string The string to be parsed.
     * @return A pointer to the parsed CommandMessage object.
     */
    CommandMessage *parse(char *string);

    /**
     * @brief Executes the main loop of the SerialMessage component.
     * 
     * @return A status code indicating the result of the loop execution.
     */
    short loop();

    /**
     * @brief Performs the setup operations for the SerialMessage component.
     * 
     * @return A status code indicating the result of the setup.
     */
    short setup();

    /**
     * @brief Performs debug operations for the SerialMessage component.
     * @return A status code indicating the result of the debug operation.
     */
    short debug();

private:
    // Private members and methods
    Vector<CommandMessage *> messages;
    millis_t lastRead;

protected:
    Stream &stream;
    CommandMessage *msg;
    String readStringFromSerial();
};

#endif