#include "command_list.h"
#include "manager_docs.h"
#include "route.h"
#include "DocsManager.h"

using namespace CommandList;
using namespace Components;
using namespace Core;
using namespace MakeTools;
using namespace DataModel;

NewPackage::NewPackage(const QString &path_string)
{
    sequence = path_string.split("/");
}

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

void NewPackage::run(Schedule::CommandsDispatcher *core) const
{
    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));
    vmgr->newPackage(Route::collect(sequence));
}

QString NewPackage::toText() const
{
    return QStringList(sequence).join("/");
}

void NewPackage::fromText(const QString &line)
{
    sequence = line.split("/");
}

NewFile::NewFile(const Route &group, const QString &name, const QString &suffix)
    : name_val(name), suffix(suffix) { group_val = group.links(); }

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

void NewFile::run(Schedule::CommandsDispatcher *core) const
{
    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));
    vmgr->newFile(Route::collect(group_val), name_val, suffix);
}

QString NewFile::toText() const
{
    return (QStringList() << QStringList(group_val).join("/") << name_val << suffix).join(":");
}

void NewFile::fromText(const QString &line)
{
    auto list = line.split(":", QString::SkipEmptyParts);
    group_val = list[0].split("/");
    name_val = list[1];
    suffix = list[2];
}

SaveAll::SaveAll()
{

}

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

void SaveAll::run(Schedule::CommandsDispatcher *core) const
{
    auto vmgr = core->get<DocumentsManager>(NAME(SaveAll));
    vmgr->save();
    vmgr->projectManager()->save();
}

QString SaveAll::toText() const
{
    return "";
}

void SaveAll::fromText(const QString &line)
{

}

OpenProject::OpenProject(const QString &path)
    : project_path(path)
{

}

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

void OpenProject::run(Schedule::CommandsDispatcher *core) const
{
    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));
    vmgr->projectManager()->openProject(project_path);
}

QString OpenProject::toText() const
{
    return project_path;
}

void OpenProject::fromText(const QString &line)
{
    project_path = line;
}

CloseProject::CloseProject()
{

}

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

void CloseProject::run(Schedule::CommandsDispatcher *core) const
{
    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));
    vmgr->save();
    vmgr->close();
    vmgr->projectManager()->save();
    vmgr->projectManager()->closeProject();
}

QString CloseProject::toText() const { return ""; }

void CloseProject::fromText(const QString &line) {}

NewProject::NewProject(const QDir &dir, const QString &name)
    : dir_symbo(dir), name_val(name)
{

}

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

void NewProject::run(Schedule::CommandsDispatcher *core) const
{
    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));
    vmgr->projectManager()->newProject(dir_symbo.absolutePath(), name_val);
}

QString NewProject::toText() const
{
    return QString("%1:%2").arg(dir_symbo.absolutePath(), name_val);
}

void NewProject::fromText(const QString &line)
{
    auto list = line.split(":");
    dir_symbo = QDir(list[0]);
    name_val = list[1];
}

Build::Build(bool fromdisk)
    :all_from_disk(fromdisk)
{

}

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

void Build::run(Schedule::CommandsDispatcher *core) const
{
    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));
    vmgr->build(all_from_disk);
}

QString Build::toText() const
{
    return QString("%1").arg(all_from_disk);
}

void Build::fromText(const QString &line)
{
    all_from_disk = line.toUInt();
}

OpenFile::OpenFile(const Core::Route &path_node)
{
    path_store = path_node.links();
}

OpenFile::OpenFile(const QFileInfo &file_path)
{
    final_file = file_path;
}

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

void OpenFile::run(Schedule::CommandsDispatcher *core) const
{
    auto mgr = core->get<DocumentsManager>(NAME(DocumentsManager));
    if(path_store.size()){
        mgr->openFile(Route::collect(path_store));
    }
    else{
        auto index = mgr->projectManager()->queryIndex(final_file);
        auto route = mgr->pathOf(index);

        mgr->openFile(route);
    }
}

QString OpenFile::toText() const
{
    return path_store.join("/");
}

void OpenFile::fromText(const QString &line)
{
    path_store = line.split("/");
}


StorychainJumpTo::StorychainJumpTo(const QList<QString> &node_path)
{
    jump_path.append(node_path);
}

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

void StorychainJumpTo::run(Schedule::CommandsDispatcher *core) const
{
    auto core_ins = core->get<AppCore>(NAME(AppCore));
    auto chain_ins = core_ins->parseCore()->queryStoryChain(jump_path[0]).first();

    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));
    auto index = vmgr->projectManager()->queryIndex(chain_ins->doc()->filePath());
    auto route = vmgr->pathOf(index);

    vmgr->openFile(route);
    vmgr->activePresentOf(route)->jumpTo(jump_path);
}

QString StorychainJumpTo::toText() const
{
    return jump_path.join("/");
}

void StorychainJumpTo::fromText(const QString &line)
{
    jump_path=line.split("/");
}

#include "storyboardspresent.h"
#include "storychainspresent.h"
#include "storyunitspresent.h"
using namespace Components;
using namespace DataModel;

StorychainDetailShow::StorychainDetailShow(const QList<QString> &chain_path)
    : chain_path(chain_path)
{

}

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

void StorychainDetailShow::run(Schedule::CommandsDispatcher *core) const
{
    auto backend = core->get<StorychainsPresentModel>(NAME(StorychainsPresentModel));
    backend->showDetails(chain_path);
}

