#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->btnCalibration->clicked();
}

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

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

void MainWindowForm::initForm()
{
    setWindowFlags(Qt::FramelessWindowHint); // 设置全屏
    setWindowState(Qt::WindowState::WindowMaximized);

    // 设置窗口位置和大小
    move(0, 0);

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

    // 设置标题栏 以绑定和触发鼠标双击事件
    this->setTitleBar(ui->wdgtTop);
    connect(this, &MainWindowForm::titleDblClick, this, &MainWindowForm::titleDblClickHandler);

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

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

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

void MainWindowForm::initMenuWidgets()
{
    wdgtCaliMain = new CalibrationMainForm(this);
    wdgtCheckMain = new CheckMainForm(this);
    wdgtInfoMain = new InfoMainForm(this);

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

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

    // 添加到stackWidget中
    ui->wdgtContent->insertWidget(0, wdgtCaliMain);
    ui->wdgtContent->insertWidget(1, wdgtCheckMain);
    ui->wdgtContent->insertWidget(2, wdgtInfoMain);
}

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);
    }
}

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

void MainWindowForm::on_btnMaxWin_clicked()
{
    if (this->isMaximized()) {
        this->showNormal();
        ui->btnMaxWin->setToolTip("最大化");
    } else {
        this->showMaximized();
        ui->btnMaxWin->setToolTip("还原");
    }
}

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());
    }
}

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

void MainWindowForm::titleDblClickHandler()
{
    on_btnMaxWin_clicked();
}

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