#include "SprinklerMasterCtrlInterface.h"
#include <QByteArray>
#include <QDataStream>
#include <QBuffer>
#include "SystemGui.h"
#include "Sprinkler.h"



CSprinklerMasterCtrlInterface::CSprinklerMasterCtrlInterface(CSprinkler *ProgramHandle)
{
    mDeviceAddress = 1;
    mNetworkPort = 2182;
    mMasterCtrlIPAddress = "127.0.0.1";
//    mNetworkPort = 6463;
//    mMasterCtrlIPAddress = "192.168.0.112";
    mNetworkCommSocket = 0;
    mMyDeviceID = ID_SPRINKLER_INTERFACE;

    mProgramHandle = ProgramHandle;
}

CSprinklerMasterCtrlInterface::~CSprinklerMasterCtrlInterface()
{
//    if(mNetworkCommSocket !=0)
//    {
////        mNetworkCommSocket->disconnectFromHost();
//   //     delete mNetworkCommSocket;
//    }
}

int CSprinklerMasterCtrlInterface::DeviceConnectedToMaster(bool Connected)
{
    if(Connected)
    {
         mNetworkCommSocket->write(GetTxPacket(SPRINKLER_INTERFACE_GET_SPRINKLERS_REQUEST,0,0,0,1,ID_MASTER,mMyDeviceID,mDeviceAddress));
    }
    else
    {
        //???
    }
    return RET_OK;
}

int CSprinklerMasterCtrlInterface::DeviceFrameReceived(int TargetDeviceID, int TargetDeviceAddress, int SenderDeviceID, int SenderAddress, int MessageID, int DataSize, QByteArray Data)
{
    Q_UNUSED(DataSize)


    if(TargetDeviceID == mMyDeviceID && (TargetDeviceAddress == BROADCAST_VALUE || TargetDeviceAddress == mDeviceAddress))
    {
        switch(MessageID)
        {
            case SPRINKLER_INTERFACE_ACK:
            {
                break;
            }
            case SPRINKLER_INTERFACE_STATUS_REQUEST:
            {
                QByteArray Frame,ResponseData;
                ResponseData.append(ACK);
                ResponseData.append((char)SPRINKLER_INTERFACE_STATUS_RESPONSE);
                Frame = GetTxPacket(SPRINKLER_INTERFACE_STATUS_RESPONSE,0,ResponseData.data(),ResponseData.size(),1,ID_MASTER,ID_SPRINKLER_INTERFACE,mDeviceAddress);
                mNetworkCommSocket->write(Frame);
                break;
            }
            case SPRINKLER_INTERFACE_GET_SPRINKLERS_RESPONSE:
            {
                break;
            }
            case SPRINKLER_INTERFACE_GET_SPRINKLER_DATA_RESPONSE:
            {
                break;
            }
            case SPRINKLER_INTERFACE_SET_SPRINKLER_DATA_ACK:
            {
                break;
            }
            case SPRINKLER_INTERFACE_GET_SPRINKLER_STATE_RESPONSE:
            {
                for(int i = 0; i < Data.at(0); i++)
                {

                }
                break;
            }
            case SPRINKLER_INTERFACE_SET_SPRINKLER_STATE_ACK:
            {
                QByteArray Frame,RequestData;
                RequestData.append(1);  //device address
                Frame = GetTxPacket(SPRINKLER_INTERFACE_GET_SPRINKLER_STATE_REQUEST,0,RequestData.data(),RequestData.size(),1,ID_MASTER,ID_SPRINKLER_INTERFACE,mDeviceAddress);
                mNetworkCommSocket->write(Frame);
                break;
            }
            case SPRINKLER_INTERFACE_STATUS_RESPONSE:
            case SPRINKLER_INTERFACE_GET_SPRINKLERS_REQUEST:
            case SPRINKLER_INTERFACE_GET_SPRINKLER_DATA_REQUEST:
            case SPRINKLER_INTERFACE_SET_SPRINKLER_DATA_REQUEST:
            case SPRINKLER_INTERFACE_GET_SPRINKLER_STATE_REQUEST:
            case SPRINKLER_INTERFACE_SET_SPRINKLER_STATE_REQUEST:
            default:
            {
                qDebug("Sprinkler: Invalid Ethernet MSg received: %d",MessageID);
                break;
            }

        }
    }

    return RET_OK;
}
