/*
 * Fooyin
 * Copyright © 2023, Luke Taylor <luket@pm.me>
 *
 * Fooyin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Fooyin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Fooyin.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#pragma once

#include <gui/fywidget.h>

#include <QPersistentModelIndex>

class QJsonObject;
class QTreeView;

namespace Fooyin {
class ActionManager;
class Command;
class PlayerController;
class PlaylistHandler;
class PlaylistInteractor;
class PlaylistOrganiserModel;
class SettingsManager;
class WidgetContext;

class PlaylistOrganiser : public FyWidget
{
    Q_OBJECT

public:
    explicit PlaylistOrganiser(ActionManager* actionManager, PlaylistInteractor* playlistInteractor,
                               SettingsManager* settings, QWidget* parent = nullptr);
    ~PlaylistOrganiser() override;

    [[nodiscard]] QString name() const override;
    [[nodiscard]] QString layoutName() const override;
    void saveLayoutData(QJsonObject& layout) override;
    void loadLayoutData(const QJsonObject& layout) override;

    struct ConfigData
    {
        QString leftScript;
        QString rightScript;
        QString playingTextColour;
        QString playingBackgroundColour;
    };

    [[nodiscard]] ConfigData factoryConfig() const;
    [[nodiscard]] ConfigData defaultConfig() const;
    [[nodiscard]] const ConfigData& currentConfig() const;
    void saveDefaults(const ConfigData& config) const;
    void clearSavedDefaults() const;
    void applyConfig(const ConfigData& config);

protected:
    void contextMenuEvent(QContextMenuEvent* event) override;
    void openConfigDialog() override;

private:
    [[nodiscard]] QModelIndex actionIndex() const;
    [[nodiscard]] QModelIndexList actionIndexes() const;
    void selectionChanged();
    void selectCurrentPlaylist();
    void createGroup(const QModelIndex& index) const;
    void createPlaylist(const QModelIndex& index, bool autoPlaylist);
    void editAutoPlaylist(const QModelIndex& index);

    void filesToPlaylist(const QList<QUrl>& urls, const UId& id);
    void filesToGroup(const QList<QUrl>& urls, const QString& group, int index);
    void filesToGroupOrdered(const QList<QUrl>& urls, const QString& group, int index);
    void importPlaylists(const QList<QUrl>& urls, const QString& group, int index);
    void tracksToPlaylist(const std::vector<int>& trackIds, const UId& id);
    void tracksToGroup(const std::vector<int>& trackIds, const QString& group, int index);
    [[nodiscard]] ConfigData configFromLayout(const QJsonObject& layout) const;
    static void saveConfigToLayout(const ConfigData& config, QJsonObject& layout);

    ActionManager* m_actionManager;
    SettingsManager* m_settings;
    PlaylistInteractor* m_playlistInteractor;
    PlayerController* m_playerController;

    QTreeView* m_organiserTree;
    PlaylistOrganiserModel* m_model;

    WidgetContext* m_context;

    QAction* m_removePlaylist;
    Command* m_removeCmd;
    QAction* m_renamePlaylist;
    Command* m_renameCmd;
    QAction* m_newGroup;
    Command* m_newGroupCmd;
    QAction* m_newPlaylist;
    Command* m_newPlaylistCmd;
    QAction* m_newAutoPlaylist;
    Command* m_newAutoPlaylistCmd;
    QAction* m_editAutoPlaylist;
    Command* m_editAutoPlaylistCmd;

    QAction* m_sortAllPlaylists;
    Command* m_sortAllPlaylistsCmd;

    QAction* m_sortGroupPlaylists;
    Command* m_sortGroupPlaylistsCmd;

    ConfigData m_config;
    QPersistentModelIndex m_contextMenuIndex;
    UId m_currentPlaylistId;
    bool m_creatingPlaylist{false};
};
} // namespace Fooyin
