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


CSprinklerDevice::CSprinklerDevice(int Address, CAbstractNetworkCommIF *NetworkInterface, CSprinklerMgr *SprinklerMgr):
    CNetworkDevice(ID_SPRINKLER_DEVICE,Address,NetworkInterface)
{
    mSprinklerState = SPRINKLER_UNKNOWN_STATE;

    mSprinklerMgr = SprinklerMgr;
    mSprinklerStatusTimer = new QTimer();

    mSprinklerStatusTimer->setInterval(5000);
    mSprinklerStatusTimer->setSingleShot(true);
    connect(mSprinklerStatusTimer,SIGNAL(timeout()),this,SLOT(StatusTimerExpired()));
    mSprinklerStatusTimer->start();


}

CSprinklerDevice::~CSprinklerDevice()
{
}

SprinklerState CSprinklerDevice::GetSprinklerState()
{
    return mSprinklerState;
}

int CSprinklerDevice::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 SPRINKLER_DEVICE_STATUS_RESPONSE:
        {
            if(Data.at(0) != (unsigned char)mSprinklerState)
            {
                if(Data.at(0) == SPRINKLER_ON_STATE || Data.at(0) == SPRINKLER_OFF_STATE)
                {
                    mSprinklerState = (SprinklerState)Data.at(0);
                    //TODO: emit sprinkler change event.
                }
                else
                {
                    mSprinklerState = SPRINKLER_UNKNOWN_STATE;
                    qDebug("Rx Invalid sprinkler state [%d] from device [%d]",Data.at(0),DeviceAddress);
                }
            }

            mFlowMeter = Data.at(1);
            mFlowMeter <<= 8;
            mFlowMeter |= (unsigned short)Data.at(2);

            qDebug("Sprinkler status RX.  Valve:%d, flow:%d",Data.at(0),mFlowMeter);

            mSprinklerStatusTimer->start();
            break;
        }
        case SPRINKLER_DEVICE_ACK:
        {
            break;
        }
        case SPRINKLER_DEVICE_SET_SPRINKLER_STATE_ACK:
        {
            mNetworkInterfacePtr->SendNetworkMessage(ID_SPRINKLER_DEVICE,1,SPRINKLER_DEVICE_GET_SPRINKLER_STATE_REQUEST,0,0);
            break;
        }
        case SPRINKLER_DEVICE_GET_SPRINKLER_STATE_RESPONSE:
        {
            if(Data.at(0) != (unsigned char)mSprinklerState)
            {
                if(Data.at(0) == SPRINKLER_ON_STATE || Data.at(0) == SPRINKLER_OFF_STATE)
                {
                    mSprinklerState = (SprinklerState)Data.at(0);
                    //TODO: emit sprinkler change event.
                }
                else
                {
                    mSprinklerState = SPRINKLER_UNKNOWN_STATE;
                    qDebug("Rx Invalid sprinkler state [%d] from device [%d]",Data.at(0),DeviceAddress);
                }
            }
            qDebug("Valve state RX.  Valve:%d",Data.at(0));
            break;
        }
        case SPRINKLER_DEVICE_GET_WATER_FLOW_RESPONSE:
        {
            mFlowMeter = Data.at(0);
            mFlowMeter <<= 8;
            mFlowMeter |= (unsigned short)Data.at(1);
            qDebug("Flow value RX.  flow:%d",mFlowMeter);
            break;
        }
        case SPRINKLER_DEVICE_GET_MOISTURE_RESPONSE:
        {
            break;
        }
        case SPRINKLER_DEVICE_SET_PROGRAM_ACK:
        {
            break;
        }

        case SPRINKLER_DEVICE_GET_PROGRAM_RESPONSE:
        {
            break;
        }

        case SPRINKLER_DEVICE_SET_PARAMETERS_ACK:
        {
            break;
        }

        case SPRINKLER_DEVICE_GET_PARAMETERS_RESPONSE:
        {
            break;
        }

        case SPRINKLER_DEVICE_SET_SPRINKLER_STATE_REQUEST:
        case SPRINKLER_DEVICE_GET_SPRINKLER_STATE_REQUEST:
        case SPRINKLER_DEVICE_GET_WATER_FLOW_REQUEST:
        case SPRINKLER_DEVICE_GET_MOISTURE_REQUEST:
        case SPRINKLER_DEVICE_SET_PROGRAM_REQUEST:
        case SPRINKLER_DEVICE_GET_PROGRAM_REQUEST:
        case SPRINKLER_DEVICE_SET_PARAMETERS_REQUEST:
        case SPRINKLER_DEVICE_GET_PARAMETERS_REQUEST:
        case SPRINKLER_DEVICE_STATUS_REQUEST:
        default:
        {
            qDebug("Received invalid msg from Sprinkler Device");
            break;
        }
    }

    return 0;
}

void CSprinklerDevice::StatusTimerExpired()
{
    mNetworkInterfacePtr->SendNetworkMessage(ID_SPRINKLER_DEVICE,1,SPRINKLER_DEVICE_STATUS_REQUEST,0,0);
}
