#include "CustomNavigationBar.h"
#include "ui_CustomNavigationBar.h"

#include <QDebug>
#include <QScroller>

CustomNavigationBar::CustomNavigationBar(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CustomNavigationBar)
{
    ui->setupUi(this);

    initTreeWidget();
}

CustomNavigationBar::~CustomNavigationBar()
{
    delete ui;
}

QTreeWidgetItem* CustomNavigationBar::addNavigationItem(const QString &text, int data, QTreeWidgetItem *parent)
{
    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setData(0, Qt::UserRole, data);
    item->setText(0, text);
    if (parent) {
        parent->addChild(item);
    }

    ui->navigationBar->addTopLevelItem(item);

    return item;
}

void CustomNavigationBar::expandAll()
{
    ui->navigationBar->expandAll();
}

void CustomNavigationBar::setSelectItem(const QString &text)
{
    // 遍历所有顶层项
    for (int i = 0; i < ui->navigationBar->topLevelItemCount(); ++i) {
        QTreeWidgetItem* item = ui->navigationBar->topLevelItem(i);
        if (findAndSelectItem(item, text)) {
            return;  // 找到后直接返回
        }
    }
}

void CustomNavigationBar::setSelectItem(const int &data)
{
    // 遍历所有顶层项
    for (int i = 0; i < ui->navigationBar->topLevelItemCount(); ++i) {
        QTreeWidgetItem* item = ui->navigationBar->topLevelItem(i);
        if (findAndSelectItem(item, data)) {
            return;  // 找到后直接返回
        }
    }
}

bool CustomNavigationBar::removeNavigationItem(const QString& text)
{
    // 遍历所有顶层项
    for (int i = 0; i < ui->navigationBar->topLevelItemCount(); ++i) {
        QTreeWidgetItem* item = ui->navigationBar->topLevelItem(i);
        if (findAndRemoveItem(item, text)) {
            return true;  // 找到并删除后直接返回
        }
    }
    return false;  // 未找到
}

bool CustomNavigationBar::removeNavigationItem(const int& data)
{
    // 遍历所有顶层项
    for (int i = 0; i < ui->navigationBar->topLevelItemCount(); ++i) {
        QTreeWidgetItem* item = ui->navigationBar->topLevelItem(i);
        if (findAndRemoveItem(item, data)) {
            return true;  // 找到并删除后直接返回
        }
    }
    return false;  // 未找到
}

bool CustomNavigationBar::removeNavigationItem(QTreeWidgetItem* item)
{
    if (!item) {
        return false;
    }
    
    // 检查是否是当前选中的项，如果是则清除选中状态
    if (ui->navigationBar->currentItem() == item) {
        ui->navigationBar->setCurrentItem(nullptr);
    }
    
    // 删除项及其所有子项
    delete item;
    return true;
}

void CustomNavigationBar::clearNavigationItems()
{
    ui->navigationBar->clear();
}

QTreeWidgetItem* CustomNavigationBar::findNavigationItem(const QString& text)
{
    // 遍历所有顶层项
    for (int i = 0; i < ui->navigationBar->topLevelItemCount(); ++i) {
        QTreeWidgetItem* item = ui->navigationBar->topLevelItem(i);
        QTreeWidgetItem* foundItem = findItemRecursive(item, text);
        if (foundItem) {
            return foundItem;  // 找到后直接返回
        }
    }
    return nullptr;  // 未找到
}

QTreeWidgetItem* CustomNavigationBar::findNavigationItem(const int& data)
{
    // 遍历所有顶层项
    for (int i = 0; i < ui->navigationBar->topLevelItemCount(); ++i) {
        QTreeWidgetItem* item = ui->navigationBar->topLevelItem(i);
        QTreeWidgetItem* foundItem = findItemRecursive(item, data);
        if (foundItem) {
            return foundItem;  // 找到后直接返回
        }
    }
    return nullptr;  // 未找到
}

