#include "threadSend.h"
#include <QUdpSocket>
#include "basecamera.h"
#include <QFile>
#include <QFileInfo>
void threadSend::init(SyncQueue<_UDPSendInfo> *p_UDP_Info_queue,std::string IP,int port)
{
    ip = QString::fromStdString(IP);
    dataport = port;
    imageport = port+NumberOfSupportedCameras;
    Local_UDP_Info_queue = p_UDP_Info_queue;
    qDebug() << "MonitorIP ip:"<<ip<<"|MonitorPort:"<<dataport;
}
void threadSend::start_work()
{
    //start(HighestPriority);
    start();
}

void threadSend::stop()
{
    isLoop = false;
    // wait();
}

void threadSend::run()
{
    while (isLoop)
    {
        _UDPSendInfo UDPSendInfo;
        Local_UDP_Info_queue->take(UDPSendInfo);
        switch(UDPSendInfo.FrameID){
            case info_frame:{
                sendData(&UDPSendInfo, dataport);
                break;
            }
            case speed_frame:{
                sendSpeed(&UDPSendInfo, dataport);
                break;
            }
            case kick_frame:{
                sendKick(&UDPSendInfo, dataport);
                break;
            }
            case ok_frame:{
                sendOK(&UDPSendInfo, dataport);
                break;
            }
            case ng_frame:{
                sendNG(&UDPSendInfo, dataport);
                break;
            }
            case total_frame:{
                sendTotal(&UDPSendInfo, dataport);
                break;
            }
            case image_frame:{
                sendImage(&UDPSendInfo,imageport);
                break;
            }
        }
    }
}

void threadSend::sendData(_UDPSendInfo *UDPSendInfo,int port)
{

    QString res = QString("%1_%2_%3")
        .arg(QString::number(UDPSendInfo->FrameID))
        .arg(UDPSendInfo->JD)
        .arg(UDPSendInfo->timecost);
    mSocket.writeDatagram(res.toUtf8(), QHostAddress(ip), port + UDPSendInfo->index);
}

void threadSend::sendData(QString data, int port)
{
    mSocket.writeDatagram(data.toUtf8(), QHostAddress(ip), port);
}

void threadSend::sendImage(_UDPSendInfo *UDPSendInfo,int port)
{
    cv::Mat mat = UDPSendInfo->image;
    char cnt = UDPSendInfo->cnt;

    cv::resize(mat, mat, cv::Size(image_width, image_heigh));
    int length = image_width * image_heigh * 3;

    char* data = (char*)malloc(sizeof(char) * length + 1);
    if (data)
    {
        memcpy(data, &cnt, 1);
        memcpy(data+1, mat.data, length);
        int ret=mSocket.writeDatagram((char*)data, length+1, QHostAddress(ip), port + UDPSendInfo->index);
        free(data);
    }
}

void threadSend::sendSpeed(_UDPSendInfo *UDPSendInfo,int port)
{
    QString res = QString("%1_%2")
        .arg(QString::number(UDPSendInfo->FrameID))
        .arg(UDPSendInfo->speed);
    mSocket.writeDatagram(res.toUtf8(), QHostAddress(ip), port + UDPSendInfo->index);
}

void threadSend::sendKick(_UDPSendInfo *UDPSendInfo,int port)
{
    QString res = QString("%1_%2")
        .arg(QString::number(UDPSendInfo->FrameID))
        .arg(QString::number(UDPSendInfo->Kick[UDPSendInfo->index]));
    mSocket.writeDatagram(res.toUtf8(), QHostAddress(ip), port + UDPSendInfo->index);
}

void threadSend::sendOK(_UDPSendInfo *UDPSendInfo,int port)
{
    QString res = QString("%1_%2")
        .arg(QString::number(UDPSendInfo->FrameID))
        .arg(QString::number(UDPSendInfo->ok[UDPSendInfo->index]));
    mSocket.writeDatagram(res.toUtf8(), QHostAddress(ip), port + UDPSendInfo->index);
}

void threadSend::sendNG(_UDPSendInfo *UDPSendInfo,int port)
{
    QString res = QString("%1_%2")
        .arg(QString::number(UDPSendInfo->FrameID))
        .arg(QString::number(UDPSendInfo->ng[UDPSendInfo->index]));
    mSocket.writeDatagram(res.toUtf8(), QHostAddress(ip), port + UDPSendInfo->index);
}

void threadSend::sendTotal(_UDPSendInfo *UDPSendInfo,int port)
{
    QString res = QString("%1_%2")
        .arg(QString::number(UDPSendInfo->FrameID))
        .arg(QString::number(UDPSendInfo->Total));
    mSocket.writeDatagram(res.toUtf8(), QHostAddress(ip), port + UDPSendInfo->index);
}

void threadSend::sendFile(QString FilePath, int port)
{
    QFileInfo info(FilePath);
    QString filename = info.fileName();
    QFile file(FilePath);
    if (file.size() > 0) {
        bool isOk = file.open(QIODevice::ReadOnly);
        if (false == isOk) {
            return ;
        }
    }
    else {
        return ;
    }
    QString sync("SYNC");
    if (!file.atEnd()) {
        QString FileStartFrame(sync);
        FileStartFrame.append("str");
        FileStartFrame.append(filename);
        mSocket.writeDatagram(FileStartFrame.toLatin1(), QHostAddress(ip), port);
    }

        msleep(10);
    while (!file.atEnd()) {//文件指针
        QString FileFrame(sync);
        FileFrame.append("con");
        QByteArray  line = FileFrame.toLatin1() + file.read(1000);
        mSocket.writeDatagram(line, QHostAddress(ip), port);
        msleep(10);
    }

    QString FileEndFrame(sync);
    FileEndFrame.append("end");//结尾标志
    FileEndFrame.append(QString::number(file.size()));//文件大小
    mSocket.writeDatagram(FileEndFrame.toLatin1(), QHostAddress(ip), port);
    file.close();
}