#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTreeWidgetItem>
#include "cmwidget.h"
#include <QMap>
#include "cdlgmsg.h"
#include "cmotordriver.h"
#include "cctrlbuttonidicon.h"
#include "cdlgterminal.h"
#include "cdlgkeyboard.h"
#include "cdlglotcode.h"
#include "cdlgcasslotstatuset.h"
#include "cmwkeyboard.h"

#include "cdlglogin.h"
#include "cdlgnewfile.h"
#include "cdlgpowermsg.h"
#include "v_state.h"
#include "cctrlgraph_chart.h"
#include "cctrlgraph_pi.h"
#include "cmwemo.h"
#include "cmwcover.h"
#include <memory>
typedef QMap<QString,QString> ArgumentMap;
#define MAXSTATEMACHINE	256
namespace Ui {
class MainWindow;
}
#include <QThread>
class MainWindow;
class CThreadWrite : public QThread
{
    Q_OBJECT
public:
    MainWindow* m_pWindow;
    CThreadWrite(MainWindow* pWindow);
    void run();
};
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void on_pushButton_box_apply_2_clicked();
    void on_pushButton_direct_AutoLight_clicked();
signals:
    void NotifyLogMesasge(const QString& info, int type);
    void NotifyTerminalMessage(const QString& info);

    void NotifyPowerMessage(bool bVisible);
public:
    void SendNotifyLogMessage(const QString& info, int type);
    void SendTerminalMessage(const std::string& info);
    void SendPowerMessage(bool bVisible);
