#include "CameraImageHandler.h"
#include "CameraMaterialGroupWnd/Group.h"
#include "CInterface.h"

CameraImageHandler::CameraImageHandler(int iCameraId) {
    captureAndConvertImage(iCameraId);
}

void CameraImageHandler::captureAndConvertImage(int iCameraId) {

    // CInterface interface;
    // ImageInfo image;

    // long result = interface.GrabImage(iCameraId, image);

    // if (result == 0) {
    //     initGroup(iCameraId);
    // }


     initGroup(iCameraId);
    // if (result == 0) {
    //     qDebug() << "Image width: " << image.width;
    //     qDebug() << "Image height: " << image.height;

    //     QImage img;

    //     // 根据图像格式转换
    //     switch (image.format) {
    //     case GRAY8:
    //         img = QImage(image.data, image.width, image.height, QImage::Format_Grayscale8);
    //         break;
    //     case RGB888:
    //         img = QImage(image.data, image.width, image.height, QImage::Format_RGB888);
    //         break;
    //     case ARGB8888:
    //         img = QImage(image.data, image.width, image.height, QImage::Format_ARGB32);
    //         break;
    //     case RGB32:
    //         img = QImage(image.data, image.width, image.height, QImage::Format_RGB32);
    //         break;
    //     case YUV422:

    //         break;
    //     default:
    //         qDebug() << "Unsupported image format";
    //     }
    // } else {
    //     qDebug() << "Failed to grab image, error code: " << result;
    // }

}

Group* CameraImageHandler::getGroup() const {
    return group;
}

Wafer* CameraImageHandler::getWafer() const{
    return wafer;
}

void CameraImageHandler::initGroup(int CameraId) {
    QStringList dispmodel1;
    dispmodel1 << "P die align" << "D device align";

    QStringList dispmodel2;
    dispmodel2 << "D device align" << "P die align";

    QStringList dispmodel3;
    dispmodel3 << "B device align" << "P die align";

    QStringList dispmodel4;
    dispmodel4 << "U die align" << "D device align";

    if (CameraId == 1) {
        wafer = new Wafer(0);
        group = new Group(CameraId, ":/images/test_image/image_1.png", MaterialWindowType, dispmodel1, this);
    } else if (CameraId == 2) {
        MaterialWindowType = 2;
        group = new Group(CameraId, ":/images/test_image/image_3.png", MaterialWindowType, dispmodel2, this);
    } else if (CameraId == 3) {
        MaterialWindowType = 3;
        group = new Group(CameraId, ":/images/test_image/image_5.png", MaterialWindowType, dispmodel3, this);
    } else if (CameraId == 4) {
        wafer = new Wafer(0);
        group = new Group(CameraId, ":/images/test_image/image_7.png", MaterialWindowType, dispmodel4, this);
    } else if (CameraId == 5) {
        wafer = new Wafer(0);
        group = new Group(CameraId, ":/images/test_image/image_1.png", MaterialWindowType, dispmodel1, this);
    } else if (CameraId == 6) {
        MaterialWindowType = 2;
        group = new Group(CameraId, ":/images/test_image/image_3.png", MaterialWindowType, dispmodel2, this);
    } else if (CameraId == 7) {
        MaterialWindowType = 3;
        group = new Group(CameraId, ":/images/test_image/image_5.png", MaterialWindowType, dispmodel3, this);
    } else if (CameraId == 8) {
        wafer = new Wafer(0);
        group = new Group(CameraId, ":/images/test_image/image_7.png", MaterialWindowType, dispmodel4, this);
    } else {
        group = nullptr; // 如果 CameraId 不匹配，则 group 为 nullptr
    }
}
