#include "Wafer.h"
#include <QPainter>
#include <QPaintEvent>
#include <cmath>
#include <QDebug>
#include <QPushButton>
#include <QGridLayout>

Wafer::Wafer(int flag, QWidget *parent) : QWidget(parent) {
    Flag = flag;
    rows = 51;
    cols = 51;
    centerX = 25;
    centerY = 25;
    radius = 25;

    // 随机数初始化
    std::srand(std::time(nullptr)); // 使用当前时间作为种子

    // 遍历矩阵,按行列判断是否在圆形区域内
    for (int row = 0; row < rows; ++row) {
        for (int col = 0; col < cols; ++col) {
            // 计算当前点到中心点的距离
            double dx = col - centerX;
            double dy = row - centerY;
            double distance = std::sqrt(dx * dx + dy * dy);

            // 如果点在圆形区域外部,跳过
            if (distance > radius) {
                continue;
            }

            // 创建一个新的WAFER_MATRIX_POINT_INFO_STRUCT
            ns_mat::WAFER_MATRIX_POINT_INFO_STRUCT point;
            point.nDieMatrixId = 1; // 假设所有点属于同一晶圆矩阵
            point.nDieRow = row;
            point.nDieCol = col;
            point.iDieIndex = row * cols + col; // 假设ID为行列号的线性映射
            point.bDisable = false; // 默认为可用
            point.stPosition.x = col * 10.0;
            point.stPosition.y = row * 10.0;

            // 判断该点是否在圆形区域的边缘
            if (distance > radius - 1 && distance < radius + 1) {
                point.eStatus = ns_mat::PICK_DIE_STATUS::EDGE_DIE; // 如果在边缘区域,状态设置为EDGE_DIE
            } else {
                // 如果在圆形区域内,随机设置其他状态
                int randomStatus = std::rand() % 4; // 随机选取DIE_EXIST, PICK_ING, NO_EXIST, SKIP_DIE
                point.eStatus = static_cast<ns_mat::PICK_DIE_STATUS>(randomStatus);
            }

            // 将点添加到waferData容器中
            waferData.append(point);
        }
    }
}

QColor Wafer::getColorByStatus(ns_mat::PICK_DIE_STATUS status) {
    switch (status) {
    case ns_mat::PICK_DIE_STATUS::DIE_EXIST: return QColor(0, 102, 255); // 蓝色
    case ns_mat::PICK_DIE_STATUS::NO_EXIST: return QColor(200, 200, 200); // 浅灰
    case ns_mat::PICK_DIE_STATUS::PICK_ING: return QColor(255, 255, 0); // 黄色
    case ns_mat::PICK_DIE_STATUS::SKIP_DIE: return QColor(128, 128, 128); // 深灰
    case ns_mat::PICK_DIE_STATUS::EDGE_DIE: return QColor(255, 165, 0); // 橙色
    default: return QColor(0, 0, 0); // 默认黑色
    }
}

void Wafer::paintInitFrom(QWidget *parent) {

    // 获取当前窗口的宽高
    int width = parent->width();
    int height = parent->height();
    // 根据行列数计算每个晶圆点的大小,选择小的边来决定
    int cellSize = qMin(width, height) / qMax(rows, cols); // 固定大小为正方形,按最小边计算
    // 计算左上角偏移量,居中显示
    int offsetX = (width - cellSize * cols) / 2;
    int offsetY = (height - cellSize * rows) / 2;

    // 创建一个 QPixmap 对象用于保存绘制的图像
    globalPixmap = QPixmap(width, height);
    globalPixmap.fill(Qt::white);  // 填充背景色为白色

    // 创建 QPainter 以绘制到 QPixmap 上
    QPainter painter(&globalPixmap);
    painter.setRenderHint(QPainter::Antialiasing); // 启用抗锯齿

    // 设置画笔为无边框
    painter.setPen(Qt::NoPen);

    // 绘制每个晶圆点
    for (int i = 0; i < waferData.size(); ++i) {
        int x = offsetX + waferData[i].nDieCol * cellSize;
        int y = offsetY + waferData[i].nDieRow * cellSize;

        // 根据点的状态设置颜色
        painter.setBrush(getColorByStatus(waferData[i].eStatus));

        // 绘制晶圆点(每个点是一个矩形)
        painter.drawRect(x, y, cellSize, cellSize);
    }

    // 根据角度绘制黑点
    int centerX, centerY;
    int angle = 0;
    switch (angle) {
    case 0: {
        centerX = offsetX + (cols / 2) * cellSize;
        centerY = offsetY + (rows - 1) * cellSize;
        break;
    }
    case 90: {
        centerX = offsetX;
        centerY = offsetY + (rows / 2) * cellSize;
        break;
    }
    case 180: {
        centerX = offsetX + (cols / 2) * cellSize;
        centerY = offsetY;
        break;
    }
    case 270: {
        centerX = offsetX + (cols - 1) * cellSize;
        centerY = offsetY + (rows / 2) * cellSize;
        break;
    }
    default:
        return;
    }

    painter.setBrush(Qt::black);
    painter.drawEllipse(centerX - 3, centerY - 3, 6, 6);
    painter.end(); // 结束绘制
}

void Wafer::initFrom(QWidget *parent) {
    scene = new QGraphicsScene(parent);
    view = new WaferGraphicsView(scene);

    // 获取当前窗口的宽高
    int width = parent->width();
    int height = parent->height();

    // 根据行列数计算每个晶圆点的大小,选择小的边来决定
    int cellSize = qMin(width, height) / qMax(rows, cols); // 固定大小为正方形,按最小边计算

    for (int i = 0; i < waferData.size(); ++i) {
        DieItem* die = new DieItem(waferData[i].nDieRow, waferData[i].nDieCol, waferData[i].eStatus, cellSize);
        die->setPos(waferData[i].nDieCol * cellSize, waferData[i].nDieRow * cellSize);
        scene->addItem(die);
    }

    view->setSceneRect(scene->itemsBoundingRect());
    view->resize(width, height);
}

QPixmap Wafer::getGlobalPixmap() const {
    return globalPixmap;
}