#pragma once

#include <pulse/pulseaudio.h>
#include "audiodevice.h"

class PulseModel;

class PulseDevice : public AudioDevice
{
    TQ_OBJECT

public:
    PulseDevice( AudioDevice::Category cat, uint32_t paIndex, TQObject *parent = 0 );
    ~PulseDevice();

    TQString     name()     const { return m_name; }
    TQString     iconName() const { return m_iconName; }
    Category    category() const { return m_category; }
    int         volume()          const { return m_volume; }
    bool        muted()           const { return m_muted; }
    int         pan()             const { return m_pan; }
    bool        recordingActive() const { return m_recordingCount > 0; }
    uint32_t    paIndex()     const { return m_paIndex; }
    uint32_t    sinkIndex()   const { return m_sinkIndex; }
    TQString     monitorName() const { return m_monitorName; }
    TQString     paName()      const { return m_paName; }

    void setPaName( const TQString &n )    { m_paName    = n; }
    void setSinkIndex( uint32_t idx )      { m_sinkIndex = idx; }
    void setModel( PulseModel *m )         { m_model = m; }
    void adjustRecordingCount( int delta );

    void setVolume( int v );
    void setMuted( bool m );
    void setPan( int p );

    void update( const TQString &name, int volume, bool muted,
                 uint8_t channels, int pan,
                 const TQString &monitorName, const TQString &iconName );

    TQWidget *createWidget( TQWidget *parent );

    void setPAContext( pa_context *ctx, pa_threaded_mainloop *mainloop );
    void detach( bool paAlreadyDead = false );
    void pauseMonitoring();
    void resumeMonitoring();

protected:
    void customEvent( TQCustomEvent *e );

private:
    void pushCVolume( const pa_cvolume &cv );
    void startMonitoring();
    static void monitorReadCb( pa_stream *s, size_t nbytes, void *userdata );

    AudioDevice::Category  m_category;
    uint32_t               m_paIndex;
    uint32_t               m_sinkIndex;
    TQString                m_name;
    TQString                m_iconName;
    int                    m_volume;
    bool                   m_muted;
    int                    m_pan;
    uint8_t                m_channels;

    pa_context            *m_context;
    pa_threaded_mainloop  *m_mainloop;

    TQString                m_monitorName;
    pa_stream             *m_monitorStream;
    TQString                m_paName;
    PulseModel            *m_model;
    int                    m_recordingCount;
};
