#include "QImageCursorWidget.h"
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QWidget>
#include <memory>
#include <qgsrasteridentifyresult.h>
#include <qgsmapcanvas.h>
#include <QCheckBox>
#include <QTreeView>
#include <qgsraster.h>
#include <qgsrasterlayer.h>
#include <qgsmeshlayer.h>




QImageCursorWidget::QImageCursorWidget(QWidget *parent)
	: QDialog(parent)
{
    maplayer_treewidgetItem.clear();
    this->initUi();
    this->setWindowTitle(u8"游标窗口");
    this->resize(400, 600);
}

QImageCursorWidget::~QImageCursorWidget()
{}

void QImageCursorWidget::initUi()
{
    // 创建主布局（垂直分布）
    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    // 1. 创建菜单按钮栏（这里使用QToolBar或QWidget配合QHBoxLayout模拟）
    QWidget* toolBarWidget = new QWidget(this);
    QHBoxLayout* toolBarLayout = new QHBoxLayout(toolBarWidget);
    toolBarLayout->setContentsMargins(0, 0, 0, 0); // 可选：移除边距使布局更紧凑

    // 创建复选框按钮：“时序图”、“方向图”
    QCheckBox* checkBoxTimeSeries = new QCheckBox(tr(u8"时序图"), toolBarWidget);
    QCheckBox* checkBoxDirection = new QCheckBox(tr(u8"方向图"), toolBarWidget);

    // 将复选框添加到按钮栏布局
    toolBarLayout->addWidget(checkBoxTimeSeries);
    toolBarLayout->addWidget(checkBoxDirection);
    toolBarLayout->addStretch(); // 添加一个弹性空间，使按钮靠左排列

    // 2. 创建TreeView
    treewidgetResults = new QTreeWidget(this);
    treewidgetResults->setColumnCount(2);
    treewidgetResults->setHeaderLabels(QStringList() << u8"属性" << u8"值");
    // 3. 将按钮栏和TreeView添加到主垂直布局中
    mainLayout->addWidget(toolBarWidget);
    mainLayout->addWidget(treewidgetResults);

    // 4. （可选）设置布局的比例，例如让TreeView占据更多空间
    mainLayout->setStretchFactor(toolBarWidget, 0); // 工具栏不拉伸
    mainLayout->setStretchFactor(treewidgetResults, 1);       // TreeView可拉伸

 
    // 设置当前窗口的布局为主布局
    this->setLayout(mainLayout);
 
}

void QImageCursorWidget::showCursor(QList<QgsMapToolIdentify::IdentifyResult> result)
{
    maplayer_treewidgetItem.clear();
    treewidgetResults->clear();
    treewidgetResults->setColumnCount(2);
    treewidgetResults->setHeaderLabels(QStringList() << u8"属性" << u8"值");
	int32_t layerCount = result.count();
	for (int32_t i = 0; i < layerCount; i++) {
		if (result[i].mLayer->type() == Qgis::LayerType::Raster) {

		}
		else if (result[i].mLayer->type() == Qgis::LayerType::Mesh) {

		}
	}
    this->populateTreeWidget(result);
}

void QImageCursorWidget::populateTreeWidget(const QList<QgsMapToolIdentify::IdentifyResult>& results)
{
    for (const QgsMapToolIdentify::IdentifyResult& result : results) {
        if (result.mLayer->type() == Qgis::LayerType::Raster) {
            addFeature(qobject_cast<QgsRasterLayer*>(result.mLayer), result.mLabel, result.mAttributes, result.mDerivedAttributes, result.mFields, result.mFeature, result.mParams);
        }
        else if (result.mLayer->type() == Qgis::LayerType::Mesh) {
            addFeature(qobject_cast<QgsMeshLayer*>(result.mLayer), result.mLabel, result.mAttributes, result.mDerivedAttributes);
        }
        else {

        }
    }
    treewidgetResults->expandAll();
}

void QImageCursorWidget::addKeyValuePair(QTreeWidgetItem* parent, const QString& key, const QString& value)
{
    QTreeWidgetItem* item = new QTreeWidgetItem(parent);
    item->setText(0, key);
    item->setText(1, value);
}

QTreeWidgetItem* QImageCursorWidget::layerItem(QgsMapLayer* layer)
{

    QTreeWidgetItem* layItem = nullptr;
    if (maplayer_treewidgetItem.contains(layer)) {
        layItem = maplayer_treewidgetItem[layer];
        treewidgetResults->addTopLevelItem(layItem);
    }
    else {
        layItem = new QTreeWidgetItem(treewidgetResults);
        layItem->setText(0, layer->name());
        maplayer_treewidgetItem.insert(layer, layItem);
        treewidgetResults->addTopLevelItem(layItem);
    }
    return layItem;
 
}



void QImageCursorWidget::addFeature(QgsRasterLayer* layer, 
                                    const QString& label, const QMap<QString, QString>& attributes, 
                                    const QMap<QString, QString>& derivedAttributes, const QgsFields& fields, 
                                    const QgsFeature& feature, const QMap<QString, QVariant>& params)
{
    QTreeWidgetItem* layItem = layerItem(layer);
    QTreeWidgetItem* labelNode = new QTreeWidgetItem();
    labelNode->setText(0, label);
    // attributes
    for (auto it = attributes.begin(); it != attributes.end(); ++it) {
        QTreeWidgetItem* tempnode = new QTreeWidgetItem();
        tempnode->setText(0, it.key());
        tempnode->setText(1, it.value());
        labelNode->addChild(tempnode);
    }
    layItem->addChild(labelNode);
    // derivedAttributes
    QTreeWidgetItem* driverNode = new QTreeWidgetItem();
    for (auto it = derivedAttributes.begin(); it != derivedAttributes.end(); ++it) {
        QTreeWidgetItem* tempnode = new QTreeWidgetItem();
        tempnode->setText(0, it.key());
        tempnode->setText(1, it.value());
        driverNode->addChild(tempnode);
    }
    layItem->addChild(driverNode);
}

void QImageCursorWidget::addFeature(QgsMeshLayer* layer, const QString& label, const QMap<QString, QString>& attributes, const QMap<QString, QString>& derivedAttributes)
{
    QTreeWidgetItem* layItem = layerItem(layer);
    QTreeWidgetItem* labelNode = new QTreeWidgetItem();
    labelNode->setText(0, label);
    // attributes
    for (auto it = attributes.begin(); it != attributes.end(); ++it) {
        QTreeWidgetItem* tempnode = new QTreeWidgetItem();
        tempnode->setText(0, it.key());
        tempnode->setText(1, it.value());
        labelNode->addChild(tempnode);
    }
    layItem->addChild(labelNode);
    // derivedAttributes
    QTreeWidgetItem* driverNode = new QTreeWidgetItem();
    driverNode->setText(0, u8"(derivedAttributes)");
    for (auto it = derivedAttributes.begin(); it != derivedAttributes.end(); ++it) {
        QTreeWidgetItem* tempnode = new QTreeWidgetItem();
        tempnode->setText(0, it.key());
        tempnode->setText(1, it.value());
        driverNode->addChild(tempnode);
    }
    layItem->addChild(driverNode);
}

 
 