#ifndef CPRODUCTSIDE_H
#define CPRODUCTSIDE_H

#include "CChannel.h"
#include "cmpoint.h"
#include "logic_compdef.h"
#include <QDataStream>
#include "export_product_driver.h"
enum KerfStyle
{
	KERF_ONLY=0,
	KERF_TARGET,
    TARGET_ONLY,
};
//切割道参数
typedef struct _tagSingleCutParam
{
    double	dCutDepth;     //加工深度
	double  dOvercut;	   //过切
    double	dCutSpd;       //速度
    int		iLaserFreq;    //频率KHZ
    int     iLaserEpulse;   //Epulse频率
    double	dLaserPower;   //功率W
    int		iSleepTime;
    double	dOffset;       //所有切割道的偏移
    int		iCutCount;		//切割的次数

    int     laserpathID;	//光路
    double	dBeamWidth;			//光束的宽度
    double	dDIndex;		//其他处理 unused
 ////////KerfCheck Param
    double	dKerfExpand;    //外扩多少尺寸,跟Kerf无关
    int     iKerfStyle;     //kerf kerf&定位 定位
    int		iKerfFirst;     //首道
    int		iKerfFreq;      //频率
    int		iKerfIndex;     //kerf编号
    double  dKerfOffset;    //Kerf偏移

    _tagSingleCutParam()
    {
    dCutDepth=0;
	dOvercut=0;
    dCutSpd=200;
    iLaserFreq=50;
    iLaserEpulse = 300;
    dLaserPower=2;
    dOffset=0;
    laserpathID=LASERPATH_D_NARROW;
    dBeamWidth=0;
    iCutCount=1;

	iKerfStyle=KERF_ONLY;
    dKerfExpand=0;
    iKerfFirst=0;
    iKerfFreq=0;
    iKerfIndex=0;
    dDIndex=0;

    dKerfOffset=0;
	iSleepTime = 0;
    }

}SINGLECUTPARAM,*PSINGLECUTPARAM;
QDataStream &operator<<(QDataStream &out,const SINGLECUTPARAM& param);
QDataStream &operator>>(QDataStream &in, SINGLECUTPARAM& param);
typedef struct _sub_index
{
    double dSubIndex;
    int iCount;         //多少条线
    double dSolidOffset;    //每根实线的偏移
    double dLocationOffset;    //定位方向偏移 IndexOffset
    double dSubIndexX;  //步进    StepIndex
    double dSolidLine;  //实线长度
    _sub_index ()
    {
        dSubIndex=0;
        iCount=1;
        dSolidOffset=0;
        dLocationOffset=0;
        dSubIndexX=0;
        dSolidLine=0;
	}
}SUBINDEX,*PSUBINDEX;
QDataStream &operator<<(QDataStream &out,const SUBINDEX& param);
QDataStream &operator>>(QDataStream &in, SUBINDEX& param);

//typedef QPair<CMPointd,CMPointd> XYPAIR;
struct XYPAIR{
    CMPointd ptPostive;
    CMPointd ptNegative;
};
typedef struct _tagCutLine
{
    BOOL m_bHasCut; //is cuted
    XYPAIR m_WPPair;    //! first 起点 second 终点 first在左，second在右，first在上，second在下
    CMPointd m_BasePt; //base point
    BOOL m_bSUB;    //是否是子切割道
    double m_YOffset; //切割道y方向的偏移
    int m_index;
    _tagCutLine(){m_bHasCut=FALSE; m_bSUB=false ;m_index=0;m_YOffset=0;}
}CUTLINE,*PCUTLINE;
QDataStream &operator<<(QDataStream &out,const CUTLINE& param);
QDataStream &operator>>(QDataStream &in, CUTLINE& param);

typedef QMMutexVector<CUTLINE> CUTLINEVECTOR;

typedef struct _model_info
{
    int     iDirectLight;
    int     iSideLight;
    float64_t   dMinScore;
    int iContrast;
    int     iExposureUs;
    int iGain;
    _model_info()
    {
        iDirectLight=50;    //模型直光光源
        iSideLight=50;      //模型环光光源
        dMinScore=0.85;     //分数阈值
        iContrast=40;		//对比度
        iExposureUs = 1000;	//曝光
        iGain=0;			//增益
    }
}MODEL_INFO;

QDataStream &operator<<(QDataStream &out,const MODEL_INFO& param);
QDataStream &operator>>(QDataStream &in, MODEL_INFO& param);
class PRODUCT_DRIVER_EXPORT CProductSide : public CChannel
{
public:

    MODEL_INFO lModels[2];
    CProductSide(const QString& name,int32_t id);
    ~CProductSide();
    bool m_bModelChanged;
    virtual void Serialize(QDataStream &stream, bool bwrite, int iVersion) override;

    CUTLINEVECTOR m_CutLineVector;  //切割道
	QMMutexVector<SUBINDEX> m_vSubIndex; //子切割道
    QMMutexVector<SINGLECUTPARAM> m_vCutParam; //切割加工参数，可能会有多道

    bool GetCutParam(int iLayerID,SINGLECUTPARAM& param);
	CUTLINE *GetCutLine(  int iLine );

	MODEL_INFO &get_Model_info(int32_t iCamid);

    CMPointd m_realCenter; //实际的中心点，会调平的过程中更新
    double m_dLevelAngle;   //调平后的角度
    double m_dSolidBasePosX;

	void RecordLineData();
	bool WriteRecoveredData();
	bool ReadRecoveredData();

    void ReadRecipe(ConfigStreamReader& streamReader, int iVersion) override;
    void WriteRecipe(ConfigStreamWriter& streamWriter, int iVersion) override;

};

ConfigStreamWriter &operator<<(ConfigStreamWriter &out, const MODEL_INFO& param);
ConfigStreamReader &operator >> (ConfigStreamReader &in, MODEL_INFO& param);
ConfigStreamWriter &operator<<(ConfigStreamWriter &out, const CUTLINE& param);
ConfigStreamReader &operator >> (ConfigStreamReader &in, CUTLINE& param);
ConfigStreamWriter &operator<<(ConfigStreamWriter &out, const SUBINDEX& param);
ConfigStreamReader &operator >> (ConfigStreamReader &in, SUBINDEX& param);
ConfigStreamWriter &operator<<(ConfigStreamWriter &out, const SINGLECUTPARAM& param);
ConfigStreamReader &operator >> (ConfigStreamReader &in, SINGLECUTPARAM& param);
#endif // CPRODUCTSIDE_H
