//
// Created by arjun-flowylabs on 1/10/2026.
//

#ifndef LLINK_MAINWINDOW_H
#define LLINK_MAINWINDOW_H

#include "store.h"
#include "networkstreammodel.h"
#include "particlelistmodel.h"
#include <QMainWindow>
#include <QUrl>

QT_BEGIN_NAMESPACE
class QSystemTrayIcon;
class AuthManager;
class QItemSelection;
class QLabel;
class QSplitter;
class QTreeView;
class QListView;
class QListWidget;
class QToolButton;
class QPushButton;
class QStatusBar;
class QStackedWidget;
QT_END_NAMESPACE

class AuthDialog;
class CreateStreamDialog;
class MemberPickerDialog;
class NetworkDetailWidget;
class SettingsDialog;
class TextInputDialog;
class MediaHandler;
class MediaPreviewDialog;
class MediaParticleGenerator;
class TextParticleWidget;
class MediaParticleWidget;

class MainWindow : public QMainWindow
{
  Q_OBJECT

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

  ~MainWindow();

private slots:
  void on_exitButton_clicked();
  void on_settingsButton_clicked();

  void onSignedIn();
  void onParticleUpdated(const QString &streamId, const QString &particleId);
  void onNewStreamClicked();
  void onCreateStreamRequested(const QString &name, const QString &visibility, const QStringList &members);
  void onAddMembersClicked();
  void onNetworksChanged();
  void onAddNetworkMemberRequested(const QString &networkId, const QString &email);

  void onTreeSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
  void onParticleSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
  void onTextMessageSubmitted(const QString &text);

protected:
  bool eventFilter(QObject *obj, QEvent *event) override;
  void keyPressEvent(QKeyEvent *event) override;
  void keyReleaseEvent(QKeyEvent *event) override;
  void mousePressEvent(QMouseEvent *event) override;
  void mouseMoveEvent(QMouseEvent *event) override;
  void mouseReleaseEvent(QMouseEvent *event) override;

private:
  void buildUi();
  void setupTrayIcon();
  void setupModels();

  QSystemTrayIcon *m_trayIcon;

  // ── UI widgets ──
  QWidget *m_topBar;
  QSplitter *m_splitter;
  QToolButton *m_settingsButton;
  QToolButton *m_exitButton;
  QTreeView *m_treeView;
  QPushButton *m_newStreamButton;
  QStackedWidget *m_contentStack;
  TextParticleWidget *m_textParticleWidget;
  MediaParticleWidget *m_mediaParticleWidget;
  QLabel *m_creatorLabel;
  QLabel *m_ackLabel;
  QListView *m_particlesView;
  QListWidget *m_membersList;
  QToolButton *m_sendTextButton;
  QToolButton *m_addMembersButton;
  QStatusBar *m_statusBar;

  QWidget *m_leftSidebar;
  QWidget *m_contentView;
  QWidget *m_rightSidebar;
  NetworkDetailWidget *m_networkDetailWidget;

  // ── Auth & dialogs ──
  AuthManager *m_authManager;
  AuthDialog *m_authDialog;

  SettingsDialog *m_settingsDialog;
  TextInputDialog *m_textInputDialog;
  CreateStreamDialog *m_createStreamDialog;

  // Data and models
  Store *m_store;
  NetworkStreamModel *m_networkStreamModel;
  ParticleListModel *m_particleListModel;

  void showTextInputDialog();
  void updateStatusBar();
  QString m_selectedParticleId;
  QString m_selectedNetworkId;
  QString m_selectedStreamId;
  int m_selectedParticleRow;
  QLabel *m_currentPathLabel;
  QLabel *m_positionLabel;
  QLabel *m_storageLabel;
  QLabel *m_pingLabel;
  QTimer *m_pingTimer;

  // Window dragging
  QPoint m_dragPosition;
  bool m_isDragging;

  MediaHandler *m_media;
  MediaPreviewDialog *m_mediaPreviewDialog;
  MediaParticleGenerator *m_mediaParticleGenerator;
  bool m_spaceHeld;

  // Temporary recording state
  QUrl m_recordedFileUrl;
  qint64 m_recordedDurationMs = 0;
  QString m_recordedMimeType;
};

#endif // LLINK_MAINWINDOW_H
