#include "AvReceiverNetworkCtrlInterface.h"
#include "AvReceiver.h"

CAvReceiverNetworkCtrlInterface::CAvReceiverNetworkCtrlInterface(CAvReceiver *ProgramHandle)
{
    mMyDeviceID = ID_AVRECEIVER_INTERFACE;
    mNetworkPort = 2182;
    mMasterCtrlIPAddress = "127.0.0.1";
    mNetworkCommSocket = 0;
    mDeviceAddress = 1;

    mProgramHandle = ProgramHandle;
}
int CAvReceiverNetworkCtrlInterface::DeviceFrameReceived(int TargetDeviceID, int TargetDeviceAddress, int SenderID, int SenderAddress, int MessageID, int DataSize, QByteArray Data)
{
    Q_UNUSED(DataSize)


    if(TargetDeviceID == mMyDeviceID && (TargetDeviceAddress == BROADCAST_VALUE || TargetDeviceAddress == mDeviceAddress))
    {
        switch(MessageID)
        {

            case AV_RECEIVER_INTERFACE_ACK:
            {
                qDebug("Av Receiver Interface ACK received :)");
                break;
            }

            case AV_RECEIVER_INTERFACE_GENERAL_STATUS_RESPONSE:
            {
//                qDebug("Av Receiver General Status received :)");
                mProgramHandle->ReceiverGeneralStatusReceived(Data);
                break;
            }

            case AV_RECEIVER_INTERFACE_SET_MAIN_POWER_RESPONSE:
            {
                break;
            }

            case AV_RECEIVER_INTERFACE_SET_ZONE2_RESPONSE:
            {
                break;
            }

            case AV_RECEIVER_INTERFACE_SEND_DIRECT_CMD_RESPONSE:
            {
                break;
            }
            case AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_RESPONSE:
            {
                break;
            }
            case AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_RESPONSE:
            {
                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_SEND_DIRECT_CMD_REQUEST:
            case AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_REQUEST:
            case AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_REQUEST:
            default:
            {
                qDebug("AV Receiver: Invalid Ethernet Msg received: %d",MessageID);
                break;
            }

        }
    }

    return RET_OK;
}

int CAvReceiverNetworkCtrlInterface::DeviceConnectedToMaster(bool Connected)
{

    if(Connected)
    {
        qDebug("Av Receiver Interface connected to Master.");
        return SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_GENERAL_STATUS_REQUEST,QByteArray());
    }
    else
        return RET_ERROR;

    return RET_OK;
}
