/*
 * daap_database.h: The daap database interface
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: daap_database.h,v 1.11 2005/10/27 10:03:53 cm Exp $
 */

#ifndef __DAAPDATABASE_H
#define __DAAPDATABASE_H

#include <stdint.h>

typedef enum {
  dfArtist,
  dfAlbum,
  dfGenre,
  dfId,
  dfName,
  dfNone
} eDatabaseField;

template <typename ItemType>
class cDynamicArray {
  private:
    bool m_ownership;
    int m_itemBufferSize;
    bool Resize(void);
  protected:
    int m_numOfItems;
    ItemType *mp_items;
  public:
    // when ownership is false, then no object's will be delete'd/free'd when necessaray
    cDynamicArray(bool ownership = true);
    virtual ~cDynamicArray();
    bool hasOwnership() const { return m_ownership; };
    int Count(void) const { return m_numOfItems; };
    ItemType Item(int index) const;
    ItemType Add(ItemType item);
    int Delete(int index);
    int Move(int dest, int src);
    int Contains(ItemType item) const;
    bool Clear(void);
    void Pack(void);
};

class cMediaDatabaseBlob {
  private:
    const uint8_t *mp_data;
    unsigned long m_length;
  public:
    cMediaDatabaseBlob(const uint8_t *data, unsigned long length);
    ~cMediaDatabaseBlob();
    const uint8_t *Get(void) const { return mp_data; };
    unsigned long GetLength(void) const { return m_length; };
};

class cMediaDatabaseGroup {
  private:
    int m_first;
    int m_last;
  public:
    cMediaDatabaseGroup(int first = 0, int last = 0);
    ~cMediaDatabaseGroup();
    int GetFirst(void) const { return m_first; };
    int GetLast(void) const { return m_last; };
};

class cMediaDatabaseItem {
  public:
    cMediaDatabaseItem();
    virtual ~cMediaDatabaseItem();
    virtual const char *GetValue(eDatabaseField field) const = 0;
    virtual int GetId(void) const = 0;
};

/*

class cMediaDatabase {
  public:
    cMediaDatabase();
    virtual ~cMediaDatabase();
};

*/

class cMediaDatabaseEngine : public cDynamicArray<cMediaDatabaseItem *> {
  private:
    eDatabaseField m_sortation;
    cDynamicArray<cMediaDatabaseGroup *> mo_groups;
    void SetGroups(eDatabaseField field);
  public:
    cMediaDatabaseEngine(bool ownership = true);
    virtual ~cMediaDatabaseEngine();
    void Sort(eDatabaseField field);
    int BinSearch(eDatabaseField field, const char *key, bool caseSensitive = false);
    int BinSearch(eDatabaseField field, char key, bool caseSensitive = false);
    int BinSearch(eDatabaseField field, int key);
};

#endif
