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

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

int CAvReceiverConsoleInterface::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->GetReceiverStatusForConsole();
              mNetworkInterfacePtr->SendNetworkMessage(ID_AVRECEIVER_CONSOLE_INTERFACE,mDeviceAddress,AV_RECEIVER_INTERFACE_GENERAL_STATUS_RESPONSE,data.size(),&data);
              break;
          }

          case AV_RECEIVER_INTERFACE_SET_MAIN_POWER_REQUEST:
          {
              bool SpkStatus = (bool)Data[0];
              mAvReceiverDevice->SetMainZone(SpkStatus);
              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:
          {
              char VolumeData;
              VolumeData = Data[0];
              float Volume = (VolumeData/2) - 80.5;

              mAvReceiverDevice->SetMainVolume(Volume);
              break;
          }
          case AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_REQUEST:
          {
              char VolumeData;
              VolumeData = Data[0];
              float Volume = (VolumeData/2) - 80.5;

              qDebug("Volume 2 %d, %f",VolumeData, Volume);
              mAvReceiverDevice->SetZone2Volume(Volume);
              break;
          }
          case AV_RECEIVER_INTERFACE_SET_ZONE2_INPUT_REQUEST:
          {
              QString Source(Data);

              mAvReceiverDevice->SetZone2Input(Source);
              break;
          }
          case  AV_RECEIVER_INTERFACE_SET_SYNC_Z2_WITH_Z1_REQUEST:
          {
              bool Sync = (bool)Data[0];

              mAvReceiverDevice->SetSyncZ2withZ1(Sync);
              break;
          }
          case  AV_RECEIVER_INTERFACE_INCREMENT_MAIN_VOLUME_REQUEST:
          {
              bool Increment = (bool)Data[0];

              mAvReceiverDevice->IncrementMainVolume(Increment);
              break;
          }
          case  AV_RECEIVER_INTERFACE_INCREMENT_Z2_VOLUME_REQUEST:
          {
              bool Increment = (bool)Data[0];

              mAvReceiverDevice->IncrementZ2Volume(Increment);
              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;

}
