#ifndef ANSWERWIDGET_H
#define ANSWERWIDGET_H

#include <QWidget>
#include <QTimer>
#include <QPushButton>
#include "CHttpBll.h"
#include "question.h"
#include "questionSet.h"
#include "awHandinPaper.h"
#include "awResumeExam.h"
#include "faceLiveness.h"
#include "awTimeLeftTips.h"
#include <opencv2/opencv.hpp>
#include "CLiveViodeProc.h"
#include "CMultiMonitorEnumerator.h"

namespace Ui {
class answerWidget;
}

class answerWidget : public QWidget , ITRTCVideoRenderCallback
{
    Q_OBJECT

signals:
    void showExamScore();
    void showPracticeInfo();
    void gobackCourseList();
    void gobackLogin();
public:
    explicit answerWidget(QWidget *parent = nullptr);
    ~answerWidget();

    void setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight);
private slots:
	void onGetExamProperty(CGetExamProperty getExamProperty);		
	void onWeiXinAnswerEnabled(CWeiXinAnswerEnabled weiXinAnswerEnabled);
	void onGetExamById(CGetExamById getExamById);
	void onFindExamQuestionList(CFindExamQuestionList findExamQuestionList);
	void onGetExamRecordPaperStruct(CGetExamRecordPaperStruct getExamRecordPaperStruct);
	void onGetQuestionContent(CGetQuestionContent getQuestionContent);
	void onStartAnswer(CStartAnswer startAnswer);
	void onExamHeartbeat(CExamHeartbeat examHeartbeat);
    void onSubmitQuestionAnswer(CSubmitQuestionAnswer submitQuestionAnswer);
	void onEndExam(CEndExam endExam);
    void onGetOrgPropertiesByGroupWithoutCache(CGetOrgPropertiesByGroupWithoutCache orgProperties);
    void onStartFaceLiveVerify(CStartFaceLiveVerify startFaceLiveVerify);
	void onGetSoftwareConfig(CGetSoftwareConfig getSoftwareConfig);
    void onExamControlCheckTime(CBaseResponsePackage res);

    void on_btn_handInPaper_clicked();

    void on_btn_all_clicked();

    void on_btn_answered_clicked();

    void on_btn_unanswered_clicked();

    void on_btn_marked_clicked();

    void on_btn_allNum_clicked();

    void on_btn_answeredNum_clicked();

    void on_btn_unansweredNum_clicked();

    void on_btn_markedNum_clicked();

private:
    void getQuestionContent(QString sQuestionId, int nOrder);
    void showSubQuestion(CQuestionStruct &qs, int nOrder, QString sTitle, QString sProgress);
	CAnsweredQuestion findQuestionByOrder(CFindExamQuestionList &feql, int nOrder);
	void heartBreat();
    void uploadAnswer(int nIndex, QString sAnswer);
    void showQuestionByOrder(int nOrder);
    void handinPaper(bool bForce = false);
    void doEndExam(bool bForce);
    void refreshStatus();
    void GetExamProperty();
    void GetExamById();
    void GetExamRecordPaperStruct();
    void FindExamQuestionList();
    void WeiXinAnswerEnabled();
    void startAnwer();
    void GetOrgPropertiesByGroupWithoutCache();
    void StartFaceLiveVerify();
    void exitExamTip();
	bool checkAnswer();
    void startCapture();
    void stopCapture();
    void onRenderVideoFrame(const char* userId, TRTCVideoStreamType streamType, TRTCVideoFrame* frame);
	void checkRemoteBreach();
    void setChecked(QPushButton *btn, bool bChecked);
    bool allMutipleQuestionHas2Answers();

    Ui::answerWidget *ui;
	
	int m_nSnapshotInterval;
	QString m_sPracticeType;
	int m_nFreezeTime;	

	std::vector<CPaperGroupStruct> m_vPaperGroupStruct;

	std::shared_ptr<question> m_pQuestion;
	std::shared_ptr<questionSet> m_pQuestionSet;
    std::shared_ptr<QTimer> m_pCountDownTimer;
    std::shared_ptr<QTimer> m_pHeartbeatTimer;
    std::shared_ptr<awHandinPaper> m_pHandinPaper;
    std::shared_ptr<awResumeExam> m_pResumeExam;
	std::shared_ptr<QTimer> m_pHandinPaperTimer;
    std::shared_ptr<faceLiveness> m_pFaceLiveness;
    std::shared_ptr<awTimeLeftTips> m_ptimeLeftTips;
	std::shared_ptr<QTimer> m_pNetWorkErrorTimerPtr;
    int m_nLeftSeconds = 0;
    int m_nEnterExamTime = 0;
    int m_nUsedExamSeconds = 0;
	int m_nEnterLivenessFaceSeconds = 0;
	bool bShowLivenessTip;//活体检测前提示
	bool bShowLeftHint;
	int m_nHeartBreatFailedCount;
	const int m_nHeartBreatFailedExitCount = 10;

    std::shared_ptr<QTimer> m_pVideoTimer;
//    std::shared_ptr<QTimer> m_pinitTimer;
//    cv::VideoCapture m_cam;
    cv::Mat m_nCurImage;
    std::mutex m_imageMutex;
    __int64 m_lastFaceTime = 0;
    int m_nLastCaptureSecond = 0;
    int m_nNextFaceCountSecond = 0;
	
	QStringList m_sRemoteList;
	QStringList m_sRemoteTipList;


    bool m_bExamInpressFaceCaptured = false;
    int m_nRetryCount = 0;
};

#endif // ANSWERWIDGET_H
