/***************************** LICENSE START ***********************************

 Copyright 2012 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#ifndef MvQBookmarks_H
#define MvQBookmarks_H

#include <QDialog>
#include <QListView>
#include <QMap>
#include <QStyledItemDelegate>

#include "MvQKeyModel.h"

#include <string>
using namespace std;


class QAction;
class QLineEdit;
class QMenu;
class QModelIndex;
class QPlainTextEdit;

class MvQContextItemSet;
class MvQKeyManager;
class MvQKeyModel;

class Folder;
class IconObject;
class MvKey;
class MvKeyProfile;

class MvQBookmarksPanel;

class MvQBookmarksDelegate : public QStyledItemDelegate
{
public:
    MvQBookmarksDelegate(QWidget* parent = 0);
    void paint(QPainter* painter, const QStyleOptionViewItem& option,
               const QModelIndex& index) const;
};

class MvQBookmarksModel : public MvQKeyModel
{
public:
    MvQBookmarksModel(MvKeyProfile* prof) :
        MvQKeyModel(prof, ShortNameMode, 0){};
    QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const;
};


class MvQBookmarks : public QObject
{
    Q_OBJECT

public:
    MvQBookmarks(QWidget*);
    ~MvQBookmarks(){};

    static void init();
    static void save();
    static bool addItem(IconObject*);
    static bool addItem(QString);
    static bool addItem(QList<Folder*>);
    static bool editItem(QString);
    static bool editItem(MvKey*);
    static bool isBookmark(QString);

signals:
    void openInBrowser(QString);
    void lookupOrOpenInTab(QString);
    void openInNewTab(QString);
    void openInWin(QString);
    void openGroupInTabs(QStringList);
    void openGroupInWin(QStringList);
    void bookmarkFolder();
    void bookmarkTabs();

protected:
    static MvQContextItemSet* cmSet();
    void defaultCommand(const QModelIndex&);
    void command(QString);
    void command(QString, const QModelIndex&);
    static bool addItem(MvKey*);
    bool editItem(const QModelIndex&);
    bool duplicateItem(const QModelIndex&);
    bool deleteItem(const QModelIndex&);

    MvKey* getKey(const QModelIndex&);
    QString getPath(const QModelIndex&);
    QStringList getPathsFromKey(MvKey*);
    static bool isFolderGroup(MvKey*);

    static MvQKeyManager* manager_;
    static MvQBookmarksModel* model_;
    static MvKeyProfile* prof_;
};

class MvQBookmarksMenu : public MvQBookmarks
{
    Q_OBJECT

public:
    MvQBookmarksMenu(QWidget*, QMenu*);
    ~MvQBookmarksMenu() {}

public slots:
    void slotUpdate();
    void slotMenu(QAction*);
    void slotContextMenu(const QPoint&);

protected:
    QString getPath(QAction*);
    MvKey* getKey(QAction*);
    QModelIndex getIndex(QAction*);

    QMenu* menu_;
    QAction* actionFolder_;
    QAction* actionTabs_;
    QAction* actionAdd_;
};


class MvQBookmarksView : public QListView
{
public:
    MvQBookmarksView(QWidget* parent = 0);

protected:
    void checkDropTarget(QDropEvent* event);
    void removeDropTarget();

    void dragEnterEvent(QDragEnterEvent* event);
    void dragMoveEvent(QDragMoveEvent* event);
    void dragLeaveEvent(QDragLeaveEvent* event);
    void dropEvent(QDropEvent* event);
    void paintEvent(QPaintEvent*);
};

class MvQBookmarksPanel : public MvQBookmarks
{
    Q_OBJECT

public:
    MvQBookmarksPanel(QWidget*);
    ~MvQBookmarksPanel(){};

    QWidget* widget() { return widget_; }

public slots:
    void slotDoubleClickItem(const QModelIndex&);
    void slotContextMenu(const QPoint&);
    void slotAddItem(bool);

signals:
    void closePanel(bool b = true);

protected:
    QWidget* widget_;
    MvQBookmarksView* view_;
};


class MvQBookmarksDialog : public QDialog
{
    Q_OBJECT

public:
    MvQBookmarksDialog(MvKey*, QString, QWidget* parent = 0);
    ~MvQBookmarksDialog(){};

public slots:
    void accept();

protected:
    MvKey* key_;
    QLineEdit* labelEdit_;
    QLineEdit* pathEdit_;
};

class MvQBookmarksGroupDialog : public QDialog
{
    Q_OBJECT

public:
    MvQBookmarksGroupDialog(MvKey*, QString, QWidget* parent = 0);
    ~MvQBookmarksGroupDialog(){};

public slots:
    void accept();

protected:
    QString getPathsFromKey();

    MvKey* key_;
    QLineEdit* labelEdit_;
    QPlainTextEdit* pathEdit_;
};

#endif
