#include "Ispindel.h"
#include <QDataStream>
#include "IspindelInterface.h"

CIspindel::CIspindel(CIspindelGUI *IspindelGui)
{
    mIspindelGui = IspindelGui;
    IspindelGui->mProgramHandle = this;
    mNetworkInterface = new CIspindelInterface(this);
    mOG = 0.0;

}

CIspindel::~CIspindel()
{
    delete mNetworkInterface;
}

void CIspindel::Start()
{
    mNetworkInterface->ConnectToMasterCtrl();
}

void CIspindel::IspindelFullBufferReceived(QByteArray *Data)
{
    int NbItems;
    QDataStream Strm(Data,QIODevice::ReadOnly | QIODevice::Unbuffered);

    Strm >> NbItems;

    if(NbItems == 0)
    {
        qDebug("Received empty Ispindel buffer...");
        return;
    }

    ClearIspindleDataList();

    for(int i = 0; i < NbItems; i++)
    {
        CIspindelData *NewFrame = new CIspindelData;
        Strm >> *NewFrame;

        mIspindelDataList.append(NewFrame);
    }
    mOG = mIspindelDataList.first()->mGravity;
    SetLasFrameTextInGUI(*mIspindelDataList.last());
    mIspindelGui->UpdateIspindelPlot(&mIspindelDataList);



}

void CIspindel::IspindelLastFrameReceived(QByteArray Data)
{
    int DataSize;
    QDataStream Strm(&Data,QIODevice::ReadOnly | QIODevice::Unbuffered);

    if(Data.size() == 0)
        return;

    CIspindelData *NewData = new CIspindelData();
    Strm >> *NewData;

    mIspindelDataList.append(NewData);
    SetLasFrameTextInGUI(*NewData);
    mIspindelGui->NewIspindelFrameReceived(NewData);

   // qDebug("Latest Ispindel data received");

}
int CIspindel::DeleteSampleResponseReceived(QByteArray Data)
{
    bool Success;
    QDataStream Strm(&Data,QIODevice::ReadOnly | QIODevice::Unbuffered);
    Strm >> Success;

    if(Success)
    {
        mNetworkInterface->SendMasterCtrlCommand(ISPINDEL_GET_FULL_DATA_BUFFER_REQUEST,QByteArray());
        return RET_OK;
    }
    return RET_ERROR;
}

void CIspindel::ClearIspindleDataList()
{
    for(int i = 0; i < mIspindelDataList.size(); i++)
    {
        delete mIspindelDataList[i];
    }

    mIspindelDataList.clear();
}

void CIspindel::ConnectedToMaster(bool connected)
{
    if(connected)
    {
         mNetworkInterface->SendMasterCtrlCommand(ISPINDEL_GET_FULL_DATA_BUFFER_REQUEST,QByteArray());
    }
}

void CIspindel::SetLasFrameTextInGUI(CIspindelData Frame)
{
    QString FrameText;
    QString ABVText;

    FrameText = QString("\nLast Frame:\n------------------------------\nAngle: %1\nBattery Voltage: %2\nGravity: %3\nSample Interval: %4\nIspindel ID: %5\nIspindel Name: %6\nRSSI: %7\nTemperature: %8%9 (%11F)\nSample date time: %10\n------------------------------")\
                        .arg(Frame.mAngle)\
                        .arg(Frame.mBattery)\
                        .arg(Frame.mGravity)\
                        .arg(Frame.mInterval)\
                        .arg(Frame.mIspindelID)\
                        .arg(Frame.mIspindelName)\
                        .arg(Frame.mRSSI)\
                        .arg(Frame.mTemperature).arg(Frame.mTemperatureUnits)\
                        .arg(Frame.mSampleDateTime.toString("yyyy-MM-dd - hh:mm:ss"))\
                        .arg(((Frame.mTemperature*9/5)+32));

    if(mIspindelDataList.size() > 1)
    {
        float ABV = ((mOG - Frame.mGravity) * 131.25);
        ABVText = QString("ABV : %1\%").arg(ABV);
    }
    else
    {
        ABVText = QString("ABV : ?\%");
    }

    mIspindelGui->SetLastIspindelFrameData(FrameText,ABVText);
}

int CIspindel::SetOGFromItem(int ItemIndex)
{
    if(ItemIndex >= mIspindelDataList.size())
        return RET_ERROR;

    mOG = mIspindelDataList.at(ItemIndex)->mGravity;
    SetLasFrameTextInGUI(*mIspindelDataList.last());

    return RET_OK;
}

int CIspindel::DeleteSample(int ItemIndex)
{
    if(ItemIndex >= mIspindelDataList.size())
        return RET_ERROR;
    QByteArray Data;
    QDataStream Strm(&Data,QIODevice::WriteOnly | QIODevice::Unbuffered);
    Strm << ItemIndex;

    mNetworkInterface->SendMasterCtrlCommand(ISPINDEL_DELETE_SAMPLE_REQUEST,Data);
}
