#include "Group.h"
#include "ui_Group.h"
#include <QDebug>
#include <QMouseEvent>
#include <QSettings>
#include <QVBoxLayout>
#include <QLabel>

Group::Group(int Id, const QString& imagePath1, int MaterialWindowType, const QStringList& textList, QWidget* parent)
    : QWidget(parent), ui(new Ui::Group) {
    ui->setupUi(this);

    QPixmap pixmap1(imagePath1);
    ui->Imagewidget_left->setPixmap(pixmap1);

    ui->DatacomboBox->addItems(textList);
    ui->GroupButton->setStyleSheet(   // 设置样式表
                           "QPushButton:hover {"
                           "   background-color: #45a049;"  // 悬停时的背景颜色
                           "}"
                           "QPushButton:pressed {"
                           "   background-color: #3e8e41;"  // 点击时的背景颜色
                           "}"
                           );
    ui->Imagewidget_left->setProperty("groupId", Id);
    ui->Imagewidget_right->setProperty("groupId", Id);

    if (MaterialWindowType == 1) { // 判断物料窗口类型，转到处理函数
        wafer = new Wafer(0, ui->Imagewidget_right);
        WaferWidget();
    } else if (MaterialWindowType == 2) {
        WaffleWidget();
    } else if (MaterialWindowType == 3) {
        MaterialBoxWidget();
    }

    // 存储参数到 QSettings
    saveGroupSettings(Id, imagePath1, MaterialWindowType, textList);
    connect(ui->GroupButton,&QPushButton::clicked,this,&Group::onclickbutton);
    initForm();
}

Group::~Group() {
    delete ui;
}

void Group::initForm() {
    ui->Imagewidget_left->installEventFilter(this);
    ui->Imagewidget_right->installEventFilter(this);
}

bool Group::eventFilter(QObject *obj, QEvent *event) {
    if (event->type() == QEvent::MouseButtonDblClick) {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if (mouseEvent->button() == Qt::LeftButton) {
            int groupId = obj->property("groupId").toInt();
            QSettings settings("YourCompany", "YourApplication_");
            settings.setValue("GroupId", groupId);
            if (obj == this->ui->Imagewidget_left) {
                settings.setValue("Index", 1);
            } else if (obj == this->ui->Imagewidget_right) {
                settings.setValue("Index", 2);
            }
            return true;
        }
    }
    return QWidget::eventFilter(obj, event);
}

void Group::saveGroupSettings(int Id, const QString& imagePath1, int materialWndType, const QStringList& textList) {
    QSettings settings("YourOrganization", "YourApplication");
    settings.beginGroup(QString::number(Id));
    settings.setValue("ImagePath1", imagePath1);
    settings.setValue("MaterialWndType", materialWndType);
    settings.setValue("TextList", textList);
    settings.endGroup();
}

// 圆晶
void Group::WaferWidget() {
    QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
    wafer->paintInitFrom(ui->Imagewidget_right);

    QLabel* pixmapLabel = new QLabel();
    pixmapLabel->setPixmap(wafer->getGlobalPixmap());

    ui->Imagewidget_right->setLayout(layout2);
    ui->Imagewidget_right->setFixedSize(114, 114);
    layout2->setContentsMargins(0, 0, 0, 0);
    layout2->addWidget(pixmapLabel);
}

// 华夫盒
void Group::WaffleWidget() {
    QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
    waffle = new Waffle(0, ui->Imagewidget_right);

    ui->Imagewidget_right->setLayout(layout2);
    ui->Imagewidget_right->setFixedSize(114, 114);
    layout2->setContentsMargins(0, 0, 0, 0);
    layout2->addWidget(waffle);
}

// 料盒
void Group::MaterialBoxWidget() {
    QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
    materialbox = new MaterialBox(0, ui->Imagewidget_right);

    ui->Imagewidget_right->setLayout(layout2);
    ui->Imagewidget_right->setFixedSize(114, 114);
    layout2->setContentsMargins(0, 0, 0, 0);
    layout2->addWidget(materialbox);
}

void Group::onclickbutton(){
    sendSignal();
}
