#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "view/beltspeedlinewidget.h"
#include "view/coaldistributbarchartwidget.h"
#include "view/coalstatisticschartwidget.h"

#include <QDateTime>
#include <QTimer>

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);

    calendarLoopPlay();

    setBeltWarnInfoTextEditStyle();

    addcoalDistributChart();

    addbeltSpeedChat();

    addCoalStatisticsChart();
}

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

void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo)
{
    m_textEditConsoleInfo.append(consoleInfo);
    QDateTime startDateTime = QDateTime::currentDateTime();
    QString formattedTime = startDateTime.toString("yyyy-MM-dd hh:mm:ss");

    if (m_textEditConsoleInfo.isEmpty())
        return;
    QString lastConsoleInfo = m_textEditConsoleInfo.last();

    qint64 timeDifferenceInMilliseconds = m_previousConsleInfoDateTime.secsTo(startDateTime);

    if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2)
        return;

    QString consoleInfoResult = QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo);
    ui->textEdit_beltWarnInfo->append(consoleInfoResult);

    m_previousConsleInfoDateTime = QDateTime::currentDateTime();
}

void MainWindow::on_pushButton_close_clicked() { this->close(); }

void MainWindow::on_pushButton_max_clicked()
{
    if (isFullScreen()) {
        this->showMaximized();
    } else {
        this->showFullScreen();
    }
}

void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); }

void MainWindow::calendarLoopPlay()
{
    ui->label_calendar->clear();
    QTimer* timer_calendar = new QTimer(this);
    connect(timer_calendar, &QTimer::timeout, this, [this]() {
        QDateTime time = QDateTime::currentDateTime();
        QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
        ui->label_calendar->setText(str);
    });
    timer_calendar->start(1000); //每一秒溢出一次进入槽函数
}

void MainWindow::setBeltWarnInfoTextEditStyle()
{
    ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100);
    ui->textEdit_beltWarnInfo->setReadOnly(true);
    ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction);
}

void MainWindow::addcoalDistributChart()
{
    //    addBeltWarnInfoToTextEdit("添加煤流分布窗口成功！");
    ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功！");

    CoalDistributBarChartWidget* coalDistributBarChartWidget = new CoalDistributBarChartWidget(this);

    // 检查布局是否已设置
    if (!ui->widget_coalDistributChart->layout()) {
        ui->widget_coalDistributChart->setLayout(new QVBoxLayout);
    }
    coalDistributBarChartWidget->setContentsMargins(0, 0, 0, 0);
    ui->widget_coalDistributChart->layout()->addWidget(coalDistributBarChartWidget);
}

void MainWindow::addbeltSpeedChat()
{
    ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功！");
    BeltSpeedLineWidget* beltSpeedLineWidget = new BeltSpeedLineWidget(this);
    // 检查布局是否已设置
    if (!ui->widget_beltSpeedChart->layout()) {
        ui->widget_beltSpeedChart->setLayout(new QVBoxLayout);
    }
    beltSpeedLineWidget->setContentsMargins(0, 0, 0, 0);
    ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget);
}

void MainWindow::addCoalStatisticsChart()
{
    ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功！");
    CoalStatisticsChartWidget* coalStatisticsChartWidget = new CoalStatisticsChartWidget(this);
    // 检查布局是否已设置
    if (!ui->widget_beltSpeedChart->layout()) {
        ui->widget_beltSpeedChart->setLayout(new QVBoxLayout);
    }
    coalStatisticsChartWidget->setContentsMargins(0, 0, 0, 0);
    ui->widget_coalStatisticsChart->layout()->addWidget(coalStatisticsChartWidget);
}
