#ifndef CPROCESS_LASERKERF_H
#define CPROCESS_LASERKERF_H

#include "cmprocess.h"
#include "cimagecontainer.h"
#include "cshape.h"
#include <QDataStream>
#include "cprecisetime.h"
#include <QReadWriteLock>
#include <sstream>
#include "ckerf_base.h"
#include "export_laser_cali_driver.h"

/**
 * @brief 激光划线定位检测.
 */
class LASER_CALI_DRIVER_EXPORT CProcess_LaserKerf : public CMProcess
{
public:
    enum msg_id
    {
        PROCESS_Kerf_MSG_ID=MAX_PROCESS_MSG_ID,
        CALCUKERFPOS,			//<计算Kerf的位置
        MOVEKERFPOS,			//<移动到Kerf的位置
        KERF,					//<执行检测
        SETKERFPOSMANUAL,		//<手动设置Kerf位置
        TESTKERF,				//<手动测试
		CUTKERFLINE,			//<计算Kerf位置并进行切割
		MEASURE_THICKNESS,		//<测量CellArea的焦点补偿，如果没有切割会进行一次切割
		REPLACE_MARKPANEL,		//<更换打码的板子
        MAX_PROCESS_Kerf_MSG_ID,
    };
    CProcess_LaserKerf(const QString& name,int32_t id);
    QMMutexVector<KERFPARAM> m_vKerfParam;
    CMPointd m_CheckPos;    //检查的点位
	
	//计算Kerf检查的位置
	int CalcuKerfPos(double& dStartPosX, double& dEndPosX, double& dPosY);
	//移动到Kerf的位置
	int MoveKerfPos();

	//kerf检查
	int Kerf(int iSideID, int iLayerID);

	//手动更新Kerf的偏移位置
	int SetKerfPosManual(int iSideID);

	//在膜上切割Kerf
	int CutKerf(int iLaserPath, double dStartPosX, double dEndPosX, double dPosY, bool bMeasure=false, int iCellIndex=-1);

	//计算并切割
	int CutKerfLine(int iLaserPath);
	//重新进行校正
	int MeasureThickness(int iCellIndex=-1, bool bOnlyMeasure=false);

	//更换校正板
	int ReplaceMarkPanel();

	//设置关联的Kerf的参数
	void SetKerfParam();
	int32_t m_iKerfCamID;

private:
	//分析识别的结果
	void AnalyseKerfResult(int iSideID,int iLayerID,CImageContainer* pContainer);
	int CalcuMeasurePos(double& dStartPosX, double& dEndPosX, double& dPosY, int iCellIndex=-1);

	void CaptureImage(int iSideID, QString &str);
	void TestKerf();

	CImageContainer* m_imageContainer[2];	//Kerf检测的相机显示控件0 SideA
	void ClearContainer();

public:
    int32_t init_Process() override;
    void SetContainer(CImageContainer* pContainer1, CImageContainer* pContainer2);

	virtual void do_msg(int iID,QVariantList& vlInput,V_MsgOutput* pvlOutput) override;
	virtual void Serialize(QDataStream &stream, bool bwrite, int iVersion) override;

	//测量厚度
	//double MeasTargetThick(int iCellIndex);

public:
	KERFPARAM *GetKerfParam();
	QVector<int>  VectorResult;
	void SearchLine(bool_t bAutoThres);

	//dRealOffset 切割道中心与相机中心点的偏移尺寸 在中心下方为负
	//dRealHeight 第一条线的尺寸mm
	//dRealHeight1 第二条线的尺寸mm
	//dRealGAP 两条线的中心点距离 只有一条线无效
	void GetKerfResult(double& dRealOffset,
		double& dRealHeight ,
		double& dRealHeight1,
		double&  dRealGAP);
	//显示Kerf结果
	void ShowKerfResult(double  dRealOffset,
		double  dRealHeight ,
		double  dRealHeight1 ,
		double  dRealGAP,
		CImageContainer *pContainer);

	void SetKerfLight();
	//设置相机光源、曝光参数
	void SetMeasureCamParam();

	//根据WPDP的坐标，计算出线的位置的偏移并应用到激光的Offset
	double SetHairLineOffset(const CMPointd& WPDP, bool bApplyOffset);

	CPreciseTime m_KerfTime;

	int m_iLaserPathId;
	QString m_sTittleName;

	//高倍相机看到时，坐标的位置
	//double m_dFocusOffset;
	////当前的区域Index
	//int m_iCurPosIdx;

	//QReadWriteLock m_Lock;
	////! 每个区域左上角区域的位置坐标
	//std::vector<_Mark_Area> m_MarkListTopLeft;
};

#endif // CPROCESS_LASERKERF_H
