#include "OriginalWnd.h"
#include "Login.h"
#include "ui_OriginalWnd.h"
#include "OriginalWnd/ChartsAndCamerasWnd.h"
#include "OriginalWnd/MainAndSecondaryCamerasWnd.h"
#include "OriginalWnd/SingleCameraOperationWnd.h"
#include "OriginalWnd/DbTreeViewManager.h"

#include <QDebug>
#include <QJsonDocument>
#include <QFile>
#include <QFileDialog>
#include <QDateTime>
#include <QTimer>
#include <QSettings>
#include <QMouseEvent>
#include <QDir>

OriginalWnd::OriginalWnd(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::OriginalWnd),
    dbTreeViewManager(nullptr) // 初始化为 nullptr
{
    ui->setupUi(this);
    if(gen_if.getLanguageValue() == 0){

    }else{
        setChineseMode(this);
    }
    // 确保 ui->widget_2 已经初始化
    if (!ui->widget_2) {
        qWarning() << "ui->widget_2 未初始化";
    }

    // 初始化 DbTreeViewManager，传递两个参数
    dbTreeViewManager = new DbTreeViewManager(this, ui->widget_2, this);

    // 将 DbTreeViewManager 添加到 UI
    // 假设 ui->widget_2 是你原来放 TreeViewManager 的地方
    QVBoxLayout *layout = new QVBoxLayout(ui->widget_2);
    layout->addWidget(dbTreeViewManager);
    ui->widget_2->setLayout(layout);

    // 初始化界面
    initForm();

    // 假设默认加载 Home 配置，权限等级为 "1"
    // 你需要根据实际登录用户的权限获取
    int defaultConfigId = 1; // Home_config.json 对应的 ConfigId
    int userPrivilege = 0x1; // 初始设为 1，后续通过登录获取
    dbTreeViewManager->initializeTree(defaultConfigId, userPrivilege);




    qDebug() << "UI 初始化完成";

}



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

QWidget* OriginalWnd::getWidgetLeft() const
{
    return ui->widget;
}


void OriginalWnd::switchToHomeConfig()
{
    int configId = 1; // Home_config.json 对应的 ConfigId
    int userPrivilege = 0x1; // 初始设为 1，后续通过登录获取
    dbTreeViewManager->initializeTree(configId, userPrivilege);
}

void OriginalWnd::switchToProductionConfig()
{
    int configId = 2; // Production_config.json 对应的 ConfigId
    int userPrivilege = 0x1; // 初始设为 1，后续通过登录获取
    dbTreeViewManager->initializeTree(configId, userPrivilege);
    dbTreeViewManager->currentMenuId = 2;
    if(dbTreeViewManager->menuArray[0].isthird == true){
        dbTreeViewManager->loadpage(configId);
    }else{

    }
}

void OriginalWnd::switchToDiagnosisConfig()
{
    int configId = 3; // Diagnosis_config.json 对应的 ConfigId
    int userPrivilege = 0x1; // 初始设为 1，后续通过登录获取
    dbTreeViewManager->initializeTree(configId, userPrivilege);
    dbTreeViewManager->currentMenuId = 3;
    if(dbTreeViewManager->menuArray[2].isthird == true){
        dbTreeViewManager->loadpage(configId);
    }else{

    }
}

void OriginalWnd::switchToDispositionConfig()
{
    int configId = 6; // Disposition_config.json 对应的 ConfigId
    int userPrivilege = 0x1; // 初始设为 1，后续通过登录获取
    dbTreeViewManager->initializeTree(configId, userPrivilege);
    dbTreeViewManager->currentMenuId = 6;
    if(dbTreeViewManager->menuArray[4].isthird == true){
        dbTreeViewManager->loadpage(configId);
    }else{

    }
}

void OriginalWnd::switchToModuleConfig()
{
    int configId = 4; // Module_config.json 对应的 ConfigId
    int userPrivilege = 0x1; // 初始设为 1，后续通过登录获取
    dbTreeViewManager->initializeTree(configId, userPrivilege);
    dbTreeViewManager->currentMenuId = 4;
    if(dbTreeViewManager->menuArray[3].isthird == true){
        dbTreeViewManager->loadpage(configId);
    }else{

    }
}

void OriginalWnd::switchToProgramConfig()
{
    int configId = 5; // Program_config.json 对应的 ConfigId
    int userPrivilege = 0x1; // 初始设为 1，后续通过登录获取
    dbTreeViewManager->initializeTree(configId, userPrivilege);
    dbTreeViewManager->currentMenuId = 5;
    qDebug()<<dbTreeViewManager->menuArray[1].isthird;
    if(dbTreeViewManager->menuArray[1].isthird == true){
        dbTreeViewManager->loadpage(configId);
    }else{

    }
}
void OriginalWnd::setChineseMode(QMainWindow *OriginalWnd){

        OriginalWnd->setWindowTitle(QCoreApplication::translate("OriginalWnd", "主界面", nullptr));
        ui->stopButton->setText(QCoreApplication::translate("OriginalWnd", "停止"
                                                                       , nullptr));
        ui->startButton->setText(QCoreApplication::translate("OriginalWnd", "启动"
                                                                        , nullptr));
        ui->ChartButton->setText(QString());
        ui->DoubleImageButton->setText(QString());
        ui->SingleImageButton->setText(QString());
        // ui->pushButton_18->setText(QCoreApplication::translate("OriginalWnd", "PushButton", nullptr));
        ui->label_name->setText(QCoreApplication::translate("OriginalWnd", "姓名001", nullptr));
        ui->RegistrationNumber->setText(QCoreApplication::translate("OriginalWnd", "注册号码 001", nullptr));
        ui->CalibrationButton->setText(QCoreApplication::translate("OriginalWnd", "校准", nullptr));
        ui->ProductIotNumber->setText(QCoreApplication::translate("OriginalWnd", "xxx生产编号", nullptr));
        ui->pushButton_2->setText(QString());
        ui->pushButton_3->setText(QString());
        ui->label_time->setText(QString());
        ui->label_username->setText(QString());
        ui->label_userlogo->setText(QString());

}
void OriginalWnd::switchToSystemConfig()
{
    int configId = 7; // System_config.json 对应的 ConfigId
    int userPrivilege = 0x1; // 初始设为 1，后续通过登录获取
    dbTreeViewManager->initializeTree(configId, userPrivilege);
}


