#include "MasterCtrlInterface.h"

CMasterCtrlInterface::CMasterCtrlInterface()
{
    mNetworkCommSocket = 0;

    mNetworkCommSocket = new QTcpSocket();

    connect(mNetworkCommSocket,SIGNAL(connected()),this,SLOT(NetworkSocketConnected()));
    connect(mNetworkCommSocket,SIGNAL(disconnected()),this,SLOT(NetworkSocketDisconnected()));
    connect(mNetworkCommSocket,SIGNAL(readyRead()),this,SLOT(NetworkSocketDataAvailable()));

    mMasterReconnectTimer.setInterval(1000);
    mMasterReconnectTimer.stop();
    mMasterReconnectTimer.setSingleShot(false);
    connect(&mMasterReconnectTimer,SIGNAL(timeout()),this,SLOT(NetworkReconnectTimerExpired()));

}

int CMasterCtrlInterface::ConnectToMasterCtrl()
{
    if(mNetworkCommSocket == 0)
    {
        mNetworkCommSocket = new QTcpSocket();

        connect(mNetworkCommSocket,SIGNAL(connected()),this,SLOT(NetworkSocketConnected()));
        connect(mNetworkCommSocket,SIGNAL(disconnected()),this,SLOT(NetworkSocketDisconnected()));
        connect(mNetworkCommSocket,SIGNAL(readyRead()),this,SLOT(NetworkSocketDataAvailable()));
    }
    if(mNetworkCommSocket->state() == QAbstractSocket::UnconnectedState)
        mNetworkCommSocket->connectToHost(mMasterCtrlIPAddress,mNetworkPort);

    return RET_OK;
}

int CMasterCtrlInterface::DisconnectMasterCtrl()
{
    if(mNetworkCommSocket != 0)
    {
        mNetworkCommSocket->close();
      //  mNetworkCommSocket->deleteLater();
    }
    mMasterReconnectTimer.start();

    return RET_OK;
}

int CMasterCtrlInterface::NewFrameReceived(int TargetDeviceID, int TargetDeviceAddress, int SenderID, int SenderAddress, int MessageID, int DataSize, QByteArray Data)
{

    if(TargetDeviceID == ID_ETHERNET_VIRTUAL)
    {
        switch(MessageID)
        {
            case ETH_NETWK_DEVICE_INFO_REQUEST:
            {
                QByteArray Frame,ResponseData;
                ResponseData.append((char)mMyDeviceID);
                ResponseData.append((char)mDeviceAddress);
                Frame = GetTxPacket(ETH_NETWK_DEVICE_INFO_RESPONSE,0,ResponseData.data(),ResponseData.size(),1,ID_MASTER,ID_ETHERNET_VIRTUAL,mDeviceAddress);
                mNetworkCommSocket->write(Frame);
                break;
            }
            case ETH_NETWK_CONNECTION_REFUSED:
            {
                qDebug("MasterCtrl connection refused for device ID: %d, address: %d",TargetDeviceID,TargetDeviceAddress);
                mNetworkCommSocket->close();
                delete mNetworkCommSocket;
                mNetworkCommSocket = 0;
                break;
            }
            case ETH_NETWK_SET_DEVICE_INFO_ACK:
            {
//                mNetworkCommSocket->write(GetTxPacket(SMS_CLIENT_DEVICE_DID_INFO_REQUEST,0,0,0,1,ID_MASTER,ID_SMS_CLIENT,mDeviceAddress));
                DeviceConnectedToMaster();
                qDebug("MasterCtrl accepted connection");
                break;
            }
            case ETH_NETWK_DEVICE_INFO_RESPONSE:
            default:
            {
                break;
            }

        }
    }
    else
    {
        DeviceFrameReceived(TargetDeviceID,  TargetDeviceAddress, SenderID, SenderAddress, MessageID, DataSize, Data);
    }

    return RET_OK;
}

void CMasterCtrlInterface::NetworkSocketConnected()
{
    qDebug("Connected to MasterCtrl");
    mMasterReconnectTimer.stop();

}

void CMasterCtrlInterface::NetworkSocketDataAvailable()
{
    AnalyzeRxBuffer(mNetworkCommSocket->readAll());
}

void CMasterCtrlInterface::NetworkSocketDisconnected()
{
    qDebug("Disconnected from MasterCtrl");
    mMasterReconnectTimer.start();

}

int CMasterCtrlInterface::SendMasterCtrlCommand(unsigned char MessageID, QByteArray Data)
{
    mNetworkCommSocket->write(GetTxPacket(MessageID,0,Data.data(),Data.size(),1,ID_MASTER,mMyDeviceID,mDeviceAddress));

    return RET_OK;
}

void CMasterCtrlInterface::NetworkReconnectTimerExpired()
{
    ConnectToMasterCtrl();
}
