/*
 * daap_client.h: The daap client (wrapper to the opendaap library)
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: daap_client.h,v 1.36 2005/10/31 14:29:13 cm Exp $
 */

#ifndef __DAAPCLIENT_H
#define __DAAPCLIENT_H

#include <daap/client.h>
#include "audio_playlist.h"
#include "daap_database.h"

typedef enum {
  pInstant = 0,
  pFirst = 1
} ePlaylist;

class cDaapClient;
class cDaapHost;
class cDaapDatabase;
class cDaapPlaylist;

class cDaapPlaylistItem : public cAudioTune {
  private:
    int m_songIndex;
    int m_songId;
    cDaapPlaylist *mpo_parent;
    void Initialize(cDaapPlaylist *parent, int songid);
  public:
    cDaapPlaylistItem(cDaapPlaylist *parent, const DAAP_ClientHost_DatabasePlaylistItem *item);
    cDaapPlaylistItem(cDaapPlaylist *parent, int songid);
    ~cDaapPlaylistItem();
    int GetSongId(void) const { return m_songId; };
    int SetSongId(int songid);
    cDaapPlaylist *Playlist(void) const { return mpo_parent; };
    const cMediaDatabaseBlob *GetData(void) const;
    int GetChannels(void) const;
    int GetSampleRate(void) const;
    int GetTime(void) const;
    short GetBitRate(void) const;
    const char *GetFormat(void) const;
};

class cDaapPlaylist : public cAudioPlaylist {
  private:
    bool m_writeable;
    bool m_initialized;
    char *mp_name;
    int m_id;
    cDynamicArray<cDaapPlaylistItem *> mo_items;
    cDaapDatabase *mpo_parent;
    DAAP_SClientHost *mp_scHost;
    void Load(void);
  public:
    cDaapPlaylist(cDaapDatabase *parent, DAAP_SClientHost *host, const char *name, int id, bool writeable = false);
    ~cDaapPlaylist();
    const char *GetName(void) const { return mp_name; };
    int GetId(void) const { return m_id; };
    int Count(void);
    bool isWriteable(void) const { return m_writeable; };
    cDaapPlaylistItem *Current(void);
    int SetCurrent(int current);
    bool Lock(void);
    bool Unlock(void);
    cDaapPlaylistItem *Item(unsigned int index);
    cDaapPlaylistItem *Add(cDaapPlaylistItem *item);
    int Delete(int index);
    int Move(int dest, int src);
    bool Clear(void);
    cDaapDatabase *Database(void) const { return mpo_parent; };
};

class cDaapPlaylists {
  private:
    cDynamicArray<cDaapPlaylist *> mo_pls;
    cDaapDatabase *mpo_parent;
    DAAP_SClientHost *mp_scHost;
    void Load(void);
  public:
    cDaapPlaylists(cDaapDatabase *parent, DAAP_SClientHost *host);
    ~cDaapPlaylists();
    int Count(void) const { return mo_pls.Count(); };
    cDaapPlaylist *Item(unsigned int index) const { return mo_pls.Item(index); };
    cDaapDatabase *Database(void) const { return mpo_parent; };
};

class cDaapDatabaseItem : public cMediaDatabaseItem {
  private:
    int m_id;
    char *mp_itemName;
    char *mp_songAlbum;
    char *mp_songArtist;
    short m_songBeatsPerMinute;
    short m_songBitRate;
    short m_songDiscCount;
    short m_songDiscNumber;
    char *mp_songGenre;
    int m_songSampleRate;
    int m_songSize;
    int m_songTime;
    short m_songTrackCount;
    short m_songTrackNumber;
    char m_songUserRating;
    short m_songYear;
    char *mp_songFormat;
    DAAP_SClientHost *mp_scHost;
    cDaapDatabase *mpo_parent;
  public:
    cDaapDatabaseItem(cDaapDatabase *parent, DAAP_SClientHost *host, const DAAP_ClientHost_DatabaseItem *item);
    ~cDaapDatabaseItem();
    int GetId(void) const { return m_id; };
    const char *GetValue(eDatabaseField field) const;
    const char *GetName(void) const { return mp_itemName; };
    const char *GetSongAlbum(void) const { return mp_songAlbum; };
    const char *GetSongArtist(void) const { return mp_songArtist; };
    short GetSongBeatsPerMinute(void) const { return m_songBeatsPerMinute; };
    short GetSongBitRate(void) const { return m_songBitRate; };
    short GetSongDiscount(void) const { return m_songDiscCount; };
    short GetSongDiscNumber(void) const { return m_songDiscNumber; };
    const char *GetSongGenre(void) const { return mp_songGenre; };
    int GetSongSampleRate(void) const { return m_songSampleRate; };
    int GetSongSize(void) const { return m_songSize; };
    int GetSongTime(void) const { return m_songTime; };
    short GetSongTrackCount(void) const { return m_songTrackCount; };
    short GetSongTrackNumber(void) const { return m_songTrackNumber; };
    char GetSongUserRating(void) const { return m_songUserRating; };
    short GetSongYear(void) const { return m_songYear; };
    const char *GetSongFormat(void) const { return mp_songFormat; };
    const cMediaDatabaseBlob *GetData(void) const;
    cDaapDatabase *Database(void) const { return mpo_parent; };
};

