#ifndef CPROCESS_SCAN_H
#define CPROCESS_SCAN_H

#include "cmprocess.h"
#include "cmotordriver.h"
/*!
 * \brief The CProcess_Scan class
 * 料盒处理流程
 */
class CProcess_Scan : public CMProcess {
public:
    enum msg_id {
        CASSETTEDRIVER_MSG_ID = MAX_PROCESS_MSG_ID,
        CASSSCAN,   //扫描料盒
        CASSUP,     //料盒升起
        MOVESLOTSCANPOS,    //料盒移动到扫描位
        MOVESLOTLOADPOS,    //SLOT移动到上料位
        MOVESLOTUNLOADPOS,  //料盒SLOT移动到下料位
        MOVECHECKBOXLOADPOS,    //UNUSED
        MOVECHECKBOXUNLOADPOS,  //UNUSED
        MAX_CASSETTEDRIVER_MSG_ID,

    };
    CProcess_Scan(const QString& name, int32_t id);

    QList<double> m_lScanPos;
    void do_msg(int iID, QVariantList& vlInput, V_MsgOutput* pvlOutput);
public:
    /*!
     * \brief CProcess_Scan::CassScan
     * 扫描料盒
     * \param cassid
     * \param iStep
     * \return
     */
    bool CassScan(int32_t cassid, int iStep);
    /*!
    * \brief CProcess_Scan::CassUp
    * 料盒升起
    * \param cassid
    */
    void CassUp(int32_t cassid);
    /**
     * \brief CProcess_Scan::MoveSlotScanPos 料盒slotindex移动到扫描位.
     * 
     * \param cassid
     * \param slotindex from 0 to max 
     * \param slotindex not support -1
     * \return 
     */
    int MoveSlotScanPos(int32_t cassid, int32_t slotindex);
    /**
     * \brief CProcess_Scan::MoveSlotLoadPos
     * 移动到加载位置
     * \param cassid
     * \param slotindex 需要移动的SLOT编号  from(0,max) -1则移动到Checkbox
     * \param bWait
     * \param bAuto
     * \return
     */
    int MoveSlotLoadPos(int32_t cassid, int32_t slotindex, bool bWait = true, bool bAuto = true);
    /**
     * \brief CProcess_Scan::MoveSlotUnLoadPos
     * 移动到下料的位置
     * \param cassid
     * \param slotindex 需要移动的SLOT编号  from(0,max) -1则移动到Checkbox
     * \param bWait
     * \param bAuto
     * \return
     */
    int MoveSlotUnLoadPos(int32_t cassid, int32_t slotindex, bool bWait = true, bool bAuto = true);
private:
    /**
     * \brief CProcess_Scan::slotunLoadPos 计算取料位置.
     * 通过slotload + 下料偏移Var_Cass_UnloadOffset_ID实现
     * \sa slotLoadPos
     * \param cassid
     * \param slotindex
     * \param bAuto 是否自动状态
     * \return 
     */
    float64_t slotunLoadPos(int32_t cassid, int32_t slotindex, bool bAuto=true);
    /**
     * \brief CProcess_Scan::slotLoadPos 计算放料位置.
     *
     * \param cassid
     * \param slotindex
     * \param bAuto 是否自动状态
     * \return 
     */
    float64_t slotLoadPos(int32_t cassid, int32_t slotindex, bool bAuto = true);
public:
    BOOL CCassExist(int32_t cassid);
    int ScanMoveCheck();
    BOOL IsCheckCellExist();
private:
    int MoveCassScanReady();
    void RecordToFile(const QList<double>& list, QString fileName);
    void FileToRecord(QList<double>& list, QString fileName);
    void RecordToFile(const QList<int>& list, QString fileName);

    void ScanStep(int cassid, int iStep);

    void ScanTwoSensor(int cassid);

    void AfterScan(int cassid);
    //在料盒运动中，当晶圆触发传感器时，获取所有数据
    void GetSensorData(
        QList<double>& scanExistList1,
        QList<double>& scanExistList2,
        QList<double>& scanEmptyList1,
        QList<double>& scanEmptyList2,
        CMotorDriver* pDriver,
        double posStart,
        double posEnd);

    //scanCenterList 以第一个传感器为准
    /**
     * \brief CProcess_Scan::ComputeWaferCenter 计算Wafer中心位置.
     * 
     * \param scanExistList1 传感器左有信号时轴的坐标数据
     * \param scanExistList2 传感器右有信号时轴的坐标数据
     * \param scanEmptyList1 传感器左无信号时轴的坐标数据
     * \param scanEmptyList2 传感器右无信号时轴的坐标数据
     * \param sensorOffset   传感器补偿 右+sensorOffset
     * \param waferThickness Wafer片厚
     * \param scanCenterList 有Wafer时中心计算出的坐标数据
     * \param scanResultList
     * \param dOffset 扫片延迟补偿，采集IO信号时的延迟，往正方向扫描为负值，往负方向扫描为正值
     */
    void ComputeWaferCenter(QList<double>& scanExistList1,
                            QList<double>& scanExistList2,
                            QList<double>& scanEmptyList1,
                            QList<double>& scanEmptyList2,
                            double sensorOffset,
                            double waferThickness,
                            QList<double>& scanCenterList,
                            QList<int>& scanResultList,
                            double dOffset);

    bool SimulatorScan(int cassid, int iStep);
};

#endif // CPROCESS_SCAN_H
