#ifndef CIGARETTE_H
#define CIGARETTE_H

#include "ui_cigarette.h"
#include "dialogsetuppasswd.hpp"
#include "dialogin.hpp"
#include "dialogsetup.hpp"
#include "plcsetup.hpp"
#include "alg_jd.h"
#include "balluffcamera.h"
#include "baslercamera.h"
#include "hikcamera.h"
#include "workthread.h"
#include "db_label.h"
#include "common.h"
#include "modbus.h"
#include "plc_item.h"
#include "AlarmInfo.h"
#include "alarmdialog.hpp"
#include <apps/Common/qtIncludePrologue.h>
#include <apps/Common/qtIncludeEpilogue.h>
#include <Common/STLHelper.h>

#include "CaptureThread.h"
#include "CaptureThreadBasler.h"
#include "CaptureThreadHIK.h"
#include "debugthread.h"
#include "savethread.h"
#include "Logthread.h"
#include "threadSend.h"
#include "threadReceive.h"
#include "Cleanthread.h"

#include <QtWidgets/QMainWindow>
#include <QTimer>
#include <QCloseEvent>
#include <QThread>
#include <QString>
#include <QScreen>
#include <QMutex>
#include <QDir>
#include <QFileInfoList>
#include <Windows.h>
#include <QSignalMapper>
#include <QSharedPointer>

#include <common.h>

class QTimer;
#define OPEN true
#define CLOSE false
class Cigarette : public QMainWindow
{
	Q_OBJECT

public:
	Cigarette(QWidget *parent = 0);
	~Cigarette();

	static QImage cvMatToQImage(const cv::Mat& mat);

	QString read_pswd();
	QString read_op_pswd();

	bool read_sys_config(SysConf &conf);
	bool read_modbus_config(ModbusConf &conf);
	void read_plc_items();
	bool save_rotate_message();
	bool read_rotate_message();
	bool save_alarm_message(std::vector<AlarmInfo> &AlarmInfo_vect);
	void init_plc_value();
	void CreatWorkThread(int classid,int Num,Cigarette* classptr);
	void InitPtrMat();
	bool ControlCamOpenOrClose(int Num,bool OpenOrClose);
	void EnumerateCameras(SingleCamInfoStruct *TempSingleCamInfo,bool showinfo,SysConf &conf);
	void start_work();
	void pause_work();
	void CleanThreadStart();
	void Exit();
private slots :
	void on_btn_start_released();
	void on_btn_pause_released();
	//void on_btn_pause_clicked(bool checked);
	void on_btn_lock_released();
	void on_btn_setup_released();
	void on_checkBox_debug_clicked(bool checked);
	void on_checkBox_unkick_clicked(bool checked);
	void on_toolButton_plc_released();
	void on_toolButton_alarm_released();
	void on_pushButton_clear_released();
	void enable_shift();
	void sendLatestData();
	void OnNotifyHub(int Num, int Cnt, cv::Mat m);
	void OnDisplayTimeCostHub(int Num, int ms);
	void OnDisplayCheckNumberHub(int Num, long no);
	void OnDisplayJdNoHub(int Num, QString jd_no);
	void OnOKHub(int Num);
	void OnNGHub(int Num);
	void updateStatisticsHub(const QString& statisticalData, int Num);
	void OnRotateReleasedHub(int Num);
	void OnToolButtonCamReleasedHub(int Num);
	void OpenCamTimeoutHub(int Num);
	void OnDBClickHub(int Num_Cnt);
	void DrawRect_init(int Num_Cnt);
	void OnTPClickHub(int Num_Cnt);
	void OnDBClickNGHub(int Num);
	void ReconnectCamHub(int Num);
	void OnMouse(QMouseEvent* event);
	void OnKey(QKeyEvent* event);
	void handleTimeout();  //定时任务，每秒运行一次
	void op_timeout();		//操作员剩余时间
	void admin_timeout();	//管理员权限剩余时间
	void EnableDebugMode();
	void DisableDebugMode();

	void OnCancelAlarm();	//双击消警
	void on_pushButton_reset_released();//复位

	void OnOp();
	void OnExit();
	void OnRestart();
	void OnAdmin();
#ifdef __DEBUG
	void TestImg();
	void TestImgs();
#endif
private:
	Ui::CigaretteClass ui;
	DialogSetupPasswd *dialog_setup_passwd;
	Dialogin *dialogin;
	DialogSetup *dialog_setup;
	PlcSetup * dialog_plc_setup;
	std::vector<PlcItem> m_plc_items;
public:
	QTimer *m_pTimer_Cam_mat[NumberOfSupportedCameras];	//相机重启定时器
	QLabel *label_cap_speed_mat[NumberOfSupportedCameras];
	QLabel *cam_status_mat[NumberOfSupportedCameras];
	QLabel *cam_work_mat[NumberOfSupportedCameras];
	QToolButton *cam_toolButton_mat[NumberOfSupportedCameras];
	db_label * display_lable_mat[NumberOfSupportedCameras][2];
	QLabel *label_timecost_mat[NumberOfSupportedCameras];
	QLCDNumber *lcdNumber_total_mat[NumberOfSupportedCameras];
	QLabel *label_jd_no_mat[NumberOfSupportedCameras];
	QLCDNumber *lcdNumber_ok_mat[NumberOfSupportedCameras];
	QLCDNumber *lcdNumber_ng_mat[NumberOfSupportedCameras];
	QLabel *label_reslut_mat[NumberOfSupportedCameras];
	QToolButton *rotate_mat[NumberOfSupportedCameras];
	QLabel *label_ng_mat[NumberOfSupportedCameras];

	struct widget_info
	{
		UINT32 x;
		UINT32 y;
		UINT32 width;
		UINT32 height;
	};
	widget_info display_lable_info[NumberOfSupportedCameras][2];
	widget_info rotate_info[NumberOfSupportedCameras];
	
	WorkThread work_thread[NumberOfSupportedCameras];
	DebugThread debug_thread[NumberOfSupportedCameras];

	SaveThread saveThread;
	void record_output_statistic(qint64 cur_quantity, int shift);

	QTimer *m_pTimer;
	QTimer *m_delay;	//换班防止连击
	QTimer* m_sendMsg_delay;	// 停止工作后发送最新数据给远程端
	QTimer *m_op_delay;	//操作员权限剩余时间
	QTimer *m_admin_delay;	//操作员权限剩余时间
	QTimer *clean_pTimer; //定时清理任务

	QSignalMapper *image_lable_DBsignalMapper0;
	QSignalMapper *image_lable_DBsignalMapper1;
	QSignalMapper *image_lable_TPsignalMapper0;
	QSignalMapper *image_lable_TPsignalMapper1;
	QSignalMapper *label_ng_signalMapper;
	QSignalMapper *pTimer_Cam_signalMapper;
	QSignalMapper *toolButton_cam_signalMapper; 
	QSignalMapper *RotateReleased_signalMapper;
	int total_production_number;
	int production_number[NumberOfSupportedCameras];
	int OpenWithUserID[NumberOfSupportedCameras];
	int ok[NumberOfSupportedCameras], ng[NumberOfSupportedCameras];
#ifdef	__UDPSend
	private:
		threadSend sThread;
		threadReceive *rThread;
#endif
public slots:
	void CleanThreadStartAuto();
	void recMsgFromUdp(QString data);
	//void ClogThreadStart();

signals:
	void sengMsgToClog(QString);
};

#endif // CIGARETTE_H
