#include "StoryBoardDocumentParser.h"
#include "StoryChainDocumentParser.h"
#include "StoryOutlineDocumentParser.h"

using namespace Parse;
using namespace Parse::Result;
using namespace Lex;
using namespace Syntax;
using namespace Syntax::Defines;

StoryOutlineDocumentParser::StoryOutlineDocumentParser(Result::ParseCore *core)
{
    appendTokensDefine({
                        {"{叙述定义}","#叙述"},
                        {"{情节引用}", "@情节"},
                        {"{描述文本}", "[^#@\\{\\}\\n]+"},
                        {"{左界限}","\\{"},
                        {"{右界限}","\\}"},
                        {"{换行符}","\\n"},
                    }, "{无法识别}");

    appendParser(new NodeStoryDepictionParser(core));
}

NodeStoryDepictionParser::NodeStoryDepictionParser(Result::ParseCore *core)
    : XSyntaxBase("叙事节点")
{
    set_common_expression("::换行前缀", {Elm("{换行符}"),Elm("{换行符}", true)});
    auto rule = addRule("进入卷宗叙事", 0, [this](const QList<LexResult> &seqs, int cnt)->ParseResult{
        auto nmunit = seqs[cnt-1];
        auto defunit = seqs[cnt-2];

        auto node = new NodeStoryDepiction(docRef(), nmunit.Text);
        refocusNode(node);

        auto word0 = new Words(node, docRef(), defunit.Text, defunit.StartRow, defunit.StartCol);
        auto word1 = new Words(node, docRef(), nmunit.Text, nmunit.StartRow, nmunit.StartCol);
        docRef()->append(word0);
        docRef()->append(word1);

        return ParseResult::SelfManipulate;
    });
    rule->addExpression("基本叙述", {Elm("{叙述定义}"), Elm("{描述文本}", true)});
    rule->addExpression("拓展叙述", {Exp("{::换行前缀}"), Exp("基本叙述")});


    rule = addRule("进入成分解析", 1, [this](const QList<LexResult> &seqs, int cnt)->ParseResult {
        auto node = currNode();

        auto word = new Words(node, docRef(), seqs[cnt - 1].Text, seqs[cnt - 1].StartRow, seqs[cnt - 1].StartCol);
        docRef()->append(word);

        return ParseResult::EnterNext;
    });
    rule->addExpression("基础成分解析", { Elm("{左界限}", true) });
    rule->addExpression("拓展成分解析", { Exp("::换行前缀"), Exp("基础成分解析", true) });


    rule = addRule("跳出成分解析", 2, [this](const QList<LexResult> &seqs, int cnt)->ParseResult {
        auto node = currNode();

        auto word = new Words(node, docRef(), seqs[cnt - 1].Text, seqs[cnt - 1].StartRow, seqs[cnt - 1].StartCol);
        docRef()->append(word);

        return ParseResult::Completed;
    });
    rule->addExpression("基础跳出解析", { Elm("{右界限}", true) });
    rule->addExpression("拓展跳出解析", { Exp("::换行前缀"), Exp("基础跳出解析", true) });

    addChild(QList<SyntaxParser*>()
             << new NodeStoryPureTextDesGroupParser(this)
             << new NodeStoryFragmentReferParser(this));
}

NodeStoryDepiction::NodeStoryDepiction(Result::DocCore *doc, const QString &name)
    : Result::NamedNode(name, doc, NODE_STORYDEPICTION, nullptr){}

bool NodeStoryDepiction::check(QList<ErrorMessage> &reasons) const
{
    auto nodes = doc()->core()->queryStoryDepiction(name()[0]);

    if(nodes.size() > 1){
        ErrorMessage ins(this);
        ins.Reason = "重复定义叙述";
        ins.Text = name()[0];
        ins.FilePath = doc()->filePath();
        ins.CodeRow = refered().first()->row();
        ins.CodeCol = refered().first()->column();
    }

    return nodes.size() == 1;
}

QString NodeStoryDepiction::toString() const
{
    QString des = "#叙述 " + name()[0] + " {";
    for(auto &c : children()){
        des += "\n" + c->toString();
    }
    return des +"\n}";
}