private slots:
    void OnTerminalMessage(const QString& msg);
    void OnShowPowerMessage(bool bVisible);

	void mon_resetButton_clicked(int32_t motorID);

    void on_pushButton_reset_clicked();

    void on_pushButton_param_clicked();

    void on_pushButton_auto_clicked();

    void on_pushButton_manual_clicked();

    void on_pushButton_setup_clicked();

    void on_pushButton_comp_clicked();

    void on_pushButton_record_clicked();

	void on_pushButton_setting_clicked();

    void on_pushButton_emg_clicked(bool checked);

    void on_pushButton_box_save_2_clicked();


    void on_pushButton_reset_all_clicked();

    void on_pushButton_box_diagnosis_clicked();


    void on_pushButton_param_read_clicked();

    void on_pushButton_param_save_clicked();
	void on_btnSettingSave_clicked();

    void on_pushButton_param_new_clicked();

    void on_pushButton_param_new_dir_clicked();

    void on_pushButton_param_delete_clicked();


    void on_radioButton_day_clicked();

    void on_radioButton_week_clicked();

    void on_radioButton_month_clicked();

    void on_radioButton_year_clicked();

    void on_pushButton_record_SelectEvent_clicked();

    void on_pushButton_log_delete_clicked();

    void on_pushButton_log_export_clicked();

    void on_pushButton_login_clicked();


    void on_pushButton_auto_start_clicked();

    void on_pushButton_auto_pause_clicked();


	void on_pushButton_box_LOAD_SIDE_clicked();

	void on_pushButton_box_A_SIDE_clicked();

	void on_pushButton_box_B_SIDE_clicked();

	void on_pushButton_box_cam2X_clicked();

	void on_pushButton_box_cam6X_clicked();

	void on_pushButton_box_D_WIDE_clicked();

	void on_pushButton_box_D_NORROW_clicked();

    void on_btnLNarrow_clicked();

    void on_btnDistanceSensor_clicked();

    void on_pushButton_auto_cass_up_clicked();

    void on_pushButton_auto_pause_coat_clicked();

	void on_pushButton_direct_homeTrans_clicked();

	void on_pushButton_direct_WTReset_clicked();

	void on_pushButton_direct_CTReset_clicked();

    void on_pushButton_direct_CLeanTReset_clicked();

    void on_pushButton_direct_TransReset_clicked();

    void on_pushButton_direct_VaccWT_clicked(bool checked);

    void on_pushButton_direct_VaccCT_clicked(bool checked);

    void on_pushButton_direct_VaccClean_clicked(bool checked);

    void on_pushButton_direct_VaccLoad_clicked(bool checked);

    void on_pushButton_direct_VaccUnLoad_clicked(bool checked);

    void on_pushButton_direct_VaccTrans_clicked(bool checked);


    void on_pushButton_box_save_screen_clicked();

    void on_pushButton_direct_InitWT_clicked();

    void on_pushButton_direct_InitCT_clicked();

    void on_pushButton_direct_InitCleanT_clicked();

    void on_pushButton_direct_InitTrans_clicked();


    void on_pushButton_direct_ClawOpen_clicked();

    void on_pushButton_direct_ClawClose_clicked();

    void on_pushButton_terminal_clicked();

    void on_pushButton_auto_select_clicked(bool checked);

    void on_pushButton_factory_clicked();

    void onLogTips(const QModelIndex& index);

    void on_pushButton_direct_LoadDown_clicked(bool checked);

    void on_pushButton_direct_LoadRight_clicked(bool checked);

    void on_pushButton_direct_UnloadDown_clicked(bool checked);

    void on_pushButton_direct_UnloadRight_clicked(bool checked);



    void on_pushButton_box_showCtrl_clicked(bool checked);

    void on_pushButton_showState_clicked(bool checked);

    void on_pushButton_box_cover_screen_clicked();


    void on_checkBox_MoveAfterHome_clicked(bool checked);


    void on_pushButton_direct_CoatCurtain_clicked(bool checked);

    void on_pushButton_direct_CleanCurtain_clicked(bool checked);

    void on_pushButton_direct_Spindle_Clean_clicked(bool checked);

    void on_pushButton_direct_Spindle_Coat_clicked(bool checked);

    void on_pushButton_Stop_clicked();
    void on_btnModifyWaferStatus_clicked();

    void on_pushButton_box_cam4X_clicked();
    void on_btnSingleNarrow_clicked();

	void on_pushButton_user_clicked();

	void on_btnDel_clicked();
	void on_btnAdd_clicked();
	void on_btnModify_clicked();
	void on_btn_user_fresh_clicked();
	void on_btnSubmit_clicked();

	void on_tabUserClicked(const QModelIndex& index);
    void on_pushButton_clicked();

    void on_btnVaccWTBreak_clicked(bool checked);

    void on_btnVaccCTBreak_clicked(bool checked);

    void on_btnVaccCleanBreak_clicked(bool checked);

    void on_btnVaccUnLoadBreak_clicked(bool checked);

    void on_radioButton_auto_clicked();

    void on_btnAlarmProcess_clicked(bool checked);

    void on_btnLaserCheck_clicked();

	void on_btnSafeDoor_clicked(bool checked);

	void on_btnInitLaser_clicked();

    void on_btnReplaceCoatLiquid_clicked();

	void on_btnBarCode_clicked();

	void on_btnSetCheck_clicked();

	void on_pushButton_LogRecord_clicked();

	void on_cmbLogRecord_Changed(int);

	void on_treeItemDoubleClicked(QTreeWidgetItem *item, int column);
private:
	void loadLogRecord(QString sText, QTreeWidgetItem *parentItem);
private:
    Ui::MainWindow *ui;

