#include "LAMPDataManagerDialog.h"
#include <QFileInfo>
#include <QHeaderView>
#include <QToolButton>
#include <QSpacerItem>
#include <QCloseEvent>
#include "ManualLabelToolWidget.h"
#include "WidgetSettingClass.h"
#include <QFileDialog>
#include <QDialog>
#include "LampDataItem.h"
#include "LampWindDataItem.h"
#include <filesystem>

LampDataManager::LampDataManager(ManualLabelToolWidget* InmainWidget, QWidget* parent)
    : QDialog(parent),
    mainWidget(InmainWidget)
{
    setupUI();
    clearCurrentFile();
    dataitems.clear();
    this->setWindowTitle(u8"数据管理");
    this->resize(500, 600);
}

LampDataManager::~LampDataManager()
{

}


void LampDataManager::LoadRasterFile(QString RasterPath)
{
}


void LampDataManager::showtreeWidgetBtnClicked()
{
    if (isShowTreeWidgetflag) {
        // 
        this->datatreeWidget->setHidden(true);
        isShowTreeWidgetflag = false;
        showtreeWidgetBtn->setText(tr(u8"展开数据列表"));
    }
    else {
        this->datatreeWidget->setHidden(false);
        isShowTreeWidgetflag = true;
        showtreeWidgetBtn->setText(tr(u8"隐藏数据列表"));
    }
}

void LampDataManager::showtextEditBtnClicked()
{
    if (isShowExtendWidgetflag) {
        this->textEdit->setHidden(true);
        isShowExtendWidgetflag = false;
        showtextEditBtn->setText(tr(u8"展开详细信息"));
    }
    else {
        this->textEdit->setHidden(false);
        isShowExtendWidgetflag = true;
        showtextEditBtn->setText(tr(u8"隐藏详细信息"));
    }
}

void LampDataManager::openRasterFile()
{
    QString lastFileDialogPath = WidgetSettingClass::instance().getLastFileDialogPath();
    if (lastFileDialogPath.isEmpty()) {
        lastFileDialogPath = ".";
    }
    // 打开影像
    // 2. 选择多个文件
    QString lampwindPath = QFileDialog::getOpenFileName(
        this,
        tr(u8"请选择影像文件"),
        lastFileDialogPath,
        tr(LAMPWINDDATAFILEFILTER)
    );
    if (std::filesystem::exists(lampwindPath.toUtf8().constData())) {
        WidgetSettingClass::instance().setLastFileDialogPath(lampwindPath);
        this->openLampWindFile(lampwindPath);
    }



}
void LampDataManager::openLampWindDataFile()
{

    QString lastFileDialogPath = WidgetSettingClass::instance().getLastFileDialogPath();
    if (lastFileDialogPath.isEmpty()) {
        lastFileDialogPath = ".";
    }
    // 打开影像
    // 2. 选择多个文件
    QString lampwindPath = QFileDialog::getOpenFileName(
        this,
        tr(u8"请选择风场文件"),
        lastFileDialogPath,
        tr(LAMPWINDDATAFILEFILTER)
    );

    if (std::filesystem::exists(lampwindPath.toUtf8().constData())) {
        WidgetSettingClass::instance().setLastFileDialogPath(lampwindPath);
        this->openLampWindFile(lampwindPath);
    }
}


void LampDataManager::closeEvent(QCloseEvent* event)
{
    event->ignore();
    this->hide();
}

void LampDataManager::reject()
{
    this->hide();
}

