#include "storyconceptspresent.h"
#include "DocsManager.h"
#include "command_list.h"
#include "opstream.h"

#include <QSplitter>
#include <QVBoxLayout>

using namespace Operate;
using namespace Components;
using namespace Core;
using namespace Parse::Result;
using namespace Tools;
using namespace Schedule;
using namespace CommandList;
using namespace DataModel;

StoryconceptsPresent::StoryconceptsPresent(CommandsDispatcher *core, StoryconceptsPresentModel *backend, QWidget *parent)
    : QWidget(parent), core_ins(core), tree_view(new QTreeView(this)), details_view(new QTextBrowser(this)) {
    tree_view->setModel(backend->treeModel());
    tree_view->setHeaderHidden(true);
    details_view->setDocument(backend->detailsBackend());
    details_view->setReadOnly(true);

    auto layout = new QVBoxLayout(this);
    layout->setMargin(0);

    auto splitter = new QSplitter(Qt::Vertical, this);
    layout->addWidget(splitter);
    splitter->addWidget(tree_view);
    splitter->addWidget(details_view);

    connect(tree_view, &QTreeView::clicked, this,
            &StoryconceptsPresent::show_details);
    connect(tree_view, &QTreeView::doubleClicked, this,
            &StoryconceptsPresent::jump_to);
}

void StoryconceptsPresent::show_details(const QModelIndex &curr) {
    if(!curr.isValid())
        return;

    QList<QString> path;
    QModelIndex node = curr;
    while (true) {
        auto name = node.data().toString();
        if (name.isEmpty())
            break;
        path.insert(0, name);
        node = node.parent();
    }

    core_ins->postCommand(StoryconceptDetailShow(path));
}

void StoryconceptsPresent::jump_to(const QModelIndex &curr) {
    if(!curr.isValid())
        return;

    QList<QString> path;
    QModelIndex node = curr;
    while (true) {
        auto name = node.data().toString();
        if (name.isEmpty())
            break;
        path.insert(0, name);
        node = node.parent();
    }

    core_ins->postCommand(StoryconceptJumpTo(path));
}

QString StoryconceptsPresent::name() const {
    return NAME(StoryconceptsPresent);
}

QWidget *StoryconceptsPresent::widget() const { return (QWidget *)this; }

using namespace DataModel;
StoryconceptsPresentModel::StoryconceptsPresentModel(Core::AppCore *core)
    : core_ins(core), model_base(new QStandardItemModel),
      detail_backend(new QTextDocument) {
    concept_model = new TreeSyncs<DesNode *>(
        model_base,
        [](DesNode *dbase, QStandardItem *it) -> bool {
            return static_cast<NamedNode *>(dbase)->name()[0] == it->text();
        },
        [](DesNode *dbase, QStandardItem *it) {
            it->setText(static_cast<NamedNode *>(dbase)->name()[0]);
        });
}

StoryconceptsPresentModel::~StoryconceptsPresentModel() {
    delete model_base;
    delete detail_backend;
    delete concept_model;
}

QStandardItemModel *StoryconceptsPresentModel::treeModel() const {
    return model_base;
}

QTextDocument *StoryconceptsPresentModel::detailsBackend() const {
    return detail_backend;
}

void StoryconceptsPresentModel::refreshTree() {
    this->concept_model->presentSync([this](DesNode *dit) {
        if (dit) {
            QList<DesNode *> rets;
            auto children = dit->children();
            for (auto &frag : children) {
                if (frag->typeValue() == NODE_STORYSTRONGPOINT) {
                    rets << frag;
                }
            }

            return rets;
        } else {
            return core_ins->parseCore()->allStoryConcept();
        }
    });
}

void StoryconceptsPresentModel::detailsShow(const QList<QString> &path) {
    QStandardItem *item = nullptr;
    // 迭代路径获取故事链节点
    for (auto &it : path) {
        auto result = OpStream<QStandardItem *>([this, &item](int &cnt, int idx) -> QStandardItem * {
                          if (!item) {
                              cnt = model_base->rowCount();
                              if (cnt <= 0)
                                  return nullptr;
                              return model_base->item(idx);
                          } else {
                              cnt = item->rowCount();
                              if (cnt <= 0)
                                  return nullptr;
                              return item->child(idx);
                          }
                      }).filter([it](QStandardItem *const &xit) {
                            return it == xit->text();
                        }).toList();

        if (!result.size()) {
            throw new SimpleException("指定的Storychain路径无效：" + path.join("/"));
        } else {
            item = result.at(0);
        }
    }

    // 填充详细内容
    QString contents;
    if (item->parent() == nullptr) {
        auto node = core_ins->parseCore()->queryStoryConcept(item->text());

        auto children = node.first()->children();
        for (auto &it : children) {
            if (it->typeValue() != NODE_STORYSTRONGPOINT) {
                contents += it->toString() + "\n";
            }
        }
    } else {
        auto node =
            core_ins->parseCore()->queryStoryConcept(item->parent()->text());
        auto point = core_ins->parseCore()->queryStoryStrongPoint(node.first(),
                                                                  item->text());

        auto children = point.first()->children();
        for (auto &it : children) {
            contents += it->toString() + "\n";
        }
    }
    detail_backend->setPlainText(contents);
}

QString StoryconceptsPresentModel::name() const {
    return NAME(StoryconceptsPresentModel);
}
