/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
    Copyright 2000-2026 the Rosegarden development team.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    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 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef RG_AUDIOMANAGERDIALOG_H
#define RG_AUDIOMANAGERDIALOG_H

#include "sound/AudioFile.h"
#include "misc/ConfigGroups.h"
#include "gui/general/ActionFileClient.h"
#include "gui/widgets/WarningGroupBox.h"
#include "base/Selection.h"

#include <QMainWindow>

class QWidget;
class QTimer;
class QString;
class QTreeWidget;
class QTreeWidgetItem;
class QLabel;
class QDropEvent;
class QCloseEvent;
class QUrl;

#include <set>


namespace Rosegarden
{


class AudioListView;
class Segment;
class RosegardenDocument;
class RealTime;
class AudioPlayingDialog;
class AudioFile;
class WAVAudioFile;
class Command;

/// Audio File Manager
/**
 * ??? This does not use the command history and the user can end up in
 *     a really bad situation.  Need to implement all of this functionality
 *     as commands so they can be undone.
 */
class AudioManagerDialog : public QMainWindow, public ActionFileClient
{
    Q_OBJECT

public:
    AudioManagerDialog(QWidget *parent,
                       RosegardenDocument *doc);
    ~AudioManagerDialog() override;

    // Populate the file list from the AudioFileManager
    //

    // Return a pointer to the currently selected AudioFile -
    // returns 0 if nothing is selected
    //
    AudioFile* getCurrentSelection();

    // Pop down playing dialog if it's currently up
    //
    void closePlayingDialog(AudioFileId id);

    // Can we playback audio currently?
    //
    void setAudioSubsystemStatus(bool ok);

    // Add a new file to the audio file manager
    //
    bool addAudioFile(const QString &filePath);


public slots:
    void slotAdd();
    void slotPlayPreview();
    // unused void slotRename();
    void slotInsert();
    void slotRemove();
    void slotRemoveAll();
    void slotRemoveAllUnused();
    void slotDeleteUnused();
    void slotExportAudio();
    void slotHelpRequested();
    void slotHelpAbout();

	//void slotItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);

    // get selection
	void slotSelectionChanged();
//	void slotSelectionChanged(QTreeWidgetItem *);

    // Repopulate
    //
    void slotPopulateFileList();

    // Commands
    //
    void slotCommandExecuted();

    /**
     * Accept a list of Segments and highlight accordingly
     * Used to reflect a selection on the main view
     * (when the user selects an audio track, the corresponding item
     * in the audio manager should be selected in turn)
     *
     * We check for embedded audio segments and if we find exactly one
     * we highlight it.  If we don't we unselect everything.
     *
     */
    void slotSegmentSelection(const SegmentSelection &);

    /**
     * Cancel the currently playing audio file
     */
    // unused void slotCancelPlayingAudioFile();

    void slotClose();

signals:

    // Control signals so we can tell the sequencer about our changes
    // or actions.
    //
    void addAudioFile(AudioFileId);
    void deleteAudioFile(AudioFileId);
    void playAudioFile(AudioFileId,
                       const RealTime &,
                       const RealTime &);
    void cancelPlayingAudioFile(AudioFileId);
    void deleteAllAudioFiles();

    /// We've selected a segment here, make the canvas select it too
    /**
     * RosegardenMainWindow::slotAudioManager() connects this to
     * RosegardenMainViewWidget::slotPropagateSegmentSelection().
     */
    void segmentsSelected(const SegmentSelection&);

    void deleteSegments(const SegmentSelection&);
    void insertAudioSegment(AudioFileId,
                            const RealTime &,
                            const RealTime &);

    void closing();
protected slots:
    void slotDropped(QDropEvent *event, QTreeWidget*, const QList<QUrl> &sl );
    void slotCancelPlayingAudio();

protected:
    bool addFile(const QUrl& kurl);
    bool isSelectedTrackAudio();
    void updateActionState(int numSelected);

    void closeEvent(QCloseEvent *) override;

    //--------------- Data members ---------------------------------

    AudioListView *m_fileList;
    WarningGroupBox    *m_wrongSampleRates;
    RosegardenDocument *m_doc;

    AudioFileId  m_playingAudioFile;
    AudioPlayingDialog      *m_audioPlayingDialog;
    QTimer                  *m_playTimer;

    static const char* const m_listViewLayoutName;
    static const int         m_maxPreviewWidth;
    static const int         m_previewHeight;

    bool                     m_audiblePreview;
    int                      m_sampleRate;

private:
    // get a list of audio file ids which are selected (either
    // directly or by the selection of a segment).
    void getSelectedIds(std::set<AudioFileId>& ids) const;

    void writeWavFile(const QString& destFileName, WAVAudioFile* sourceFile);

};



}

#endif
