/*
 * UKUI Intelligent Data Manager
 *
 * Copyright (C) 2025, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: JinQin He <hejinqin@kylinos.cn>
 *
 */
#include "search-file-item.h"
#include"QTimer"
SearchFileItem::SearchFileItem(std::shared_ptr<Peony::FileInfo> info, SearchFileItem *parentItem, SearchFileItemModel *model, QObject *parent)
{
//    qRegisterMetaType<QVector<SearchFileItem*>* >("QVector<FileItem*>*");
//    qRegisterMetaType<QHash<QString, SearchFileItem*>>("QHash<QString, FileItem*>");
//    qRegisterMetaType<std::vector<std::shared_ptr<FileInfo> >>("std::vector<std::shared_ptr<FileInfo> >&");
    m_parent = parentItem;
    m_info = info;
    m_children = new QVector<SearchFileItem*>();
    m_uri_item_hash.clear();
    m_model = model;
    setParent(m_model);
    if(this->m_info!=nullptr){
//        m_watcher = std::make_shared<FileWatcher>(this->m_info->uri(), nullptr, true);
//        //m_watcher->startMonitor();
    }else{
        needConnectSignal = true;
    }
    m_thumbnail_watcher = std::make_shared<Peony::FileWatcher>("thumbnail://");
    connectFunc();

}

SearchFileItem::~SearchFileItem()
{
    if (m_children) {
        qDeleteAll(*m_children);  // 删除所有指针指向的对象
        m_children->clear();      // 清空向量
    }
    m_uri_item_hash.clear();
    disconnect(m_model->m_fileManagerThread, &FileManagerThread::finishQueryFileInfos, this, nullptr);
}

const QString SearchFileItem::uri()
{
    return m_info->uri();
}

void SearchFileItem::appendChild(SearchFileItem *child)
{
    if (!child) return;

    m_children->append(child);
    if(m_info!=nullptr){
       m_uri_item_hash.insert(child->uri(), child);
    }
    child->m_parent = this;
}

int SearchFileItem::childCount() const
{
    return m_children->size();
}

void SearchFileItem::connectFunc()
{
    if(needConnectSignal){
        connect(m_model->m_fileManagerThread, &FileManagerThread::finishQueryFileInfos, this, [=](std::vector<std::shared_ptr<FileInfo> >& retFileInfos, SearchFileItem *parentItem,bool isfinish){
            /* 查询结果返回，更新数据 */
            if(parentItem && this != parentItem)
                return;
            m_item_type = parentItem;
            QModelIndex model = m_model->indexFromItem(parentItem);
            m_model->beginInsertRows(model, m_children->count(), m_children->count() + retFileInfos.size() -1);
            for (auto& info : retFileInfos) {
                Peony::FileInfoManagerHelper::updateFileInfo(info);
                auto item = new SearchFileItem(info, this, m_model);
                m_children->append(item);
                m_uri_item_hash.insert(item->uri(), item);
                ThumbnailManager::getInstance()->createThumbnail(info->uri(), m_thumbnail_watcher);
            }
            m_model->endInsertRows();
            if(isfinish){
                m_model->finishSearch();
            }
        });
    }
    connect(m_thumbnail_watcher.get(), &FileWatcher::fileChanged, this, [=](const QString &uri){
        auto index = m_model->indexFromUri(uri,m_item_type);
        if (index.isValid()) {
            auto item = m_model->itemFromIndex(index);
            if (item) {
                m_model->updated();
                //m_model->thumbnailUpdated(uri);
            }
        }
    });
    connect(m_thumbnail_watcher.get(), &FileWatcher::thumbnailUpdated, this, [=](const QString &uri) {
            m_model->updated();
            //m_model->dataChanged(m_model->indexFromUri(uri), m_model->indexFromUri(uri));
        }, Qt::UniqueConnection);
}

void SearchFileItem::updateInfoAsync()
{
    FileInfoJob *job = new FileInfoJob(m_info);
    job->setAutoDelete();
    job->connect(job, &FileInfoJob::infoUpdated, this, [=]() {
        m_model->updated();
        ThumbnailManager::getInstance()->createThumbnail(this->uri(), m_thumbnail_watcher, true);
        //Q_EMIT m_model->updateFilter();
    });
    job->queryAsync();
}

SearchFileItem *SearchFileItem::getChildFromUri(QString uri)
{
    // optimize
    auto index = m_model->indexFromUri(uri,m_item_type);
    if (index.isValid()) {
        return m_model->itemFromIndex(index);
    }
    return nullptr;
}

