#pragma once

#include "server_widget.hh"
#include "command_processor.hh"
#include "database.hh"
#include "../TradeCore/message.hh"
#include <QMainWindow>
#include <QSystemTrayIcon>

class QMenu;
class Logger;

namespace Ui {
class MainWindow;
}

/**
 * @brief The server main window that can be minimized to tray
 *
 * Server window displays log of server and clients activities,
 * list of active connections, list of registered users and list
 * of loaded modules (trade strategies) and last part of window
 * is dedicated to recording database.
 */
class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    void checkApplicationDirectory();
    void initSystray();
    void connectToDatabase();
    void connectSignalsSlots();
    void startServer();
    void appendLog(const QString& str, bool error = false);
    void closeEvent(QCloseEvent* event) override;

private slots:
    void onConnectionRegistered(
        int clientId, const Registration& registration);

    void onConnectionClosed(int clientId);
    void onTrayActivated(QSystemTrayIcon::ActivationReason reason);
    void onActionExit();

private:
    Ui::MainWindow* ui_;
    QSystemTrayIcon* tray_;
    QMenu* trayMenu_;
    std::unique_ptr<Logger> logger_;
    std::shared_ptr<Database> db_;
};