void LampDataManager::setupUI()
{
    // 4. 设置布局 - 这是将工具栏集成到对话框的关键步骤
    QVBoxLayout* layout = new QVBoxLayout(this);
    fileToolbar = new QToolBar(this);
    showtreeWidgetBtn = new QPushButton(tr(u8"隐藏数据列表"), this);
    datatreeWidget = new QTreeWidget(this);
    showtextEditBtn = new QPushButton(tr(u8"隐藏详细信息"), this);
    textEdit = new QTextEdit(this); 
    layout->addWidget(fileToolbar); // 将工具栏添加到布局顶部
    layout->addWidget(showtreeWidgetBtn); // 将工具栏添加到布局顶部
    layout->addWidget(datatreeWidget); // 将工具栏添加到布局顶部
    layout->addWidget(showtextEditBtn); // 将工具栏添加到布局顶部
    layout->addWidget(textEdit); // 将工具栏添加到布局顶部
    layout->setAlignment(Qt::AlignTop);
    setLayout(layout);

	isShowTreeWidgetflag = true;
	isShowExtendWidgetflag = true;

    datatreeWidget->setColumnCount(2); // 设置两列
    datatreeWidget->setHeaderLabels(QStringList() << u8"名称" << u8"类型"); // 设置列标题
    datatreeWidget->setColumnWidth(0, 150);


    LoadFileAction = fileToolbar->addAction(tr(u8"加载数据"));
	//openFileAction = fileToolbar->addAction(tr(u8"打开数据"));
	removeFileAction = fileToolbar->addAction(tr(u8"移除数据"));
	removeAllFileAction = fileToolbar->addAction(tr(u8"移除所有数据"));
	

	QObject::connect(this->showtreeWidgetBtn, SIGNAL(clicked()), this, SLOT(showtreeWidgetBtnClicked()));
	QObject::connect(this->showtextEditBtn, SIGNAL(clicked()), this, SLOT(showtextEditBtnClicked()));
	QObject::connect(this->removeFileAction, SIGNAL(triggered()), this, SLOT(removeNodeItem()));
	QObject::connect(this->removeAllFileAction, SIGNAL(triggered()), this, SLOT(removeALLNodeItems()));
	QObject::connect(this->LoadFileAction, SIGNAL(triggered()), this, SLOT(onShowData()));
	QObject::connect(this->datatreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(oncurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));

 

}

void LampDataManager::openLampWindFile(QString winddatapath)
{
    LampWindDataItem* item = new LampWindDataItem(this->datatreeWidget);
    item->OpenFileData(winddatapath);
    this->AddDataNode(item);
}


void LampDataManager::AddDataNode(LampDataItem* item)
{
    item->InitTreeNode();
    this->dataitems.append(item);
}

 
void LampDataManager::oncurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
{
    QTreeWidgetItem* currentItem = datatreeWidget->currentItem();
    if (currentItem == nullptr) return;
    
    QTreeWidgetItem* rootnode = nullptr;

    if (currentItem->parent() == nullptr) {
        rootnode = currentItem;
    }
    else {
        // 当前项是子节点
        QTreeWidgetItem* parent = currentItem->parent();
        rootnode = parent;
    }

    if (rootnode) {
        LampDataItem* lampItem = static_cast<LampDataItem*>(rootnode);
        QString context = lampItem->getDescription();
        this->textEdit->clear();
        this->textEdit->setText(context);
    }
    
}


void LampDataManager::removeNodeItem()
{
    QTreeWidgetItem* currentItem = datatreeWidget->currentItem();
    if (currentItem == nullptr) return;

    QTreeWidgetItem* rootnode = nullptr;

    if (currentItem->parent() == nullptr) {
        rootnode = currentItem;
    }
    else {
        // 当前项是子节点
        QTreeWidgetItem* parent = currentItem->parent();
        rootnode = parent;
    }

    if (rootnode) {
        LampDataItem* lampItem = static_cast<LampDataItem*>(rootnode);
        int32_t hashcode = lampItem->getHash();
        for (int32_t i = 0; i < this->dataitems.count(); i++) {
            if (this->dataitems[i]->getHash() == hashcode) {
                this->dataitems.removeAt(i);
                break;
            }
        }

        int index = datatreeWidget->indexOfTopLevelItem(rootnode);
        if (index >= 0) {
            delete datatreeWidget->takeTopLevelItem(index);
        }
    }

    this->textEdit->clear();

}

void LampDataManager::removeALLNodeItems()
{
    if (!datatreeWidget) return;

    // 循环取出并删除每一个顶级节点
    while (datatreeWidget->topLevelItemCount() > 0) {
        QTreeWidgetItem* item = datatreeWidget->takeTopLevelItem(0); // 取出第0个顶级节点
        if (item) {
            delete item; // 必须手动删除，takeTopLevelItem仅移除关联
        }
    }

    this->dataitems.clear();
    this->textEdit->clear();
}

void LampDataManager::onShowData()
{
    // 检查是否选择并加载数据
    for (int32_t i = 0; i < datatreeWidget->topLevelItemCount(); i++) {
        QTreeWidgetItem* item = datatreeWidget->topLevelItem(i);
        LampDataItem* lampItem = static_cast<LampDataItem*>(item);
        lampItem->showData(mainWidget);
    }
}

void LampDataManager::loadFile(const QString& filePath)
{

}

void LampDataManager::clearCurrentFile()
{

}

void LampDataManager::onLoadDataClicked()
{

}

