/***************************************************************************
 *   Copyright (c) 2014 Werner Mayer <wmayer[at]users.sourceforge.net>     *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  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 Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/


#ifndef GUI_DIALOG_DLGPROPERTYLINK_H
#define GUI_DIALOG_DLGPROPERTYLINK_H

#include <QDialog>
#include <QAbstractItemView>
#include <QTimer>
#include <QPushButton>
#include <QPointer>

#define FC_XLINK_VALUE_INDEX 5

namespace Gui { namespace Dialog {

class Ui_DlgPropertyLink;
class DlgPropertyLink : public QDialog, public Gui::SelectionObserver
{
    Q_OBJECT

public:
    DlgPropertyLink(QWidget* parent = 0);
    ~DlgPropertyLink();

    void accept();

    QList<App::SubObjectT> currentLinks() const;
    QList<App::SubObjectT> originalLinks() const;

    void init(const App::DocumentObjectT &prop, bool tryFilter=true);

    static QString linksToPython(QList<App::SubObjectT> links);

    static QList<App::SubObjectT> getLinksFromProperty(const App::PropertyLinkBase *prop);

    static QString formatObject(App::Document *ownerDoc, App::DocumentObject *obj, const char *sub);

    static inline QString formatObject(App::Document *ownerDoc, const App::SubObjectT &sobj) {
        return formatObject(ownerDoc, sobj.getObject(), sobj.getSubName().c_str());
    }

    static QString formatLinks(App::Document *ownerDoc, QList<App::SubObjectT> links);

protected:
    void showEvent(QShowEvent *);
    void hideEvent(QHideEvent *);
    void closeEvent (QCloseEvent * e);
    void leaveEvent(QEvent *);
    bool eventFilter(QObject *obj, QEvent *ev);
    void keyPressEvent(QKeyEvent *ev);

    void detachObserver();
    void attachObserver();

    void onSelectionChanged(const Gui::SelectionChanges& msg);

private Q_SLOTS:
    void on_checkObjectType_toggled(bool);
    void on_typeTree_itemSelectionChanged();
    void on_searchBox_textChanged(const QString&);
    void onItemExpanded(QTreeWidgetItem * item);
    void onItemSelectionChanged();
    void onItemEntered(QTreeWidgetItem *item);
    void onItemSearch();
    void onTimer();
    void onClicked(QAbstractButton *);

private:
    QTreeWidgetItem *createItem(App::DocumentObject *obj, QTreeWidgetItem *parent);
    QTreeWidgetItem *createTypeItem(Base::Type type);
    void filterObjects();
    void filterItem(QTreeWidgetItem *item);
    bool filterType(QTreeWidgetItem *item);
    QTreeWidgetItem *findItem(App::DocumentObject *obj, const char *subname=0, bool *found=nullptr);
    void itemSearch(const QString &text, bool select);
    QList<App::SubObjectT> getLinkFromItem(QTreeWidgetItem *, bool needSubName=true) const;

private:
    Ui_DlgPropertyLink* ui;
    QTimer *timer;
    QPushButton *resetButton;
    QPushButton *refreshButton;

    QPointer<QWidget> parentView;
    std::vector<App::SubObjectT> savedSelections;

    App::DocumentObjectT objProp;
    std::set<App::DocumentObject*> inList;
    std::map<App::Document*, QTreeWidgetItem*> docItems;
    std::map<App::DocumentObject*, QTreeWidgetItem*> itemMap;
    std::map<QByteArray, QTreeWidgetItem*> typeItems;
    std::set<QTreeWidgetItem*> subSelections;
    QList<QTreeWidgetItem*> selections;
    std::set<QByteArray> selectedTypes;
    QList<App::SubObjectT> oldLinks;
    bool allowSubObject = false;
    bool singleSelect = false;
    bool singleParent = false;
    App::DocumentObject *currentObj = nullptr;
    QTreeWidgetItem *searchItem = nullptr;
    QBrush bgBrush;
};

} // namespace Dialog
} // namespace Gui


#endif // GUI_DIALOG_DLGPROPERTYLINK_H

