
#include "define.h"
#include "AudioConsoleInterface.h"
#include "WiFiCtrl.h"
#include "NetworkProtocol.h"
#include "ProtocolDefs.h"
#include "timer.h"
#include "AudioConsole.h"
#include <stdio.h>
#include "BoardCfg.h"


void AudioConsoleIFExecuteMasterCommand(unsigned char SenderID, unsigned char Command, unsigned char *Data, int DataSize)
{
    
    switch(Command)
    {
        case AV_RECEIVER_INTERFACE_ACK:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_GENERAL_STATUS_RESPONSE:
        {
            ONBOARD_LED2_PIN = ~ONBOARD_LED2_PIN;
            AVReceiverStatus_t MainZone, Zone2;
            
            //            bool MainPwrStatus = false;
            //            bool MainSleepStatus = false;
            //            bool IsMute;
            //            bool DataValid = false;
            //            bool ReceiverOnline = false;
            //            bool SyncZones = false;
            //            float MainVolume = 0.0;
            //            
            //            bool Zone2PwrStatus = false;
            //            bool Zone2SleepStatus = false;
            //            bool Zone2IsMute;
            //            bool Zone2DataValid = false;
            //            bool Zone2ReceiverOnline = false;
            //            bool Zone2SyncZones = false;
            //            float Zone2MainVolume = 0.0;
            
            
            int IntToFloat = 0;
            //Main Zone status
            
            MainZone.MainPwrStatus = Data[0];
            MainZone.MainSleepStatus = Data[1];
            
            MainZone.MainVolume = Data[5];
            MainZone.MainVolume <<= 8;
            MainZone.MainVolume += Data[4];
            MainZone.MainVolume <<= 8;
            MainZone.MainVolume += Data[3];
            MainZone.MainVolume <<= 8;
            MainZone.MainVolume += Data[2];
          //  memcpy(&(MainZone.MainVolume),&IntToFloat,4);
            
            MainZone.IsMute = Data[6];
            MainZone.DataValid = Data[7];
            MainZone.ReceiverOnline = Data[8];
            MainZone.SyncZones = Data[9];
            
            
            //Zone 2 status
            IntToFloat = 0;
            
            Zone2.MainPwrStatus = Data[0];
            Zone2.MainSleepStatus = Data[1];
  
            
            Zone2.MainVolume = Data[2];
            Zone2.MainVolume <<= 8;
            Zone2.MainVolume += Data[3];
            Zone2.MainVolume <<= 8;
            Zone2.MainVolume += Data[4];
            Zone2.MainVolume <<= 8;
            Zone2.MainVolume += Data[5];
            
//            IntToFloat = Data[2];
//            IntToFloat <<= 8;
//            IntToFloat += Data[3];
//            IntToFloat <<= 8;
//            IntToFloat += Data[4];
//            IntToFloat <<= 8;
//            IntToFloat += Data[5];
//            memcpy(&(Zone2.MainVolume),&IntToFloat,4);
            
            Zone2.IsMute = Data[6];
            Zone2.DataValid = Data[7];
            Zone2.ReceiverOnline = Data[8];
            Zone2.SyncZones = Data[9];
            
            
            
            AudioConsoleSetReceiverStatus(&MainZone, &Zone2);
            
            
            break;
        }
        case AV_RECEIVER_INTERFACE_SET_MAIN_POWER_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SET_ZONE2_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SET_MAIN_ZONE_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SET_SPEAKERS_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SEND_DIRECT_CMD_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SELECT_SCENE_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SET_ZONE2_INPUT_RESPONSE:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_SET_SYNC_Z2_WITH_Z1_RESPONSE:
        {
            break;
        }
        case ETH_NETWK_DEVICE_INFO_REQUEST:
        {
            char data[2];
            data[0] = ID_AVRECEIVER_CONSOLE_INTERFACE;
            data[1] = MY_DEVICE_ADDRESS;
            int PayloadSize;
            unsigned char *response = ProtocolGetFrame(ID_MASTER,MASTER_ADDRESS,ID_ETHERNET_VIRTUAL,ETH_NETWK_DEVICE_INFO_RESPONSE, data,2,0, &PayloadSize);
            SendAudioInterfaceData((char*)response,PayloadSize);
            
            break;
        }
        case ETH_NETWK_SET_DEVICE_INFO_ACK:
        {
            AudioConsoleSetIFConnectionStatus(true);
            break;
        }
        case ETH_NETWK_CONNECTION_REFUSED:
        {
            break;
        }
        case AV_RECEIVER_INTERFACE_GENERAL_STATUS_REQUEST:
        case AV_RECEIVER_INTERFACE_SET_MAIN_POWER_REQUEST:
        case AV_RECEIVER_INTERFACE_SET_ZONE2_REQUEST:
        case AV_RECEIVER_INTERFACE_SET_MAIN_ZONE_REQUEST:
        case AV_RECEIVER_INTERFACE_SET_SPEAKERS_REQUEST:
        case AV_RECEIVER_INTERFACE_SEND_DIRECT_CMD_REQUEST:
        case AV_RECEIVER_INTERFACE_SELECT_SCENE_REQUEST:
        case AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_REQUEST:
        case AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_REQUEST:
        case AV_RECEIVER_INTERFACE_SET_ZONE2_INPUT_REQUEST:
        case AV_RECEIVER_INTERFACE_SET_SYNC_Z2_WITH_Z1_REQUEST:
            
        default:
        {
            
        }
    }
}

void AudioConsoleSendCommand(int Command, unsigned char *Data, unsigned int DataSize)
{
    unsigned char *Payload;
    unsigned int PayloadSize;
    Payload = ProtocolGetFrame(ID_MASTER,MASTER_ADDRESS,ID_AVRECEIVER_CONSOLE_INTERFACE,Command,Data,DataSize,0,&PayloadSize);
    SendAudioInterfaceData((char*)Payload,PayloadSize);
}