QString StorychainDetailShow::toText() const
{
    return chain_path.join("/");
}

void StorychainDetailShow::fromText(const QString &line)
{
    chain_path = line.split("/");
}

StoryunitDetailShow::StoryunitDetailShow(const QList<QString> &path) {
    unit_nav.append(path);
}

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

void StoryunitDetailShow::run(Schedule::CommandsDispatcher *core) const {
    auto backend =
        core->get<StoryunitsPresentModel>(NAME(StoryunitsPresentModel));
    backend->details_show(unit_nav);
}

QString StoryunitDetailShow::toText() const { return unit_nav.join("/"); }

void StoryunitDetailShow::fromText(const QString &line) {
    unit_nav = line.split("/");
}

StoryunitJumpTo::StoryunitJumpTo(const QList<QString> &path) {
    unit_nav = path;
}

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

void StoryunitJumpTo::run(Schedule::CommandsDispatcher *core) const {
    auto core_ins = core->get<AppCore>(NAME(AppCore));
    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));

    auto unit_ins = core_ins->parseCore()->queryStoryUnit(unit_nav[0]).first();
    auto index =
        vmgr->projectManager()->queryIndex(unit_ins->doc()->filePath());
    auto pjt_path = vmgr->converter(index);
    vmgr->openFile(pjt_path);
    vmgr->activePresentOf(pjt_path)->jumpTo(unit_nav);
}

QString StoryunitJumpTo::toText() const { return unit_nav.join("/"); }

void StoryunitJumpTo::fromText(const QString &line) {
    unit_nav = line.split("/");
}

#include "storyconceptspresent.h"
StoryconceptDetailShow::StoryconceptDetailShow(const QList<QString> &path) {
    navi_path.append(path);
}

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

void StoryconceptDetailShow::run(Schedule::CommandsDispatcher *core) const {
    auto vmgr =
        core->get<StoryconceptsPresentModel>(NAME(StoryconceptsPresentModel));
    vmgr->detailsShow(navi_path);
}

QString StoryconceptDetailShow::toText() const { return navi_path.join("/"); }

void StoryconceptDetailShow::fromText(const QString &line) {
    navi_path = line.split("/");
}

StoryconceptJumpTo::StoryconceptJumpTo(const QList<QString> &path) {
    navi_path.append(path);
}

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

void StoryconceptJumpTo::run(Schedule::CommandsDispatcher *core) const {
    auto core_ins = core->get<AppCore>(NAME(AppCore));
    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));

    auto cept_ins =
        core_ins->parseCore()->queryStoryConcept(navi_path[0]).first();
    auto cept_doc = cept_ins->doc();
    auto idx = vmgr->projectManager()->queryIndex(cept_doc->filePath());
    auto route = vmgr->converter(idx);
    vmgr->openFile(route);
    vmgr->activePresentOf(route)->jumpTo(navi_path);
}

QString StoryconceptJumpTo::toText() const { return navi_path.join("/"); }

void StoryconceptJumpTo::fromText(const QString &line) {
    navi_path = line.split("/");
}

StoryboardJumpTo::StoryboardJumpTo(const QList<QString> &path) {
    navi_path.append(path);
}

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

void StoryboardJumpTo::run(Schedule::CommandsDispatcher *core) const {
    auto core_ins = core->get<AppCore>(NAME(AppCore));

    auto unit_ins = core_ins->parseCore()->queryStoryUnit(navi_path[0]).first();
    auto unit_doc = unit_ins->doc();

    auto vmgr = core->get<DocumentsManager>(NAME(DocumentsManager));

    auto index = vmgr->projectManager()->queryIndex(unit_doc->filePath());
    auto route = vmgr->converter(index);
    vmgr->openFile(route);
    vmgr->activePresentOf(route)->jumpTo(navi_path);
}

QString StoryboardJumpTo::toText() const { return navi_path.join("/"); }

void StoryboardJumpTo::fromText(const QString &line) { navi_path.append(line.split("/")); }

StoryboardDetailShow::StoryboardDetailShow(const QList<QString> &path) {
    navi_path.append(path);
}

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

void StoryboardDetailShow::run(Schedule::CommandsDispatcher *core) const {
    auto vm = core->get<StoryboardsPresentModel>(NAME(StoryboardsPresentModel));
    vm->detailShow(navi_path);
}

QString StoryboardDetailShow::toText() const { return navi_path.join("/"); }

void StoryboardDetailShow::fromText(const QString &line) {
    navi_path = line.split("/");
}

CompVisible::CompVisible(Components::PresentHost *host, PresentBase *target, bool state) : host_ins(host), target_ins(target), state_val(state) {
    path = Route::collect(QList<QString>() << target->name());
}

QString CompVisible::name() const { return QString("/%1[%2]").arg(NAME(CompVisible), target_ins->name()); }

void CompVisible::run(Schedule::CommandsDispatcher *core) const {
    target_ins->widget()->setVisible(state_val);
    if (state_val) {
        host_ins->append(target_ins, path);
    } else {
        host_ins->remove(path);
    }
}

QString CompVisible::toText() const { return QString("%1/%2").arg(state_val).arg(path.links().join(":")); }

void CompVisible::fromText(const QString &line) {
    auto list = line.split("/");
    state_val = list[0].toUInt();
    path = Route::collect(list[1].split(":"));
}
