#ifndef THREADS_VIEW_H
#define THREADS_VIEW_H

#include "piintrospection_threads_p.h"
#include "piqt.h"
#include "pisystemmonitor.h"
#include "ui_threads_view.h"

#include <QAbstractItemModel>
#include <QDebug>
#include <QHash>
#include <QStyledItemDelegate>
#include <QTreeView>
#include <QWidget>


class ThreadsModel: public QAbstractItemModel {
	Q_OBJECT

public:
	ThreadsModel();

	void update(const PIVector<PIIntrospectionThreads::ThreadInfo> & t);
	void setStat(const PIVector<PISystemMonitor::ThreadStats> & s);
	void clear();

	int rowCount(const QModelIndex & parent = QModelIndex()) const;
	int columnCount(const QModelIndex & parent = QModelIndex()) const;
	QModelIndex parent(const QModelIndex & child) const { return QModelIndex(); }
	QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const;
	bool hasChildren(const QModelIndex & parent = QModelIndex()) const;
	QVariant headerData(int section, Qt::Orientation orientation, int role) const;
	QVariant data(const QModelIndex & index, int role) const;
	Qt::ItemFlags flags(const QModelIndex & index) const;

protected:
	PIVector<PIIntrospectionThreads::ThreadInfo> threads;
	PIVector<PISystemMonitor::ThreadStats> stat;
	QHash<int, QString> state_names;
	QHash<int, QColor> state_colors;
};


/*
class ContainersDelegate: public QStyledItemDelegate {
    Q_OBJECT
public:
    ContainersDelegate(QAbstractItemModel * m) {model = m;}
    void paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex &index) const override;
    QAbstractItemModel * model;
};
*/


class ThreadsView
	: public QWidget
	, private Ui::ThreadsView {
	Q_OBJECT

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

	void setStat(const PIVector<PISystemMonitor::ThreadStats> & stat);
	void showThreads(const PIVector<PIIntrospectionThreads::ThreadInfo> & threads);
	void clear();

protected:
	void changeEvent(QEvent * e);

	ThreadsModel * model;

private slots:
	void sessionSave(QByteArray * data) { *data = treeThreads->header()->saveState(); }
	void sessionLoad(QByteArray * data) { treeThreads->header()->restoreState(*data); }
	void updateHidden();

public slots:
};


#endif // CONTAINERS_VIEW_H
