#include "AvReceiverInterface.h"
#include "ProtocolDefs.h"
#include "AVReceiverDevice.h"
#include <QDataStream>

CAvReceiverInterface::CAvReceiverInterface(int Address, CAbstractNetworkCommIF *NetworkInterface , CAVReceiverDevice *DevicePtr):
     CNetworkDevice(ID_AVRECEIVER_INTERFACE,Address,NetworkInterface)
{
    mAvReceiverDevice = DevicePtr;
}

int CAvReceiverInterface::NewDeviceFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data)
{
    Q_UNUSED(DeviceID)
    Q_UNUSED(DeviceAddress)
    Q_UNUSED(DataSize)
    Q_UNUSED(Data)

      switch(MessageID)
    {
          case AV_RECEIVER_INTERFACE_ACK:
          {
              break;
          }
          case AV_RECEIVER_INTERFACE_GENERAL_STATUS_REQUEST:
          {
              QByteArray data = mAvReceiverDevice->GetReceiverStatus();
              mNetworkInterfacePtr->SendNetworkMessage(ID_AVRECEIVER_INTERFACE,mDeviceAddress,AV_RECEIVER_INTERFACE_GENERAL_STATUS_RESPONSE,data.size(),&data);
              break;
          }

          case AV_RECEIVER_INTERFACE_SET_MAIN_POWER_REQUEST:
          {
              break;
          }
          case AV_RECEIVER_INTERFACE_SET_ZONE2_REQUEST:
          {
              bool SpkStatus = (bool)Data[0];
              mAvReceiverDevice->SetZone2(SpkStatus);
              break;
          }
          case AV_RECEIVER_INTERFACE_SET_MAIN_ZONE_REQUEST:
          {
              bool SpkStatus = (bool)Data[0];
              mAvReceiverDevice->SetMainZone(SpkStatus);
              break;

          }
          case AV_RECEIVER_INTERFACE_SET_SPEAKERS_REQUEST:
          {
                break;
          }
          case AV_RECEIVER_INTERFACE_SEND_DIRECT_CMD_REQUEST:
          {
              break;
          }
          case AV_RECEIVER_INTERFACE_SELECT_SCENE_REQUEST:
          {
              mAvReceiverDevice->SelectScene(Data[0],Data[1]);
              break;
          }
          case AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_REQUEST:
          {
              float VolumeData;
              QDataStream Strm(&Data,QIODevice::ReadOnly);
              Strm >> VolumeData;

              mAvReceiverDevice->SetMainVolume(VolumeData);
              break;
          }
          case AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_REQUEST:
          {
              float VolumeData;
              QDataStream Strm(&Data,QIODevice::ReadOnly);
              Strm >> VolumeData;

              mAvReceiverDevice->SetZone2Volume(VolumeData);
              break;
          }
          case AV_RECEIVER_INTERFACE_SET_ZONE2_INPUT_REQUEST:
          {
              QString Source;
              QDataStream Strm(&Data,QIODevice::ReadOnly);
              Strm >> Source;

              mAvReceiverDevice->SetZone2Input(Source);
              break;
          }
          case  AV_RECEIVER_INTERFACE_SET_SYNC_Z2_WITH_Z1_REQUEST:
          {
              bool Sync;
              QDataStream Strm(&Data,QIODevice::ReadOnly);
              Strm >> Sync;

              mAvReceiverDevice->SetSyncZ2withZ1(Sync);
              break;
          }
          case AV_RECEIVER_INTERFACE_GENERAL_STATUS_RESPONSE:
          case AV_RECEIVER_INTERFACE_SET_MAIN_POWER_RESPONSE:
          case AV_RECEIVER_INTERFACE_SET_ZONE2_RESPONSE:
          case AV_RECEIVER_INTERFACE_SEND_DIRECT_CMD_RESPONSE:
          case AV_RECEIVER_INTERFACE_SET_SPEAKERS_RESPONSE:
          case AV_RECEIVER_INTERFACE_SET_MAIN_ZONE_RESPONSE:
          case AV_RECEIVER_INTERFACE_SELECT_SCENE_RESPONSE:
          case AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_RESPONSE:
          case AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_RESPONSE:
          case AV_RECEIVER_INTERFACE_SET_ZONE2_INPUT_RESPONSE:
          case AV_RECEIVER_INTERFACE_SET_SYNC_Z2_WITH_Z1_RESPONSE:
          default:
          {
              qDebug("Received invalid msg from Sprinkler Interface");
              break;
          }
      }

    return 0;

}
