#include <QApplication>
#include <QDesktopServices>
#include <QTreeView>
#include <QFileSystemModel>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QLabel>
#include <QUrl>
#include <QDebug>
#include <QDesktopWidget>
#include <QProxyStyle>
#include <QTreeView>
#include <QFileSystemModel>
#include <qheaderview.h>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QMouseEvent>
#include <QPushButton>
#include <QScreen>

#include "CustomFileDelegate.h" // 包含自定义代理类





// class Custom_Style : public QProxyStyle {
// public:
//     explicit Custom_Style(QStyle *baseStyle = nullptr) : QProxyStyle(baseStyle) {}
//
//     void drawPrimitive(QStyle::PrimitiveElement element,
//                        const QStyleOption *option,
//                        QPainter *painter,
//                        const QWidget *widget = nullptr) const override {
//         if (element == QStyle::PE_IndicatorBranch) {
//             // 自定义绘制箭头
//             QRect rect = option->rect;
//             QPoint center = rect.center();
//             if (option->state & QStyle::State_Open) {
//                 // 绘制展开的箭头（向下）
//                 QPoint points[3] = {
//                     QPoint(center.x() - 5, center.y() - 2),
//                     QPoint(center.x() + 5, center.y() - 2),
//                     QPoint(center.x(), center.y() + 5)
//                 };
//                 painter->setBrush(Qt::black);
//                 painter->drawPolygon(points, 3);
//             } else {
//                 // 绘制收起的箭头（向右）
//                 QPoint points[3] = {
//                     QPoint(center.x() - 2, center.y() - 5),
//                     QPoint(center.x() - 2, center.y() + 5),
//                     QPoint(center.x() + 5, center.y())
//                 };
//                 painter->setBrush(Qt::black);
//                 painter->drawPolygon(points, 3);
//             }
//         } else {
//             // 调用默认绘制逻辑
//             QProxyStyle::drawPrimitive(element, option, painter, widget);
//         }
//     }
// };
//


class Custom_File_Delegate;
class file_explorer : public QWidget {
    Q_OBJECT
public:
    QTreeView *tree_view;
    QFileSystemModel *model;
    QVBoxLayout *layout;
    QLineEdit* show_root_dir;
    QFileInfo focus;
    QLabel *showFocusFile;
    QPushButton *confirmButton, *cancelButton;
    int height = 60;
    Custom_File_Delegate *it;
    void show_center() {
        QScreen *screen = QGuiApplication::primaryScreen();
        QRect screen_rect = screen->availableGeometry();
        QRect window_rect = frameGeometry();
        int x = (screen_rect.width() - window_rect.width()) / 2;
        int y = (screen_rect.height() - window_rect.height()) / 2;
        move(x, y);
        show();
    }
    void resizeEvent(QResizeEvent* event) override {

    }
protected:
    void paintEvent(QPaintEvent* event) override {
        QPainter painter(this);
        painter.fillRect(rect(), Qt::white);
        painter.end();
    }
public:
    void change_root(QString dir_path) {
        dir_path.replace("\\", "/");
        QFileInfo file_info(dir_path); // 用 QFileInfo 检测路径
        if (file_info.isDir()) {
            //qDebug() << "dir " << dir_path;
        } else if (file_info.isFile()) {
            //qDebug() << "file " << dir_path;
            focus = file_info;
            showFocusFile->setText(focus.absolutePath());
            dir_path = file_info.dir().absolutePath();
        } else {
            //qDebug() << "error " << dir_path;
            show_root_dir->setText(model->rootPath());
            return;
        }
        if (!dir_path.endsWith("/")) {
            dir_path.append("/");
        }
        show_root_dir->setText(dir_path);
        model->setRootPath(dir_path);
        tree_view->setRootIndex(model->index(dir_path));

        model->setHeaderData(0,Qt::Horizontal,"文件名称");
        model->setHeaderData(1,Qt::Horizontal,"文件大小");

        tree_view->setColumnWidth(0, 500);
    }

    file_explorer();
};



