/* BookmarksMenu.cpp */

/* Copyright (C) 2011-2024 Michael Lugmair (Lucio Carreras)
 *
 * This file is part of sayonara player
 *
 * This program 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.

 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "PlaylistBookmarksMenu.h"

#include "Components/Bookmarks/Bookmarks.h"
#include "Components/Bookmarks/Bookmark.h"

#include "Gui/Plugins/PlayerPluginHandler.h"
#include "Gui/Utils/Icons.h"

#include "Utils/MetaData/MetaData.h"
#include "Utils/Language/Language.h"

namespace Playlist
{
	struct BookmarksMenu::Private
	{
		PlayerPlugin::Handler* playerPluginHandler;
		BookmarkStoragePtr bookmarkStorage{BookmarkStorage::create()};

		explicit Private(PlayerPlugin::Handler* playerPluginHandler) :
			playerPluginHandler{playerPluginHandler} {}
	};

	BookmarksMenu::BookmarksMenu(PlayerPlugin::Handler* playerPluginHandler, QWidget* parent) :
		QMenu(parent),
		m{Pimpl::make<Private>(playerPluginHandler)}
	{
		setTitle(Lang::get(Lang::Bookmarks));
	}

	BookmarksMenu::~BookmarksMenu() = default;

	void BookmarksMenu::setTrack(const MetaData& track, bool editAllowed)
	{
		m->bookmarkStorage->setTrack(track);
		bookmarksChanged(editAllowed);
	}

	MetaData BookmarksMenu::track() const { return m->bookmarkStorage->track(); }

	void BookmarksMenu::bookmarksChanged(const bool editAllowed)
	{
		const auto actions = this->actions();
		for(auto* a: actions)
		{
			a->deleteLater();
		}

		clear();

		const auto& bookmarks = m->bookmarkStorage->bookmarks();
		for(const auto& bookmark: bookmarks)
		{
			if(const auto name = bookmark.name(); !name.isEmpty())
			{
				auto* action = addAction(name);
				action->setData(bookmark.timestamp());
				connect(action, &QAction::triggered, this, &BookmarksMenu::actionPressed);
			}
		}

		addSeparator();

		auto* editAction = new QAction(Gui::icon(Gui::Edit), Lang::get(Lang::Edit), this);
		addAction(editAction);

		connect(editAction, &QAction::triggered, this, [this]() {
			m->playerPluginHandler->showPlugin("Bookmarks");
		});

		editAction->setEnabled(editAllowed);
	}

	void BookmarksMenu::actionPressed()
	{
		const auto* action = dynamic_cast<QAction*>(sender());
		const auto time = static_cast<Seconds>(action->data().toInt());

		emit sigBookmarkPressed(time);
	}
}
