#pragma once

/**************************************************************************************************
IAPETag.h - define the APE tag interfaces so APETag.h doesn't need to get included.
**************************************************************************************************/
namespace APE
{

/**************************************************************************************************
ID3 v1.1 tag
**************************************************************************************************/
#define ID3_TAG_BYTES 128 // equal to sizeof(ID3_TAG)
#pragma pack(push, 1)
struct ID3_TAG
{
    char Header[3];             // should equal 'TAG'
    char Title[30];             // title
    char Artist[30];            // artist
    char Album[30];             // album
    char Year[4];               // year
    char Comment[29];           // comment
    unsigned char Track;        // track
    unsigned char Genre;        // genre
};
#pragma pack(pop)

/**************************************************************************************************
IAPETagField interface
**************************************************************************************************/
class IAPETagField
{
public:
    // destruction
    virtual ~IAPETagField() {}

    // gets the size of the entire field in bytes (name, value, and metadata)
    virtual int GetFieldSize() const = 0;

    // get the name of the field
    virtual const str_utfn * GetFieldName() const = 0;

    // get the value of the field
    virtual const char * GetFieldValue() = 0;

    // get the size of the value (in bytes)
    virtual int GetFieldValueSize() const = 0;

    // get any special flags
    virtual int GetFieldFlags() const = 0;

    // checks to see if the field is read-only or UTF-8
    virtual bool GetIsReadOnly() const = 0;
    virtual bool GetIsUTF8Text() const = 0;
};

/**************************************************************************************************
IAPETag interface
**************************************************************************************************/
class IAPETag
{
public:
    virtual ~IAPETag() {}

    // save the tag to the I/O source (bUseOldID3 forces it to save as an ID3v1.1 tag instead of an APE tag)
    virtual int Save(bool bUseOldID3 = false) = 0;

    // removes any tags from the file (bUpdate determines whether is should re-analyze after removing the tag)
    virtual int Remove(bool bUpdate = true) = 0;

    // sets the value of a field (use nFieldBytes = -1 for null terminated strings)
    // note: using NULL or "" for a string type will remove the field
    virtual int SetFieldString(const str_utfn * pFieldName, const str_utfn * pFieldValue, const str_utfn * pListDelimiter = APE_NULL) = 0;
    virtual int SetFieldString(const str_utfn * pFieldName, const char * pFieldValue, bool bAlreadyUTF8Encoded, const str_utfn * pListDelimiter = APE_NULL) = 0;
    virtual int SetFieldBinary(const str_utfn * pFieldName, const void * pFieldValue, intn nFieldBytes, int nFieldFlags) = 0;

    // gets the value of a field (returns -1 and an empty buffer if the field doesn't exist)
    virtual int GetFieldBinary(const str_utfn * pFieldName, void * pBuffer, int * pBufferBytes) = 0;
    virtual int GetFieldString(const str_utfn * pFieldName, str_utfn * pBuffer, int * pBufferCharacters, const str_utfn * pListDelimiter = L"; ") = 0;
    virtual int GetFieldString(const str_utfn * pFieldName, str_ansi * pBuffer, int * pBufferCharacters, bool bUTF8Encode = false) = 0;
    virtual int GetFieldNumber(const str_utfn * pFieldName, int nNotFoundResult = -1) = 0;

    // remove a specific field
    virtual int RemoveField(const str_utfn * pFieldName) = 0;
    virtual int RemoveField(int nIndex) = 0;

    // clear all the fields
    virtual int ClearFields() = 0;

    // see if we've been analyzed (we do lazy analysis)
    virtual bool GetAnalyzed() = 0;

    // get the total tag bytes in the file from the last analyze
    // need to call Save() then Analyze() to update any changes
    virtual int GetTagBytes() = 0;

    // fills in an ID3_TAG using the current fields (useful for quickly converting the tag)
    virtual int CreateID3Tag(ID3_TAG * pID3Tag) = 0;

    // see whether the file has an ID3 or APE tag
    virtual bool GetHasID3Tag() = 0;
    virtual bool GetHasAPETag() = 0;
    virtual int GetAPETagVersion() = 0;

    // gets a desired tag field (returns NULL if not found)
    // again, be careful, because this a pointer to the actual field in this class
    virtual IAPETagField * GetTagField(const str_utfn * pFieldName) = 0;
    virtual IAPETagField * GetTagField(int nIndex) = 0;

    // options
    virtual void SetIgnoreReadOnly(bool bIgnoreReadOnly) = 0;
};

}
