#include "MainWindowForm.h"
#include "ui_MainWindowForm.h"

MainWindowForm::MainWindowForm(QWidget *parent) :
    FramelessWindow(parent),
    ui(new Ui::MainWindowForm)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);

    initForm();
    initMenuWidgets();
    initFunctionButtons();

    // 每秒更新 界面上没有地方能摆 暂时先不启动定时器
    connect(TimeCounterUtil::getInstance().clockTimer, &QTimer::timeout, this, &MainWindowForm::onUpdateTimestampHandler);
//    TimeCounterUtil::getInstance().clockTimer->start(1000);

    /** 临时调试代码 **/
//    emit ui->btnInfo->clicked();
}

MainWindowForm::~MainWindowForm()
{
    qDebug() << "~MainWindowForm";
    delete ui;
}

void MainWindowForm::setLoginUsername(QString loginUsername)
{
    ui->labelUserName->setText(loginUsername);
}

void MainWindowForm::initForm()
{
    setWindowFlags(Qt::FramelessWindowHint); // 设置全屏
    this->setTitleBar(ui->wdgtTop);
    // 设置窗口位置和大小
    move(0, 0);

    QRect screenRect = QApplication::screens().at(0)->availableGeometry();
    qint16 screenWidth = screenRect.width();
    qint16 screenHeight = screenRect.height();
    resize(screenWidth, screenHeight);

    // 加载样式表
    QString qssStyle = QssFileUtils::loadQssFileContent(":/qss/mainForm.css");
    if (qssStyle.isEmpty() == false) {
        this->setStyleSheet(qssStyle); // 仅本窗口生效
    }

    // 设置标题
    ui->labelTitle->setText(SettingConfig::getInstance().CLIENT_TITLE);
    setWindowTitle(SettingConfig::getInstance().CLIENT_TITLE);

    // 设置最小尺寸（宽度, 高度） lxc
     QScreen *screen = QGuiApplication::primaryScreen();
     QRect screenSize = screen->availableGeometry();  // 可用区域（排除任务栏）

     // 设置最小尺寸为屏幕宽度的 80%，高度 600
     int minWidth = screenSize.width() * 0.8;
     this->setMinimumSize(minWidth, 600);
}

void MainWindowForm::initMenuWidgets()
{
//    wdgtInfo = new TF_InfoManag(this);
//    wdgtCheckMethod = new CheckMethodManage(this);
//    wdgtCheck = new CheckWindow(this);
//    wdgtData = new TF_TextDataManag(this);
//    wdgtCert = new TF_CertificateApproval(this);
//    wdgtUserMan = new TF_UserManag(this);
//    wdgtTestDev = new TF_TextDevice(this);
//    wdgtStandDev = new tf_StandardDevice(this);

//    wdgtStdDevForm = new StandardDeviceForm(this);
//    wdgtEqptDevForm = new TestDeviceForm(this);
//    wdgtFileResForm = new FileResourcesForm(this);

//    wdgtCalibrationForm = new CalibrationForm(this);

    // 绑定事件
//    connect(wdgtCalibrationForm, &CalibrationForm::enableMenuButtons, this, &MainWindowForm::onMenuButtonsEabled);

//    connect(wdgtCheck, &CheckWindow::BarsetEnabled, this, &MainWindowForm::onMenuButtonsEabled);

    // 添加到stackWidget中
//    ui->wdgtContent->insertWidget(0, wdgtCalibrationForm);
//    ui->wdgtContent->insertWidget(1, wdgtStdDevForm);
//    ui->wdgtContent->insertWidget(1, wdgtStandDev);
//    ui->wdgtContent->insertWidget(2, wdgtTestDev);
//    ui->wdgtContent->insertWidget(2, wdgtEqptDevForm);
//    ui->wdgtContent->insertWidget(3, wdgtCheckMethod);
//    ui->wdgtContent->insertWidget(4, wdgtCheck);
//    ui->wdgtContent->insertWidget(5, wdgtData);
//    ui->wdgtContent->insertWidget(6, wdgtCert);
//    ui->wdgtContent->insertWidget(7, wdgtFileResForm);
//    ui->wdgtContent->insertWidget(8, wdgtUserMan);
}

void MainWindowForm::initFunctionButtons()
{
    // 查找所有 QPushButton 类型的子控件
    QButtonGroup * funcButtGroup = new QButtonGroup(); // buttonGroup用于checked状态互斥
    QList<QToolButton *> buttList = ui->wdgtFunc->findChildren<QToolButton *>();
    for (int i = 0; i < buttList.size(); i++) {
        QToolButton * funcButt = buttList.at(i);
        funcButt->setProperty("index", i);
        funcButt->setCheckable(true);
        funcButtGroup->addButton(funcButt);
        connect(funcButt, &QToolButton::clicked, this, &MainWindowForm::onFunctionButtonsAction);
    }
    connect(this, SIGNAL(titleDblClick()), this, SLOT(titleDblClick()));
}

void MainWindowForm::on_btnMinWin_clicked()
{
    setWindowState(Qt::WindowMinimized | windowState());
}

void MainWindowForm::on_btnCloseWin_clicked()
{
    // 创建一个消息框
    int ret = CustomMessageBox::confirm(this, "退出", "确定要退出登录吗？");
    if (ret == 1) {
        LogUtil::PrintLog("INFO", QString("[%1]退出登录").arg(ProMemory::getInstance().getLoginUser().value("name").toString()));
        emit logout();
        emit close();
    }
}

void MainWindowForm::onFunctionButtonsAction()
{
    QToolButton *btn = qobject_cast<QToolButton*>(sender());
    if(btn) {
        btn->setChecked(true);
        ui->wdgtContent->setCurrentIndex(btn->property("index").toInt());

        /** 待所有的导航菜单替换完之后不再需要 **/
        // 获取当前在 QStackedWidget 中的部件
        QWidget * currentWidget = ui->wdgtContent->currentWidget();

        // 尝试将当前部件转换为 InfoBase 类型
//        Baseobject * baseInfo = qobject_cast<Baseobject *>(currentWidget);
//        // 检查转换是否成功
//        if (baseInfo != nullptr) {
//            // 如果转换成功，调用子类Load 方法
//            baseInfo->Load();
//        }
    }
}

void MainWindowForm::onUpdateTimestampHandler()
{
    QDateTime now = QDateTime::currentDateTime();
    //    ui->labelTime->setText(now.toString("yyyy-MM-dd HH:mm:ss"));
}

void MainWindowForm::on_btnMenuMax_clicked()
{
    if (this->isMaximized()) {
        this->showNormal();
    } else {
        this->showMaximized();
    }
}

void MainWindowForm::titleDblClick()
{
    on_btnMenuMax_clicked();
}

void MainWindowForm::onMenuButtonsEabled(bool enable) {
    QList<QToolButton *> menuButtonList = ui->wdgtFunc->findChildren<QToolButton *>();
    for (QToolButton * btn : menuButtonList) {
        btn->setEnabled(enable);
    }
}