protected:
    void showEvent(QShowEvent *event);
    void hideEvent(QHideEvent *event);
    void timerEvent(QTimerEvent *event);
    void closeEvent(QCloseEvent *);
    void resizeEvent(QResizeEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    int timer_time_id;
	int timer_widget_id;
	int timer_time_slow;
	int timer_time_fast;

    enum _MWidget_ID
    {
        MW_reset_StatusMotorPos,        //工作台位置
        MW_status_StatusMotorPos,
        MW_reset_StatusMotorPos_Trans,  //上下料位置
        MW_status_StatusMotorPos_Trans,
		MW_param_product,
		MW_param_cut,
		MW_param_SideA,
		MW_param_SideB,
		MW_param_Coat,
		MW_Param_Clean,
		MW_Param_Locate,
		MW_Param_Kerf,
		MW_Param_HalfKerf,
		MW_view_Cam2X,
		MW_view_Cam6X,
		MW_view_Cam4X,
        MW_view_WT,
		MW_view_Trans,

        MW_cam_MATCHSHAPE_H2X,
        MW_cam_FINDEAGE_H2X,
        MW_cam_MATCHNCC_H2X,
		MW_cam_CALINCC_H6X,
        MW_cam_MATCHSHAPE_H6X,
        MW_cam_AKERF_H6X,
		MW_cam_LaserKerf_H6X,
		MW_cam_HalfcutKerf_H6X,
        MW_cam_MATCHNCC_H6X,
		MW_cam_property2X,
		MW_cam_property6X,
		MW_cam_AKERF_H4X,
		MW_cam_MATCHNCC_H4X,
		MW_cam_property4X,
        MW_cali_WT_COORD,
        MW_cali_BEAM_POS,
        MW_cali_CASS,
		MW_cali_HalfcutKerf,
        MW_cali_KERF,
		MW_cali_LaserKerf_D_Narrow,
        MW_cali_LaserKerf_D_Wide,
        MW_cali_LEVEL,
        MW_cali_LOCATE,
        MW_cali_LASERPATH,
        MW_cali_COAT,
        MW_cali_CLEAN,
        MW_cali_TRANS,
        MW_cali_LASERFOUCS,
        MW_cali_LASERPOWER,
		MW_setting_BASE,
		MW_setting_CASS,
		MW_setting_COAT,
		MW_setting_CLEAN,
		MW_setting_Leveling,
		MW_setting_Kerf,

		MW_state_model_SIDE_A,
		MW_state_model_SIDE_B,
        MW_state_Sensor,
        MW_state_Log,
        MW_state_View,
        MW_state_Cam,
        MW_manual_Load,
        MW_manual_Technology,
		MW_manual_Trans,
        MW_manual_Reset,
        MW_manual_Model,
        MW_manual_Meas,
        MW_manual_Locate,
        MW_manual_Step_Level,
        MW_manual_Cut,
        MW_manual_Kerf,
		MW_manual_LocateKerf,
		MW_manual_LaserCalibration_D_Narrow,
        MW_manual_LaserCalibration_D_Wide,
		MW_manual_Recover,
		MW_comp_Motor,
        MW_comp_Light,
		MW_comp_Other,
		MW_comp_Eap,

        MW_comp_IO_INPUT_IN0,
        MW_comp_IO_OUTPUT_OUT0,
        MW_comp_IO_INPUT_IN1,
        MW_comp_IO_OUTPUT_OUT1,

        MW_comp_IO_INPUT_IN2,
        MW_comp_IO_OUTPUT_OUT2,
        MW_comp_IO_INPUT_IN3,
        MW_comp_IO_OUTPUT_OUT3,

        MW_comp_PowerMeter,
        MW_comp_Laser,
        MW_direct_cam,
        MW_AUTO,
		//MW_ALARM_PROCESS,

        MW_Factory_Offset_Adjust,
		MW_Factory_Laser_Offset_Adjust,
        MW_Factory_Motor_Config,
        MW_Factory_System_Config,
        MW_Factory_Motor_INPUT_IO,
        MW_Factory_Motor_OUT_IO,
        MW_Factor_Motor_INPUT_Analog,

        MW_view_All_Cam,
    };
    QMap<int32_t,CMWidget*> mMWidgets;
    void MWidgetsFresh();
    void MWidgetsTimerFresh();
    void MWidgetsInit();
    void MWidgetsExit();
public:
    void MWidgetsApply();
    void mainwindow_Init();
    void mainwindow_Exit();
    const char* m_strSystemstate;
    bool load_File(QString path, QString relPath);
    void save_File(const QString& path);
private:
    void direct_relmove_msg(int32_t motorid, int32_t idirect);
    void direct_jogmove_msg(int32_t motorid, int32_t idirect,bool bhighspd);
    void direct_jogmove_twospd(int32_t motorid, int32_t idirect);
    void direct_stopmove_msg(int32_t motorid);
    void home_axis_msg(int32_t motorid);
    CDlgMsg* m_pMsgDlg;
	typedef struct _resetButton
	{
        CCtrlButtonIDIcon* pButtonMotor;
		int32_t m_bHomeState;
		_resetButton()
		{
			pButtonMotor=0;
			m_bHomeState=-1;
		}
	}RESETBUTTON;
	typedef QMap<int32_t,RESETBUTTON> mRESETBUTTONs;
	mRESETBUTTONs mresetButtons;
    int32_t m_bAllHomeState;
    void set_homeState(int32_t motorid,bool_t bhasHomed);

    QIcon icon_off;
    QIcon icon_blue;
    QIcon icon_red;

	int32_t m_iCurSideID;
    int32_t m_iCurChID;
    int32_t m_iCurProcessID;
    int32_t m_iCurSMStateType;
    int32_t m_iCurWaferSize;
    void FreshLog(QString& evtid);
    void FreshUser();

    V_State* m_iCurStateID[MAXSTATEMACHINE];
    QString m_sCurWorkName;
	QString m_sLastComm;

    CDlgTerminal* m_pDlgTerminal;
    CDlgLogin *m_pDlgLogin;
	CDlgLotCode* m_pDlgLotCode;
    CDlgPowerMsg* m_pDlgPowerMsg;
public:
    CDlgKeyboard* m_pDlgKeyboard;
    CMWKeyboard* m_pMWKeyboard;
public:
    CDlgNewFile* m_pDlgNewFile;
    std::unique_ptr<CDlgCasslotStatuSet> m_pDlgCasslotStatuSet;
private:
    void freshOutputView();
	void freshMsgDlg();
	void freshStateMachine();
	void freshResetButton();
    void freshDirectIO();
	void freshEAPStatus();
	void freshButtonStatus();
	void freshButtonStatus(int iSMID, int iStateId, QPushButton* pBtn);
    void FreshAuthority();
	bool IsOperatorCanOperate();
    int m_iLaserEmitState;
    int m_iJogMotorID;
    int m_iJogTimerID;
    int m_iDirect;
public:
    void LoginFresh();
    void FileTreeFresh();
    bool m_bEnableSendFreshMsg;

	void GetLightData(int& iLight1, int& iLight2, int iCamIndex);
    void GetCamInfo(int& iExposure1, int& iExposure2, int iCamIndex);
	void SetLightData(int iLight1, int iLight2, int iCamIndex);
	void SetCamInfo(int iExposure1, int iExposure2, int iCamIndex);
private:
    void* GetCamLightCtrol(int iCamIndex);
public:
    void LogStateAnalyse(const QDateTime& startDate, LPI_Graph_Data& data, QStringList &list);

    void LogProductionAnalyse(const QDateTime& startDate,
                              Chart_Data_Vector& vdataUPH,
                              Chart_Data_Vector& vdataPCS);

    enum _CtrlSizeState
    {
        Ctrl_Max,
        Ctrl_Normal,
    };
    void ResizeCtrlSize(int istate);

    CMWEmo* m_pEMODlg;
    CMWCover* m_pCoverDlg;

    int m_iTimerShowMax;
    void ShowMax();
    int m_iIndicatorLightCur;
    QSqlTableModel* m_pModelLog;
    QMutex m_MutextRecipe;
};
extern MainWindow* m_gMainWindow;
#endif // MAINWINDOW_H