void CustomNavigationBar::initTreeWidget()
{
    ui->navigationBar->setHeaderHidden(true);
    ui->navigationBar->setExpandsOnDoubleClick(true);

    connect(ui->navigationBar, &QTreeWidget::itemClicked,       this, &CustomNavigationBar::slotTreeWidgetItemClicked);

    QScroller::grabGesture(ui->navigationBar,              QScroller::TouchGesture);
    QScroller::grabGesture(ui->navigationBar->viewport(),  QScroller::LeftMouseButtonGesture);
}

bool  CustomNavigationBar::findAndSelectItem(QTreeWidgetItem *parentItem, const QString &targetText)
{
    // 检查当前项是否符合条件
    if (parentItem->text(0) == targetText) {
        ui->navigationBar->setCurrentItem(parentItem);
        return true;
    }

    // 递归检查所有子项
    for (int i = 0; i < parentItem->childCount(); ++i) {
        QTreeWidgetItem* childItem = parentItem->child(i);
        if (findAndSelectItem(childItem, targetText)) {
            return true;
        }
    }

    return false;  // 未找到
}

bool CustomNavigationBar::findAndSelectItem(QTreeWidgetItem *parentItem, const int &data)
{
    // 检查当前项是否符合条件
    if (parentItem->data(0, Qt::UserRole).toInt() == data) {
        ui->navigationBar->setCurrentItem(parentItem);
        return true;
    }

    // 递归检查所有子项
    for (int i = 0; i < parentItem->childCount(); ++i) {
        QTreeWidgetItem* childItem = parentItem->child(i);
        if (findAndSelectItem(childItem, data)) {
            return true;
        }
    }

    return false;  // 未找到
}

bool CustomNavigationBar::findAndRemoveItem(QTreeWidgetItem* parentItem, const QString& targetText)
{
    // 检查当前项是否符合条件
    if (parentItem->text(0) == targetText) {
        removeNavigationItem(parentItem);
        return true;
    }

    // 递归检查所有子项
    for (int i =0; i < parentItem->childCount(); ++i) {
        QTreeWidgetItem* childItem = parentItem->child(i);
        if (findAndRemoveItem(childItem, targetText)) {
            return true;
        }
    }

    return false;  // 未找到
}

bool CustomNavigationBar::findAndRemoveItem(QTreeWidgetItem* parentItem, const int& data)
{
    // 检查当前项是否符合条件
    if (parentItem->data(0, Qt::UserRole).toInt() == data) {
        removeNavigationItem(parentItem);
        return true;
    }

    // 递归检查所有子项
    for (int i =0; i < parentItem->childCount(); ++i) {
        QTreeWidgetItem* childItem = parentItem->child(i);
        if (findAndRemoveItem(childItem, data)) {
            return true;
        }
    }

    return false;  // 未找到
}

QTreeWidgetItem* CustomNavigationBar::findItemRecursive(QTreeWidgetItem* parentItem, const QString& targetText)
{
    // 检查当前项是否符合条件
    if (parentItem->text(0) == targetText) {
        return parentItem;
    }

    // 递归检查所有子项
    for (int i =0; i < parentItem->childCount(); ++i) {
        QTreeWidgetItem* childItem = parentItem->child(i);
        QTreeWidgetItem* foundItem = findItemRecursive(childItem, targetText);
        if (foundItem) {
            return foundItem;
        }
    }

    return nullptr;  // 未找到
}

QTreeWidgetItem* CustomNavigationBar::findItemRecursive(QTreeWidgetItem* parentItem, const int& data)
{
    // 检查当前项是否符合条件
    if (parentItem->data(0, Qt::UserRole).toInt() == data) {
        return parentItem;
    }

    // 递归检查所有子项
    for (int i =0; i < parentItem->childCount(); ++i) {
        QTreeWidgetItem* childItem = parentItem->child(i);
        QTreeWidgetItem* foundItem = findItemRecursive(childItem, data);
        if (foundItem) {
            return foundItem;
        }
    }

    return nullptr;  // 未找到
}

void CustomNavigationBar::slotTreeWidgetItemClicked(const QTreeWidgetItem *item, const int column)
{
    emit navigationItemClicked(item->data(column, Qt::UserRole).toInt());
}