class cDaapDatabase {
  private:
    bool m_copy;
    bool m_initialized;
    char *mp_name;
    int m_id;
    DAAP_SClientHost *mp_scHost;
    cMediaDatabaseEngine *mpo_items;
    cDaapPlaylists *mpo_playlists;
    cDaapHost *mpo_parent;
    void Load(void);
  public:
    cDaapDatabase(cDaapHost *parent, DAAP_SClientHost *host, const char *name, int id, cMediaDatabaseEngine *dbe = NULL, cDaapPlaylists *pls = NULL);
    ~cDaapDatabase();
    const char *GetName(void) const { return mp_name; };
    int GetId(void) const { return m_id; };
    bool isCopy(void) const { return m_copy; };
    int Count(void);
    cDaapDatabaseItem *Item(unsigned int index);
    cDaapPlaylists *Playlists(void) const { return mpo_playlists; };
    cDaapHost *Host(void) const { return mpo_parent; };
    cDaapDatabase *Sort(eDatabaseField field);
    cDaapDatabase *Filter(eDatabaseField field, const char *key);
    int FindIndexByChar(eDatabaseField field, char key, bool caseSensitive);
    int FindIndexById(int key);
};

class cDaapDatabases {
  private:
    cDynamicArray<cDaapDatabase *> mo_dbs;
    cDaapHost *mpo_parent;
    DAAP_SClientHost *mp_scHost;
    void Load(void);
  public:
    cDaapDatabases(cDaapHost *parent, DAAP_SClientHost *host);
    ~cDaapDatabases();
    int Count(void) const { return mo_dbs.Count(); };
    cDaapDatabase *Item(unsigned int index) const { return mo_dbs.Item(index); };
    cDaapHost *Host(void) const { return mpo_parent; };
};

class cDaapHost {
  private:
    bool m_running;
    char m_share[129];
    char m_password[129];
    DAAP_SClientHost *mp_scHost;
    cDaapDatabases *mpo_dbs;
    cDaapClient *mpo_parent;
    void Initialize(void);
  public:
    cDaapHost(cDaapClient *parent, DAAP_SClientHost *host);
    ~cDaapHost();
    const char *GetShare(void) const { return m_share; };
    const char *GetPassword(void) const { return m_password; };
    const char *SetPassword(char *pwd);
    bool isRunning(void) const { return m_running; };
    bool SetRunning(bool set);
    cDaapDatabases *Databases(void);
    cDaapClient *Client(void) const { return mpo_parent; };
};

class cDaapHosts {
  private:
    cDynamicArray<cDaapHost *> mo_hosts;
    cDaapClient *mpo_parent;
  public:
    cDaapHosts(cDaapClient *parent);
    ~cDaapHosts();
    void Enumerate(DAAP_SClient *daap);
    cDaapHost *Item(int index) const { return mo_hosts.Item(index); };
    cDaapHost *Add(cDaapHost *host);
    void Delete(cDaapHost *host);
    int Count(void) const { return mo_hosts.Count(); };
    cDaapClient *Client(void) const { return mpo_parent; };
};

class cDaapClient {
  private:
    DAAP_SClient *mp_daap;
    cDaapHosts mo_hosts;
  public:
    cDaapClient();
    ~cDaapClient();
    int Create(void);
    int Destroy(void);
    cDaapHosts *Hosts(void) { return &mo_hosts; };
};

#endif