void OriginalWnd::initForm()
{
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &OriginalWnd::updateTime);
    timer->start(100);
    //给stopbutton加样式
    QString stopButton_styleSheet = "QToolButton:hover { background: #cc0000; }"
                                    "QToolButton:pressed { background: #990000; }";
    ui->stopButton->setStyleSheet(stopButton_styleSheet);
    ui->stopButton->setIcon(QIcon(":/images/light/stop.png"));
    ui->startButton->setIcon(QIcon(":/images/light/start.png"));

    ui->label_userlogo->setPixmap(QPixmap(":/images/TopStatusBar/user.png").scaled(20, 20, Qt::KeepAspectRatio));

    QSettings settings("OrganizationName__", "ApplicationName__");
    int lastSavedIndex = settings.value("lastIndex", 1).toInt();
    if (lastSavedIndex == 1) {
        ChartsAndCamerasWnd *chartsAndCamerasWnd = new ChartsAndCamerasWnd;
        ui->switchShowPageUI->addWidget(chartsAndCamerasWnd);
        ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(chartsAndCamerasWnd));
        // ui->ChartButton->setStyleSheet("background-color: #A9B4FF;");
		ui->ChartButton->setChecked(true);
    }
    if (lastSavedIndex == 2) {
        MainAndSecondaryCamerasWnd *mainAndSecondaryCamerasWnd = new MainAndSecondaryCamerasWnd;
        ui->switchShowPageUI->addWidget(mainAndSecondaryCamerasWnd);
        ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(mainAndSecondaryCamerasWnd));
        // ui->DoubleImageButton->setStyleSheet("background-color: #A9B4FF;");
		ui->DoubleImageButton->setChecked(true);
    }
    if (lastSavedIndex == 3) {
        SingleCameraOperationWnd *singleCameraOperationWnd = new SingleCameraOperationWnd;
        ui->switchShowPageUI->addWidget(singleCameraOperationWnd);
        ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(singleCameraOperationWnd));
        // ui->SingleImageButton->setStyleSheet("background-color: #A9B4FF;");
		ui->SingleImageButton->setChecked(true);
    }


    ui->label_userlogo->installEventFilter(this);
    ui->label_username->installEventFilter(this);
}

void OriginalWnd::updateTime() {
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString timeStr = currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
    ui->label_time->setText(timeStr);

    QSettings settings("YourCompany_2", "YourAppName_2");
    QString userName = settings.value("userName", "???").toString();
    ui->label_username->setText(userName);
}

bool OriginalWnd::eventFilter(QObject *obj,QEvent *event)
{
    if (obj == this->ui->label_userlogo || obj == this->ui->label_username)
    {
        //判断事件类型是否为鼠标事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            //转换为鼠标事件
            QMouseEvent* mouseenevt = static_cast<QMouseEvent*>(event);
            //判断鼠标左键点击
            if (mouseenevt->button() == Qt::LeftButton)
            {
                if (QStackedWidget *parent = qobject_cast<QStackedWidget*>(this->parentWidget())) {
                    Login *loginWnd = new Login(this);
                    parent->addWidget(loginWnd);
                    parent->setCurrentIndex(parent->indexOf(loginWnd));
                }
            }
        }
    }
    return QWidget::eventFilter(obj, event);
}

void OriginalWnd::on_ChartButton_clicked()
{
    ChartsAndCamerasWnd *demo001_1 = new ChartsAndCamerasWnd;
    ui->switchShowPageUI->addWidget(demo001_1);
    ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_1));
    QSettings settings("OrganizationName__", "ApplicationName__");
    settings.setValue("lastIndex", 1);
}

void OriginalWnd::on_DoubleImageButton_clicked()
{
    MainAndSecondaryCamerasWnd *demo001_2 = new MainAndSecondaryCamerasWnd;
    ui->switchShowPageUI->addWidget(demo001_2);
    ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_2));
    QSettings settings("OrganizationName__", "ApplicationName__");
    settings.setValue("lastIndex", 2);
}

void OriginalWnd::on_SingleImageButton_clicked()
{
    SingleCameraOperationWnd *demo001_3 = new SingleCameraOperationWnd;
    ui->switchShowPageUI->addWidget(demo001_3);
    ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_3));
    QSettings settings("OrganizationName__", "ApplicationName__");
    settings.setValue("lastIndex", 3);
}

void OriginalWnd::on_startButton_clicked()
{
    QString styleSheet = "QToolButton:disabled { color: #808080; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #F8FBFF, stop: 1 #E7ECF3); }"; // 设置禁用状态下的文本颜色为灰色
    ui->startButton->setDisabled(true);
    ui->startButton->setStyleSheet(styleSheet);
}

void OriginalWnd::on_stopButton_clicked()
{
    QString styleSheet1 = "QToolButton { background: #4AB832; color: #FFFFFF; }"
                          "QToolButton:hover { background: #00FF00; }";
    ui->startButton->setDisabled(false);
    ui->startButton->setStyleSheet(styleSheet1);
}
void OriginalWnd::onHandleLanguageSignal(const int &data){
    if(data == 0){
        ui->retranslateUi(this);
    }else{
        setChineseMode(this);
    }
}
