#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QFrame>
#include <QList>
#include <QMainWindow>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QWidget>

#include "addressfactory.h"
#include "blastProject/blastprojectfactory.h"
#include "blastRecord/blastrecordfactory.h"
#include "blastoperationfactory.h"
#include "detInfo/detinfofactory.h"
#include "equipmentfactory.h"
#include "global.h"
#include "mqtt/mqttclient.h"
#include "serial/serialtool.h"
#include "serialGpsthread.h"
#include "worker/timeupdatethread.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

   public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void setProjectTitle(const QString &newTitle);

   signals:
    void projectTitleChanged(const QString &newTitle);

   private slots:
    void onToggleButtonClicked();
    void onButtonClicked(QPushButton *button);
    void handleStoredGNRMCData(const RMCData &data);
    void onTimeUpdated(const QString &timeString);
    void updateProjectTitleLabel(const QString &newTitle);

    void handleMqttProjectsMessage(const QByteArray &message, const QMqttTopicName &topic);

   private:
    void initializeAnimate();
    void setStyleSheets(QPushButton *selectedButton);
    void switchPage(QWidget *button);
    void initialMqttService();
    void onMqttConnected();
    void initialBtnSerial();
    void initialGPSSerial();
    void initDateTime();
    void onSerialToolCreated();
    QList<QString> findAllChildUuids(const QJsonArray &addressArray, const QString targetUuid);

   private:
    Ui::MainWindow *ui;
    SerialTool *serialTool;
    bool m_isMqttConnected = false;
    bool m_btnSerialInitialized = false;
    QPropertyAnimation *animate_leftFrame;
    QList<QWidget *> left_button_station;
    QMap<QWidget *, PageFactory *> pageFactories;
    QMap<QWidget *, QWidget *> createdPageByButton;
    TimeUpdateThread *timeThread;
    QString lat = "未知";
    QString lon = "未知";
    QPoint m_dragPosition;
    QString m_currentProjectTitle;  // Stores the current project title

    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
};
#endif  // MAINWINDOW_H
