#include "SprinklerInterface.h"
#include "ProtocolDefs.h"
#include <QDebug>
#include "SprinklerMgr.h"
#include "SprinklerDevice.h"


CSprinklerInterface::CSprinklerInterface(int Address, CAbstractNetworkCommIF *NetworkInterface, CSprinklerMgr *SprinklerMgr):
    CNetworkDevice(ID_SPRINKLER_INTERFACE,Address,NetworkInterface)
{
    mSprinklerMgr = SprinklerMgr;
}

CSprinklerInterface::~CSprinklerInterface()
{
}

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

    return RET_OK;

    switch(MessageID)
    {
        case SPRINKLER_INTERFACE_STATUS_RESPONSE:
        {
            break;
        }
        case SPRINKLER_INTERFACE_ACK:
        {
            break;
        }
        case SPRINKLER_INTERFACE_GET_SPRINKLERS_REQUEST:
        {
            break;
        }
        case SPRINKLER_INTERFACE_GET_SPRINKLER_DATA_REQUEST:
        {
            break;
        }
        case SPRINKLER_INTERFACE_GET_SPRINKLER_DATA_RESPONSE:
        {
            break;
        }
        case SPRINKLER_INTERFACE_GET_SPRINKLER_STATE_REQUEST:
        {
//            if(Data.at(0) == 0xFF)
//            {
//                //send all devices...

//            }
//            if(Data.at(0) == 1)
//            {

//            }
            CSprinklerDevice *Sprinkler = mSprinklerMgr->GetSprinkler(1);
            QByteArray Frame, MsgData;
            MsgData.append(1); //1 device.
            MsgData.append(Sprinkler->mDeviceAddress); //1st device address
            MsgData.append((unsigned char)Sprinkler->mSprinklerState);

            mNetworkInterfacePtr->SendNetworkMessage(ID_SPRINKLER_INTERFACE,mDeviceAddress,SPRINKLER_INTERFACE_GET_SPRINKLER_STATE_RESPONSE,MsgData.size(),&MsgData);

            break;
        }
        case SPRINKLER_INTERFACE_SET_SPRINKLER_STATE_REQUEST:
        {
            break;
        }

        case SPRINKLER_INTERFACE_SET_SPRINKLER_DATA_REQUEST:
        case SPRINKLER_INTERFACE_GET_SPRINKLER_STATE_RESPONSE:
        case SPRINKLER_INTERFACE_GET_SPRINKLERS_RESPONSE:
        case SPRINKLER_INTERFACE_SET_SPRINKLER_DATA_ACK:
        case SPRINKLER_INTERFACE_SET_SPRINKLER_STATE_ACK:
        case SPRINKLER_INTERFACE_STATUS_REQUEST:
        default:
        {
            qDebug("Received invalid msg from Sprinkler Interface");
            break;
        }
    }

    return 0;
}


