#ifndef KMSG_H
#define KMSG_H

#include <klog.h>

#include "kmsgdef.h"
#include "ktag.h"

class KMsg : public cKlog
{
public:
    KMsg(uint8_t iMsgID);
    ~KMsg();
    uint8_t addTag(KTag *tag);
    const KTag * getTag();
    const KTag * getCurrentTag() { return (mCurrentTag); }
    const KTag * gotoFirst();
    const KTag * gotoLast();
    uint8_t getMsgSeq() {return (mMsgSeq&0x7F);}
    void setMsgSeq(uint8_t seq) {mMsgSeq = seq&0x7F;}
    bool isLastSegment();
    void setLastSegment(bool s);

    uint16_t getTagCount() {return (mTagCount);}
    uint16_t getTagBytes() { return (mTagBytes); }
    uint16_t getMsgBytes() { return (KMSG_META_LEN + mTagBytes); }
    uint16_t getMsg(void *buf, uint16_t len);
    uint8_t getMsgID() { return (mMsgID); }
    static KMsg * parseMsg(const void *buf, uint16_t len);

    /* Segment this message into smaller messages which will fit
     * into segment size buffers
     */
    void segmentMessage(int seg_size);
    KMsg * getNextSegment();

    /* Reset the segment iterator t the first message */
    void resetSegment();

    /* Add a message segment into the base message */
    void addSegment(KMsg *);

private:
    uint8_t mMsgID;
    uint8_t mMsgSeq;
    uint16_t mTagBytes;
    KTag *mFirstTag;
    KTag *mCurrentTag;
    uint16_t mTagCount;
    KMsg * mSegments;
    KMsg * mCurrentSegment;
    KMsg * mNext;
};

#endif
