#include "QQuickDetailsView.h"
#include "private/qqmldata_p.h"
#include <QQmlEngine>

#include "qqmlcontext.h"
#include "QQuickDetailsViewPrivate.h"
#include "QQuickDetailsViewRow.h"
#include "QQuickFunctionLibrary.h"

void QQuickDetailsViewPrivate::initItemCallback(int serializedModelIndex, QObject* object)
{
    QQuickTreeViewExPrivate::initItemCallback(serializedModelIndex, object);
	auto item = qobject_cast<QQuickItem*>(object);
	if (!item)
		return;
	const QModelIndex& index = m_treeModelToTableModel.mapToModel(serializedModelIndex);
	IDetailsViewRow* node = static_cast<IDetailsViewRow*>(index.internalPointer());
	node->setupItem(item);
}

QQuickDetailsView::QQuickDetailsView(QQuickItem* parent /*= nullptr*/)
    : QQuickTreeViewEx(*(new QQuickDetailsViewPrivate()),parent)
{
    setModel(QVariant::fromValue(d_func()->mModel));
	setReuseItems(false);
	setEditTriggers(QQuickTableView::EditTrigger::DoubleTapped);
}

qreal QQuickDetailsView::getSpliterPencent() const
{
    return d_func()->mSpliterPencent;
}

void QQuickDetailsView::setSpliterPencent(qreal val)
{
    if(val != d_func()->mSpliterPencent){
		d_func()->mSpliterPencent = val;
		Q_EMIT asSpliterPencentChanged(val);
    }
}

void QQuickDetailsView::setObject(QObject* inObject)
{
	if (inObject != d_func()->mModel->getObject()) {
		d_func()->mModel->setObject(inObject);
		Q_EMIT asObjectChanged(inObject);

		// Auto-expand group rows (once, C++ side — independent of delegate lifecycle)
		Q_D(QQuickDetailsView);
		QAbstractItemModel* m = d->mModel;
		for (int i = 0; i < m->rowCount(); ++i) {
			QModelIndex idx = m->index(i, 0);
			IDetailsViewRow* node = static_cast<IDetailsViewRow*>(idx.internalPointer());
			if (node && node->isGroup()) {
				int row = d->m_treeModelToTableModel.itemIndex(idx);
				if (row >= 0) {
					expand(row);
				}
			}
		}
	}
}

QObject* QQuickDetailsView::getObject() const
{
	return d_func()->mModel->getObject();
}

void QQuickDetailsView::componentComplete()
{
    QQmlEngine* engine = qmlEngine(this);
	QQmlComponent* delegate = new QQmlComponent(engine, this);
	engine->rootContext()->setContextProperty("helper", QQuickFunctionLibrary::Get());
	delegate->setData(R"(
        import QtQuick;
        import QtQuick.Controls;
        import QtQuick.Layouts;
		import QtQuick.DetailsView;
        Item {
			 id: detailsDelegate
			 readonly property real indent: 20
             readonly property real padding: 5
			 required property DetailsView detailsView
			 required property int row
             required property bool isTreeNode
             required property bool expanded
             required property int hasChildren
             required property int depth
             required property bool isGroup
             implicitWidth: detailsView.width
             implicitHeight: heightProxy ? heightProxy.height + 5 : 30
			 TapHandler {
				onTapped: detailsView.toggleExpanded(row)
             }
			 onImplicitHeightChanged: {
				detailsView.invalidateLayout();
             }
         })", QUrl("QQuickDetailsView.componentComplete"));;
	setDelegate(delegate);
    QQuickTreeViewEx::componentComplete();
}
