#ifndef SEARCHRESULTMODEL_H
#define SEARCHRESULTMODEL_H

#include <QAbstractItemModel>
#include <QString>
#include <QPixmap>
#include <QList>
#include <QHash>
#include <QStringList>
#include <QRegExp>


#define COLUMN_COUNT            0
#define COLUMN_FILENAME         1
#define COLUMN_EXTENSION        2
#define COLUMN_SIZE             3
#define COLUMN_ESIZE            4
#define COLUMN_TTH              5
#define COLUMN_PATH             6
#define COLUMN_NICK             7
#define COLUMN_FREESLOTS        8
#define COLUMN_ALLSLOTS         9
#define COLUMN_IP               10
#define COLUMN_HUB              11
#define COLUMN_HOST             12


class SearchListItem
{

public:
    SearchListItem(const QList<QVariant> &data, SearchListItem *parent = 0);
    ~SearchListItem();

    void appendChild(SearchListItem *child);

    SearchListItem *child(int row);
    int childCount() const;
    int columnCount() const;
    QVariant data(int column) const;
    int row() const;
    SearchListItem *parent();

    QPixmap icon;
    qulonglong size;
    unsigned count;
    unsigned slots_all;
    unsigned slots_free;

    bool hide;

    bool exists_in_share;

    QList<SearchListItem*> childItems;
private:

    QList<QVariant> itemData;
    SearchListItem *parentItem;
};

class SearchListModel : public QAbstractItemModel
{
    Q_OBJECT

public:

    enum FilterRole{
        FilterFileName = 0,
        FilterPath,
        FilterTTH,
        FilterNick,
        FilterNone
    };

    SearchListModel(QObject *parent = 0);
    ~SearchListModel();

    /** */
    QVariant data(const QModelIndex &, int) const;
    /** */
    Qt::ItemFlags flags(const QModelIndex &) const;
    /** */
    QVariant headerData(int section, Qt::Orientation, int role = Qt::DisplayRole) const;
    /** */
    QModelIndex index(int, int, const QModelIndex &parent = QModelIndex()) const;
    /** */
    QModelIndex parent(const QModelIndex &index) const;
    /** */
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    /** */
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    /** sort list */
    virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);

    /** */
    void addResult(
            const QString file,
            const QPixmap icon,
            qulonglong size,
            const QString tth,
            const QString path,
            const QString nick,
            const int free_slots,
            const int all_slots,
            const QString ip,
            const QString hub,
            const QString host,
            const bool exists);

    /** */
    int getSortColumn() const;
    /** */
    void setSortColumn(int);
    /** */
    Qt::SortOrder getSortOrder() const;
    /** */
    void setSortOrder(Qt::SortOrder);
    /** */
    const QList<SearchListItem*> &getRowItems();

    /** Clear model and redraw view*/
    void clearModel();

    /** */
    FilterRole getFilterRole() const;
    /** */
    void setFilterRole(FilterRole);

    /** Return true if item contains childs*/
    bool isTTHRoot(const QString&, const QString&, const QString&, const QString&);
    /** */
    const SearchListItem &getTTHItems(const QString&, const QString&, const QString&, const QString&);

private:
    /** */
    FilterRole role;
    /** */
    int sortColumn;
    /** */
    Qt::SortOrder sortOrder;
    /** */
    SearchListItem *rootItem;
    /** */
    QHash<QString, SearchListItem*> tths;
    /** */
    FilterRole filterRole;
    /** */
    SearchListItem dummy;
};

#endif
