// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu>
// SPDX-FileCopyrightText: 2026 Valentyn Bondarenko <bondarenko@vivaldi.net>

#pragma once

#include <QObject>
#include <QQmlEngine>

class NavigationController : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON

    /// This property holds the current notebook path.
    Q_PROPERTY(QString notebookPath READ notebookPath WRITE setNotebookPath NOTIFY notebookPathChanged)

    /// This property holds the current notebook name.
    Q_PROPERTY(QString notebookName READ notebookName NOTIFY notebookPathChanged)

    /// This property holds the current note path relative to the notebookPath.
    Q_PROPERTY(QString notePath READ notePath WRITE setNotePath NOTIFY notePathChanged)

    // This property holds the normalized absolute path used for note highlighting
    Q_PROPERTY(QString absoluteNotePath READ absoluteNotePath NOTIFY notePathChanged)

    /// This property holds the current note name.
    Q_PROPERTY(QString noteName READ noteName NOTIFY notePathChanged)

    /// This property holds the current note path.
    Q_PROPERTY(QUrl noteFullPath READ noteFullPath NOTIFY notePathChanged)

    /// This property holds whether we are in mobile mode.
    Q_PROPERTY(bool mobileMode READ mobileMode WRITE setMobileMode NOTIFY mobileModeChanged)

    Q_PROPERTY(bool sourceMode READ sourceMode WRITE setSourceMode NOTIFY sourceModeChanged)

public:
    explicit NavigationController(QObject *parent = nullptr);

    QString notebookPath() const;
    void setNotebookPath(const QString &notebookPath);

    QString notebookName() const;

    QString notePath() const;

    /**
     * @brief Computes a normalized absolute path for the active note.
     * Combines the notebook path and the relative note path while
     * resolving redundant separators or directory segments.
     * @return The cleaned absolute path as a string.
     */
    QString absoluteNotePath() const;

    /**
     * @brief Sets the active note and persists the selection.
     * Updates the relative path of the current note and saves it to the
     * notebook's .directory configuration file for state restoration.
     * @param notePath The relative path of the note to select.
     */
    void setNotePath(const QString &notePath);

    QString noteName() const;
    QUrl noteFullPath() const;

    bool mobileMode() const;
    void setMobileMode(bool mobileMode);

    bool sourceMode() const;
    void setSourceMode(bool sourceMode);

Q_SIGNALS:
    void notebookPathChanged();
    void notebookIconNameChanged();
    void notebookColorChanged();
    void notePathChanged();
    void mobileModeChanged();
    void sourceModeChanged();

private:
    QString m_notebookPath;
    QString m_notePath;
    bool m_mobileMode;
    bool m_sourceMode = false;
};
