#include "LoraNetworkCommIF.h"
#include "GlobalDefine.h"

CLoraNetworkCommIF::CLoraNetworkCommIF()
{
    connect(&mLoraDeviceSerialPort,SIGNAL(readyRead()),this,SLOT(LoraDeviceNewDataReady()));

    mLoraMagicResponse.clear();
    mLoraMagicResponse.append(0xDE);
    mLoraMagicResponse.append(0xAD);
    mLoraMagicResponse.append(0xBE);
    mLoraMagicResponse.append(0xEF);

    mDestLoraAddress = 6;
    mDestLoraChannel = 4;
}

QByteArray CLoraNetworkCommIF::GetLoraFrame(unsigned short DestAddress, unsigned char DestChannel, QByteArray Payload)
{
    QByteArray OutputFrame;
    OutputFrame.clear();

    //E32 modules frame is [Address_MSB][Address_LSB][Channel][Payload]



    char AddressByte = (char)((DestAddress >> 8) & 0x00FF);
    OutputFrame.append(AddressByte);
    AddressByte = (char)(DestAddress & 0x00FF);
    OutputFrame.append(AddressByte);
    OutputFrame.append(DestChannel);
    OutputFrame.append(Payload);

    return OutputFrame;
}

int CLoraNetworkCommIF::SetLoraSerialPortSettings(QString PortName, qint32 BaudRate)
{
    mLoraSerialPortName = PortName;
    mLoraSerialPortBaudRate = BaudRate;

    if(mLoraDeviceSerialPort.isOpen())
    {
        mLoraDeviceSerialPort.close();
    }

    mLoraDeviceSerialPort.setPortName(mLoraSerialPortName);
    if(mLoraDeviceSerialPort.setBaudRate(BaudRate) == false)
    {
        qDebug("Invalid Lora Device serial port baud rate...");
    }

    //The other serial port parameters (parity, stop bits, data bits) are all set to mentally sane default values so no need to set them.
    if(mLoraDeviceSerialPort.open(QIODevice::ReadWrite) == false)
    {
        qDebug("Could not open Lora device port");
        return RET_ERROR;
    }

    qDebug("Lora serial port opened");
    return RET_OK;

}

int CLoraNetworkCommIF::SendLoraFrame(unsigned short DestAddress, unsigned char DestChannel, QByteArray Payload)
{

    mLoraDeviceSerialPort.write(GetLoraFrame(DestAddress,DestChannel,Payload));
   // mLoraDeviceSerialPort.write(Payload);
    mLoraDeviceSerialPort.waitForBytesWritten(500);
    return RET_OK;

}


void CLoraNetworkCommIF::LoraDeviceNewDataReady()
{
    QByteArray NewData = mLoraDeviceSerialPort.readAll();

//    QString Printf = "New Lora Data: ";

    AnalyzeRxBuffer(NewData);

//    for(int i = 0; i < NewData.size(); i++)
//    {


//        int snatch = NewData.at(i) & 0xFF;

////        mRxLoraTestMsgBuf <<= 8;
////        mRxLoraTestMsgBuf |= snatch;
////        if(mRxLoraTestMsgBuf == (qint32)LORA_MAGIC_WORD)
////        {
////            SendLoraFrame(6,4,mLoraMagicResponse);
////            qDebug("Magic word received, sending magic response!");
////        }

//        QString bleh;
//        bleh = QString().asprintf("0x%X - ",snatch);
//        Printf.append(bleh);
//    }
//    qDebug("%s",qPrintable(Printf));

}

int CLoraNetworkCommIF::NewFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data)
{
    mDevicePtr->NewDeviceFrameReceived(DeviceID,DeviceAddress,MessageID,DataSize,Data);
    return RET_OK;
}

int CLoraNetworkCommIF::SendNetworkMessage(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray *Data)
{
    QByteArray Packet = GetTxPacket(MessageID,0,Data->data(),DataSize,DeviceAddress,DeviceID);
    //SendLoraFrame(6,4,Packet);    //channel 6 = chalet
    //SendLoraFrame(7,4,Packet);  //channel 7 = chaloupe
    SendLoraFrame(mDestLoraAddress,mDestLoraChannel,Packet);
    return RET_OK;
}

int CLoraNetworkCommIF::SetLoraDestinationAddress(quint16 Address, quint8 Channel)
{
    mDestLoraAddress = Address;
    mDestLoraChannel = Channel;
    return RET_OK;
}
