/* TagEdit.h */

/* 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/>.
 */

#ifndef TAGEDIT_H
#define TAGEDIT_H

#include <QThread>
#include <QPixmap>

#include "Utils/Pimpl.h"

class Genre;

namespace Tagging
{
	class TagReader;
	class TagWriter;
	class Editor :
		public QObject
	{
			Q_OBJECT
			PIMPL(Editor)
		signals:
			void sigStarted();
			void sigFinished();
			void sigProgress(int);
			void sigMetadataReceived(const MetaDataList& tracks);

		public:
			Editor(const std::shared_ptr<TagReader>& tagReader, const std::shared_ptr<TagWriter>& tagWriter,
			       bool doSelectiveUpdate, QObject* parent);
			Editor(const std::shared_ptr<TagReader>& tagReader, const std::shared_ptr<TagWriter>& tagWriter,
			       const MetaDataList& tracks, bool doSelectiveUpdate, QObject* parent);
			~Editor() override;

			enum FailReason
			{
				FileNotWriteable = 1,
				FileNotFound,
				TagLibError,
				NoError
			};

			void undo(int index);

			void undoAll();

			[[nodiscard]] MetaData metadata(int index) const;

			[[nodiscard]] MetaDataList metadata() const;

			bool applyRegularExpression(const QString& regex, int index);

			void addGenre(int index, const Genre& genre);

			void deleteGenre(int index, const Genre& genre);

			void renameGenre(int idx, const Genre& genre, const Genre& new_genre);

			[[nodiscard]] int count() const;

			[[nodiscard]] bool hasChanges() const;

			void updateTrack(int index, const MetaData& track);

			void updateCover(int index, const QPixmap& cover);
			[[nodiscard]] bool hasCoverReplacement(int index) const;

			void setMetadata(const MetaDataList& tracks);

			[[nodiscard]] bool isCoverSupported(int index) const;

			[[nodiscard]] bool canLoadEntireAlbum() const;
			void loadEntireAlbum();

			[[nodiscard]] QMap<QString, FailReason> failedFiles() const;

		public slots:
			void commit();

		private:
			void insertMissingArtistsAndAlbums();
			void startSameAlbumCrawler(const QString& filepath);

		private slots:
			void loadEntireAlbumFinished();
	};
}

#endif // TAGEDIT_H
