#pragma once
#include <QDialog>
#include "ui_output_statistic.h"
#include <vector>
#include <qdir.h>
#include <iostream>
#include <SyncQueue.h>
#include <qthread.h>
#include "basecamera.h"
#include "tinyxml2.h"
#include "common.h"
#include "cigarette.h"
#include "qdebug.h"
#define EXPORTDATA_FILE    "camera%1_data_info.xml"
#include <wininet.h>

using namespace tinyxml2;

class _XMLExportDataInfo {
public:
    // 总的统计数据
    long long cameraTotal;
    int TotalCheckNum;	// 总检测数量
    int TotalKickNum;	// 总剔除数量
    double PassRate;	// 合格率
    double KickRate;	// 剔除率
    // 各相机数据
    int cameraId;
    int shotCounts;	// 拍摄张数
    int checkNum;	// 各相机检测个数
    int okNum;	// 各相机ok数量
    int ngNum;	// 各相机ng数量
    int kickNum;	// 各相机实际剔除数量（单通道剔除时为总剔除数量）
    QString AcquisitionSpeed;	// 各相机采集速度
    QString jdNum;	// 各相机胶点识别个数
    QString DetectSpeed;	// 各相机检测速度(识别时间)

    _XMLExportDataInfo() {
        // 总的统计数据
        TotalCheckNum = 0;	// 总检测数量
        TotalKickNum = 0;	// 总剔除数量
        PassRate = 0.0;	// 合格率
        KickRate = 0.0;	// 剔除率
        cameraId = -1;
        jdNum = QString("0,0,0");
        // 各相机数据
        shotCounts = 0;
        checkNum = 0;
        okNum = 0;
        ngNum = 0;
        kickNum = 0;		// 各相机实际剔除数量（单通道剔除时为总剔除数量）
        AcquisitionSpeed = "0";	// 各相机采集速度
        DetectSpeed = "0";	// 各相机检测速度
    }
};

class _ExportDataInfo
{
    struct JdPoint //创建点的数据
    {
        string x;
        string y;
    };

public:
    int cameraId;
    int shotCounts;
    int jd[20];
    string timeCost;
    float jdInterval;
    bool isNg;
    bool isJdExist[20];
    long long cameraTotal;
    std::vector<JdPoint> jdPointsLocation;
    float jdSize[20];

    bool getAverageData(map<string, float>& averageData, int index);

    string getPoint(int index) {
        string point = "(";
        point += jdPointsLocation.at(index).x + ',' + jdPointsLocation.at(index).y + ')';
        return point;
    }

    _ExportDataInfo()
    {
        cameraId = -1;
        jdInterval = 0.0;
        isNg = false;
        for (int i = 0; i < 20; i++)isJdExist[i] = false;
        for (int i = 0; i < 20; i++)jd[i] = 0;
        timeCost = "0ms";
        for (int i = 0; i < 20; i++)jdSize[i] = 0.0;
        shotCounts = 0;
    }
};

//extern SyncQueue<_ExportDataInfo>* export_Data_Info_queue;
extern SyncQueue<_XMLExportDataInfo>* export_XMLData_Info_queue;

class ExportDataThread : public QThread
{
    Q_OBJECT
signals:


public:
    string ip, username, userpwd;
    int port;
    ExportDataThread(QObject* parent = 0);
    ~ExportDataThread()
    {
        stop();
        //export_Data_Info_queue->put(data);
        quit();
        wait();
    }
    QTimer* timer;
    QStringList listToZip;
    QStringList tmpListToZip;
    BOOLEAN readyToSendZip;


    void init(string ip_, int port_, string username_, string userpwd_);
    void start_work();
    void stop();
    //int insertXMLNode(const char* xmlPath, _ExportDataInfo& data);
    int insertXMLNode(const char* xmlPath, _XMLExportDataInfo& data);
    bool ConnectFtp(string *ip_, int *port_, string *username_, string *userpwd_);
    bool FtpConnectionTest();
    void ConnectServer(QString srcPath, QString destPath);
    void GetFiles(string path, std::vector<string>& files);

protected:
    void run();
    void check_save_dir(std::string dir_path);
public:
    bool b_quit;
    tinyxml2::XMLDocument* pDocument[NumberOfSupportedCameras];
    //int flag;

    HINTERNET hint = NULL;
    HINTERNET hftp = NULL;


public slots:
    void EDrecMsgFromCigarette();
    void GetDataFromSaveThread(QString filePath);
};
