#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QAction>
#include <QMenu>
#include <QProcess>
#include <QTreeView>
#include <QStandardItemModel>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QSplitter>
#include <QFutureWatcher>
#include <memory>
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "qsortfilterproxymodel.h"
#include "qthread.h"
#include "qtimer.h"
#include "sshmanager.h"
#include "playbookmanager.h"
#include "uimanager.h"
#include <QPropertyAnimation>
#include <QSyntaxHighlighter>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

protected:
    void closeEvent(QCloseEvent *event) override;
    bool eventFilter(QObject *obj, QEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;

private slots:
    void connectToServer();
    void disconnectFromServer();
    void runPlaybook();
    void stopPlaybook();
    void updateLog();
    void playbookFinished(int exit_status);
    void editPlaybook();
    void showOptions();
    void applyFilter();
    void changeFontSize(int size);
    void changeFont();
    void changeBackground();
    void changeForeground();
    void selectTags();
    void performKinit();
    void onPlaybookSelected(const QModelIndex &index);
    void onPlaybookDoubleClicked(const QModelIndex &index);
    void updateTheme();
    void filterPlaybooks(const QString &text);
    void setLightTheme();
    void setDarkTheme();
    void setBlueTheme();
    void toggleServerConnectionParams();
    void processLogBuffer();
    void startLogThread();
    void stopLogThread();
    void showContextMenu(const QPoint &pos);
    void addNewPlaybook();
    void deletePlaybook();
    void selectExtraVars();
    void validateExtraVars(const QString& text);

signals:
    void logAppended(const QString& text);
    void playbookExecutionFinished(int exitStatus);

private:
    void setupUI();
    void setupLogger();
    void setupSSHConnection();
    void loadSettings();
    void saveSettings();
    void updatePlaybookList();
    void appendToLog(const QString& text);
    void updateAvailableTags();
    bool loadPlaybooksFromPath(const QString& path, QStandardItem* parentItem);
    void selectPlaybookInTree(const QString& playbookPath);
    QModelIndex findPlaybookInModel(QStandardItem* parentItem, const QString& playbookPath);
    void applySettings();
    bool isWindowsServer();
    void keepAliveSSH();
    void collapseConnectionParams();
    QString applyColorRules(const QString& text);
    void filterPlaybookItem(QStandardItem *item, const QString &text);
    QSortFilterProxyModel *m_proxyModel;
    QSplitter *m_mainSplitter;
    QTreeView *m_playbookTreeView;
    QStandardItemModel *m_playbookModel;
    QTextEdit *m_logArea;
    QLineEdit *m_sudoPassword;
    QLineEdit *m_limit;
    QLineEdit *m_tags;
    QPushButton *m_runButton;
    QPushButton *m_stopButton;
    // QPushButton *m_editButton;
    QLineEdit *m_filterInput;
    QSpinBox *m_linesAfter;
    QLineEdit *m_serverHostInput;
    QSpinBox *m_serverPortInput;
    QLineEdit *m_serverUsernameInput;
    QLineEdit *m_serverPasswordInput;
    QLineEdit *m_extraVars;
    QString m_selectedPlaybook;
    QString m_defaultPlaybookPath;
    QString m_lastUsedPlaybook;
    QStringList m_availableTags;
    QString m_fullLog;
    QSize m_editDialogSize;
    std::unique_ptr<QProcess> m_process;
    ssh_session m_sshSession;
    sftp_session m_sftpSession;
    QFutureWatcher<void> m_futureWatcher;
    QTimer *m_keepAliveTimer;
    bool m_isDarkTheme;
    QColor m_defaultTextColor;
    bool m_useSudoEnabled;
    bool m_verboseEnabled;
    bool m_clearLogOnRunEnabled;
    bool m_checkModeEnabled;
    bool m_isPlaybookRunning;
    std::unique_ptr<SSHManager> m_sshManager;
    std::unique_ptr<PlaybookManager> m_playbookManager;
    std::unique_ptr<UIManager> m_uiManager;
    void createThemeActions();
    QAction *lightThemeAction;
    QAction *darkThemeAction;
    QAction *blueThemeAction;
    QWidget *m_serverConnectionWidget;
    QPushButton *m_toggleButton;
    QPropertyAnimation *m_animation;
    int m_serverConnectionWidgetHeight;
    void setupServerConnectionToggle();
    UIManager::Theme m_currentTheme;
    void updateUIState();
    void showAbout();
    QThread m_logThread;
    QMutex m_logMutex;
    QTimer m_logUpdateTimer;
    QString m_logBuffer;
    int m_maxLogLines;
    QMenu *m_contextMenu;
};

#endif // MAINWINDOW_H
