#ifndef AVRECEIVERDEVICE_H
#define AVRECEIVERDEVICE_H

#include <QTcpSocket>
#include <QHash>
#include "NetworkDevice.h"
#include <QString>
#include <QTimer>
#include "AvReceiverData.h"

#define RECEIVER_PORT 50000
#define RECEIVER_IP_ADDRESS "192.168.50.101"
#define RECEIVER_STATE_UPDATE_TIMEOUT 3000
#define RECEIVER_MINIMUM_VOLUME -80.5
#define RECEIVER_MAXIMUM_VOLUME 16.5
#define RECEIVER_VOLUME_INCREMENT_STEP 0.5

enum eReceiverSpkStatus
{
    RECEIVER_OFF_SPK_STATUS,
    RECEIVER_A_ON_SPK_STATUS,
    RECEIVER_B_ON_SPK_STATUS,
    RECEIVER_A_B_ON_SPK_STATUS,
    RECEIVER_UNKNOWN_SPK_STATUS
};

enum eSpkSetStateCmds
{
    RECEIVER_SET_SPK_OFF,
    RECEIVER_SET_SPK_ON,
    RECEVIVER_TOGGLE_SPK,
    RECEIVER_DONT_CHANGE_SPK
};

enum eReceiverSubUnits
{
    SYS,
    MAIN,
    TUN,
    SIRIUS,
    IPOD,
    BT,
    UAW,
    RHAP,
    SIRIUSIR,
    PANDORA,
    NAPSTER,
    PC,
    NETRADIO,
    USB,
    IPODUSB
};

enum eReceiverZone2Inputs
{
    AUDIO1,
    AUDIO2,
    AUDIO3,
    AUDIO4,
    AUDIO5,
    PHONO,
    TUNER,
    SERVER,
    MAIN_ZONE_SYNC
};

class CAVReceiverDevice :public QObject
{
    Q_OBJECT

public:
    CAVReceiverDevice();
    ~CAVReceiverDevice();
    int Start();


private:
    int ConnectToReceiver();
    int DisconnectReceiver();
    int DisconnectReceiverDelayed(int Delay = 500);
    int SendReceiverCommand(QString Command);
    int UpdateReceiverStateParam(QString SubUnit, QString Fcn, QString Param);

public:
    //Network Interface Implementation
    int SetZone2(bool OnOff);
    int SetMainZone(bool OnOff);
    int SetSpeakers(int SpeakerA, int SpeakerB);
    int SendRawCommand(QString Cmd);
    int SelectScene(char Zone, char Scene);
    int SetMainVolume(float Volume);
    int SetZone2Volume(float Volume);
    int SetZone2Input(QString InputString);
    int SetSyncZ2withZ1(bool Sync);
    int IncrementMainVolume(bool Increment);
    int IncrementZ2Volume(bool Increment);
    QByteArray GetReceiverStatus();
    QByteArray GetReceiverStatusForConsole();

private:
    QTcpSocket *mReceiverSocket;
    int AnalyseRxData(QByteArray data);
    QTimer *mDisconnectTimer, *mStateRequestTimer, *mSyncDelayTimer;
    CAvReceiverMainStatus mReceiverStatus;
    CAvReceiverMainStatus mZone2Status;

    bool mIsConnected;
    QString mPendingCommand;
    float mLastMainZoneVolume;


public slots:
    void SocketConnected();
    void SocketDisconnected();
    void SocketRX();
    void DisconnectTimerExpired();
    void StateRequestTimerExpired();
   // void SyncTimerExpired();
};

#endif // AVRECEIVERDEVICE_H
