#include "SMSDevice.h"
#include "ProtocolDefs.h"
#include <QBuffer>
#include "Contact.h"

CSMSDevice::CSMSDevice(int Address, CAbstractNetworkCommIF *NetworkInterface, CVoipMsSMSClient *VoipMsIFPtr):
    CNetworkDevice(ID_SMS_CLIENT,Address,NetworkInterface)
{
    mStatusRequestsCount = 0;
    mVoipMsInterfaceHandle = VoipMsIFPtr;
    mStatusRequestTimer = new QTimer;
    mStatusRequestTimer->setInterval(ETHERNET_NETWORK_STATUS_TIMEOUT);
    mStatusRequestTimer->setSingleShot(true);
    connect(mStatusRequestTimer,SIGNAL(timeout()),this,SLOT(StatusReqTimerExpired()));
   // mStatusRequestTimer->start();
}

CSMSDevice::~CSMSDevice()
{
    delete mStatusRequestTimer;
}

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

    Q_UNUSED(DeviceID)
//    Q_UNUSED(DeviceAddress)
//    Q_UNUSED(MessageID)
    Q_UNUSED(DataSize)
    Q_UNUSED(Data)


    mStatusRequestTimer->stop();

    if(DeviceAddress != GetDeviceAddress())
    {
        return RET_ERROR;
    }

    switch(MessageID)
    {
        case SMS_CLIENT_DEVICE_ACK:
        {
            break;
        }
        case SMS_CLIENT_DEVICE_STATUS_RESPONSE:
        {
           // mStatusRequestTimer->start();
            DeviceStatusReceived();
            break;
        }
        case SMS_CLIENT_DEVICE_DID_INFO_REQUEST:
        {
            QByteArray FrameData;
            QBuffer FrameBuffer(&FrameData);
            FrameBuffer.open(QIODevice::ReadWrite);
            QDataStream *FrameDataStrm = new QDataStream(&FrameBuffer);
          //  QDataStream *FrameDataStrm = new QDataStream(&FrameData,QIODevice::ReadWrite);

            QString DID = mVoipMsInterfaceHandle->GetDefaultDID();
            *FrameDataStrm << DID;

            FrameBuffer.seek(0);
            mNetworkInterfacePtr->SendNetworkMessage(mDeviceID,mDeviceAddress,SMS_CLIENT_DEVICE_DID_INFO_RESPONSE,FrameData.size(),&FrameData);
            delete FrameDataStrm;

            break;
        }
        case SMS_CLIENT_DEVICE_GET_ALL_MSG_REQUEST:
        {
            QByteArray FrameData;
            QBuffer FrameBuffer(&FrameData);
            FrameBuffer.open(QIODevice::ReadWrite);
            QDataStream *FrameDataStrm = new QDataStream(&FrameBuffer);

            QList<CSMSMessage> *SMSList = mVoipMsInterfaceHandle->GetSMSList();
            *FrameDataStrm << SMSList->size();
            for(int i = 0; i < SMSList->size(); i++)
            {
                *FrameDataStrm << SMSList->at(i);
            }

            FrameBuffer.seek(0);
            mNetworkInterfacePtr->SendNetworkMessage(mDeviceID,mDeviceAddress,SMS_CLIENT_DEVICE_GET_ALL_MSG_RESPONSE,FrameData.size(),&FrameData);

            FrameBuffer.close();
            delete FrameDataStrm;

            break;
        }
        case SMS_CLIENT_DEVICE_SEND_SMS_REQUEST:
        {
//            QBuffer FrameBuffer(&Data);
//            FrameBuffer.open(QIODevice::ReadWrite);
            QDataStream *FrameDataStrm = new QDataStream(Data);

            qint32 NbMsgs;
            *FrameDataStrm >> NbMsgs;
            if(NbMsgs > 0)
            {

                QList<CSMSMessage> SMSList;
                for(int i = 0; i < NbMsgs; i++)
                {
                    CSMSMessage NewMessage;
                    *FrameDataStrm >> NewMessage;
                    SMSList.append(NewMessage);
                }
                mVoipMsInterfaceHandle->SendSMS(&SMSList);

            }

      //      FrameBuffer.close();
            delete FrameDataStrm;

            break;

        }
        case SMS_CLIENT_DEVICE_GET_CONTACTS_REQUEST:
        {
            QByteArray FrameData;
            QBuffer FrameBuffer(&FrameData);
            FrameBuffer.open(QIODevice::ReadWrite);
            QDataStream *FrameDataStrm = new QDataStream(&FrameBuffer);

            QList<CContact> *ContactsList = mContactRepositoryHandle->GetContacts();
            *FrameDataStrm << ContactsList->size();
            for(int i = 0; i < ContactsList->size(); i++)
            {
                *FrameDataStrm << ContactsList->at(i);
            }

            FrameBuffer.seek(0);
            mNetworkInterfacePtr->SendNetworkMessage(mDeviceID,mDeviceAddress,SMS_CLIENT_DEVICE_GET_CONTACTS_RESPONSE,FrameData.size(),&FrameData);

            FrameBuffer.close();
            delete FrameDataStrm;

            break;

        }
        case SMS_CLIENT_DEVICE_STATUS_REQUEST:
        case SMS_CLIENT_DEVICE_DID_INFO_RESPONSE:
        case SMS_CLIENT_DEVICE_NEW_MSG_NOTIFICATION:
        case SMS_CLIENT_DEVICE_GET_ALL_MSG_RESPONSE:
        case SMS_CLIENT_DEVICE_GET_CONTACTS_RESPONSE:
        default:
        {
            qDebug("SMSDevice Rx invalid MSG %d",MessageID);
            break;
        }

    }
    mIsOnline = true;
    mStatusRequestTimer->start();
    mStatusRequestsCount = 0;
    return RET_OK;
}

