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

// 静态变量初始化
Group* Group::currentlySelectedGroup = nullptr;
Group* Group::previouslySelectedBlueGroup = nullptr;
Group* Group::lastClickedGroup = nullptr;
int Group::lastClickedIndex = 0;
Group::Group(int Id, const QString& imagePath1, int MaterialWindowType, const QStringList& textList, QWidget* parent)
    : QWidget(parent), ui(new Ui::Group),currentGroupId(Id) {
    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) {
		waffle = new Waffle(0, ui->Imagewidget_right);
        WaffleWidget();
    } else if (MaterialWindowType == 3) {
		materialbox = new MaterialBox(0, ui->Imagewidget_right);
        MaterialBoxWidget();
    }

    // 存储参数到 QSettings
    saveGroupSettings(Id, imagePath1, MaterialWindowType, textList);
    connect(ui->GroupButton,&QPushButton::clicked,this,&Group::onclickbutton);
    connect(ui->DatacomboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &Group::on_DatacomboBox_currentIndexChanged);
    initForm();
	connect(this, &Group::groupSelected, this, &Group::onGroupSelected);
}

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);
			int index = 0;
            if (obj == this->ui->Imagewidget_left) {
				index = 1;
                settings.setValue("Index", 1);
				check_selected(1);
            } else if (obj == this->ui->Imagewidget_right) {
				index = 2;
                settings.setValue("Index", 2);
				check_selected(2);
            }
			emit groupSelected(this, index);
            return true;
        }
    }
    return QWidget::eventFilter(obj, event);
}

void Group::check_selected(int index)
{
    if (index == 1) {
        ui->leftBackground->setStyleSheet("border: 2px solid red;");
    } else if (index == 2) {
        ui->rightBackground->setStyleSheet("border: 2px solid red;");
    }
}
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(110, 110);
    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(110, 110);
    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(110, 110);
    layout2->setContentsMargins(0, 0, 0, 0);
    layout2->addWidget(materialbox);
}

void Group::onclickbutton(){
    emit send_button_Signal();
}

void Group::setDatacomboBox(int index){
    ui->DatacomboBox->setCurrentIndex(index);
}

void Group::on_DatacomboBox_currentIndexChanged(int index){
    send_ComboBox_singal(currentGroupId,index);
}

void Group::onGroupSelected(Group* group, int index)
{
    QSettings settings("OrganizationName__", "ApplicationName__");
    int lastSavedIndex = settings.value("lastIndex", 1).toInt();

    // 清除当前红色边框
    if (currentlySelectedGroup != nullptr) {
        currentlySelectedGroup->ui->leftBackground->setStyleSheet("");
        currentlySelectedGroup->ui->rightBackground->setStyleSheet("");
    }

    // 清除当前蓝色边框
    if (previouslySelectedBlueGroup != nullptr) {
        previouslySelectedBlueGroup->ui->leftBackground->setStyleSheet("");
        previouslySelectedBlueGroup->ui->rightBackground->setStyleSheet("");
    }

    if (lastSavedIndex == 1 || lastSavedIndex == 3) {
        // 仅当前选中设为红色，蓝色边框清空
        previouslySelectedBlueGroup = nullptr;
    } else if (lastSavedIndex == 2) {
        // 当前选中设为红色，上一个选中设为蓝色
        if (lastClickedGroup != nullptr) {
            QString blueBorderStyle = "border: 2px solid blue;";
            // 即使是同一个实例，只要索引不同就设置蓝色边框
            if (lastClickedGroup == group && lastClickedIndex != index) {
                if (lastClickedIndex == 1) {
                    lastClickedGroup->ui->leftBackground->setStyleSheet(blueBorderStyle);
                } else if (lastClickedIndex == 2) {
                    lastClickedGroup->ui->rightBackground->setStyleSheet(blueBorderStyle);
                }
                previouslySelectedBlueGroup = lastClickedGroup;
            } else if (lastClickedGroup != group) {
                if (lastClickedIndex == 1) {
                    lastClickedGroup->ui->leftBackground->setStyleSheet(blueBorderStyle);
                } else if (lastClickedIndex == 2) {
                    lastClickedGroup->ui->rightBackground->setStyleSheet(blueBorderStyle);
                }
                previouslySelectedBlueGroup = lastClickedGroup;
            }
        }
    }

    // 更新当前选中的 Group 实例
    currentlySelectedGroup = group;
    // 将当前选中的边框设为红色
    group->check_selected(index);

    // 更新上一次点击的记录
    lastClickedGroup = group;
    lastClickedIndex = index;
}