void CSMSDevice::RequestStatus()
{
    mNetworkInterfacePtr->SendNetworkMessage(mDeviceID,mDeviceAddress,SMS_CLIENT_DEVICE_STATUS_REQUEST,0,0);
}

void CSMSDevice::SendSMSSentAck(bool Ack)
{
    QByteArray FrameData;
    QBuffer FrameBuffer(&FrameData);
    FrameBuffer.open(QIODevice::ReadWrite);
    QDataStream *FrameDataStrm = new QDataStream(&FrameBuffer);

    qint32 ACKData = 0;
    if(Ack)
    {
        ACKData = 1;
    }
    *FrameDataStrm << ACKData;

    mNetworkInterfacePtr->SendNetworkMessage(mDeviceID,mDeviceAddress,SMS_CLIENT_DEVICE_SEND_SMS_ACK,FrameData.size(),&FrameData);


    FrameBuffer.close();
    delete FrameDataStrm;
}

void CSMSDevice::SendNewSMSReceived(QList<CSMSMessage> NewMessages)
{
    QByteArray FrameData;
    QBuffer FrameBuffer(&FrameData);
    FrameBuffer.open(QIODevice::ReadWrite);
    QDataStream *FrameDataStrm = new QDataStream(&FrameBuffer);

   // QList<CSMSMessage> *SMSList = mVoipMsInterfaceHandle->GetSMSList();
    *FrameDataStrm << NewMessages.size();
    for(int i = 0; i < NewMessages.size(); i++)
    {
        *FrameDataStrm << NewMessages.at(i);
    }

    FrameBuffer.seek(0);
    mNetworkInterfacePtr->SendNetworkMessage(mDeviceID,mDeviceAddress,SMS_CLIENT_DEVICE_NEW_MSG_NOTIFICATION,FrameData.size(),&FrameData);

    FrameBuffer.close();
    delete FrameDataStrm;
}

void CSMSDevice::StatusReqTimerExpired()
{
    if(++mStatusRequestsCount > 3)
    {
        mIsOnline = false;
        qDebug("SMS Device offline,Address: %d",mDeviceAddress);
    }
    RequestStatus();
    mStatusRequestTimer->start();
}

void CSMSDevice::DeviceStatusReceived()
{

    mStatusRequestsCount = 0;
    mStatusRequestTimer->start();
}
