#pragma once

#include "XSyntaxBase.h"
#include "ParseFrame.h"

namespace Parse {
	/**
	 * 故事单元.
	 */
    class NodeStoryUnit : public Result::NamedNode
	{
	public:
        explicit NodeStoryUnit(Result::DocCore *doc, const QString &name);
		virtual ~NodeStoryUnit() = default;

		// 通过 NamedNode
        virtual QString name() const override;

		// 通过 DesNode 继承
		inline virtual int depth() const override {
			return 0;
		}
        virtual Result::DocCore * document() const override;
		virtual int typeValue() const override;
		virtual DesNode * parent() const override;
		virtual void appendChild(DesNode * ins) override;
		virtual QList<DesNode*> children() const override;
		virtual bool check(QList<QString>& reasons) const override;
		virtual QString toString() const override;

	private:
        Result::DocCore *doc_ref;
		QString name_store;

		QList<DesNode*> children_nodes;
	};

	/** 故事单元解析器 */
	class NodeStoryUnitParser : public Syntax::XSyntaxBase
	{
	public:
        NodeStoryUnitParser(Result::ProjectCore *core);

        Result::ProjectCore* project() const;

	private:
        Result::ProjectCore *const pjt_core;
	};

	/**
	 * 故事情节节点.
	 */
    class NodeStoryFragment : public Result::NamedNode
	{
	public:
		explicit NodeStoryFragment(NodeStoryUnit* unit, const QString &name);
		virtual ~NodeStoryFragment() = default;

		// 通过 NamedNode 继承
		inline virtual int depth() const override {
			return unit_ins->depth() + 1;
		}
        virtual Result::DocCore * document() const override;
		virtual int typeValue() const override;
		virtual DesNode * parent() const override;
		virtual void appendChild(DesNode * ins) override;
		virtual QList<DesNode*> children() const override;
		virtual bool check(QList<QString>& reasons) const override;
		virtual QString toString() const override;
		virtual QString name() const override;

	private:
		NodeStoryUnit *const unit_ins;
		QString name_store;

		QList<DesNode*> children_nodes;
	};

	/**
	 * 故事情节解析器.
	 */
	class NodeStoryFragmentParser : public Syntax::XSyntaxBase
	{
	public:
		NodeStoryFragmentParser(NodeStoryUnitParser *pparser);

		NodeStoryUnitParser * nodeStoryUnitParser() const;
        Result::DocCore * docRef() const;

	private:
		NodeStoryUnitParser *const parent_parser;
	};

	/**
	 * 故事脉络节点引用.
	 */
    class NodeStoryPointRefer : public Result::DesNode
	{
	public:
        explicit NodeStoryPointRefer(DesNode *parent, const QString &chain, const QString &point);
        virtual ~NodeStoryPointRefer() = default;

		// 通过 DesNode 继承
		inline virtual int depth() const override {
			return parent_ins->depth() + 1;
		}
        virtual Result::DocCore * document() const override;
		virtual int typeValue() const override;
		virtual DesNode * parent() const override;
		virtual void appendChild(DesNode * ins) override;
		virtual QList<DesNode*> children() const override;
		virtual bool check(QList<QString>& reasons) const override;
		virtual QString toString() const override;

	private:
		DesNode *const parent_ins;
		QString chain_name;
		QString point_name;
	};

    /**
     * @brief 特殊用途的解析器，目标是移除指定标记的
     */
    class NodeStoryLinePrefixParser : public Syntax::XSyntaxBase
    {
    public:
        NodeStoryLinePrefixParser(SyntaxParser* pparent);

        void setMatchEnable(bool v);

    private:
        Syntax::ParseRule *critical_rule;
    };

    class NodeStoryMixedDesGroupParser : public Syntax::XSyntaxBase
	{
	public:
        NodeStoryMixedDesGroupParser(Syntax::XSyntaxBase * parent);

        virtual void reset() override;

	private:
		Syntax::XSyntaxBase *const parent_parser;
        NodeStoryLinePrefixParser *const unique_tidy;
	};


    class NodeStoryTextSpanParser : public Syntax::XSyntaxBase
    {
    public:
        NodeStoryTextSpanParser(Syntax::XSyntaxBase *parent);
    private:
        Syntax::XSyntaxBase *const parent_parser;
    };

    class NodeStoryPointReferParser : public Syntax::XSyntaxBase
    {
    public:
        NodeStoryPointReferParser(Syntax::XSyntaxBase *parent);

    private:
        Syntax::XSyntaxBase *const parent_parser;
    };

	/**
	 * 故事情节有序注释，单独解析
     * #注解 注解名 序列名 值 {
     *      注解段落
     *      注解段落
     * }
	 */
    class NodeStoryFragmentComment : public Result::NamedNode
	{
	public:
		NodeStoryFragmentComment(DesNode *parent, const QString &order, const QString &name);
		virtual ~NodeStoryFragmentComment() = default;

		QString order() const;

		// 通过 NamedNode 继承
		inline virtual int depth() const override
		{
			return parent_ins->depth() + 1;
		}
        virtual Result::DocCore * document() const override;
		virtual int typeValue() const override;
		virtual DesNode * parent() const override;
		virtual void appendChild(DesNode * ins) override;
		virtual QList<DesNode*> children() const override;
		virtual bool check(QList<QString>& reasons) const override;
		virtual QString toString() const override;
		virtual QString name() const override;

        QString orderValue() const;
        void resetValue(const QString &value);

	private:
		DesNode *const parent_ins;
		QString name_store;
		QString order_name;
        QString order_value;

		QList<DesNode*> children_nodes;
	};

    class NodeStoryFragmentCommentParser : public Syntax::XSyntaxBase
    {
    public:
        NodeStoryFragmentCommentParser(Syntax::SyntaxParser* pparser);
    private:
        Syntax::SyntaxParser *const pparser;
    };


    class StoryUnitDocumentParser : public Syntax::ParseFrame
	{
	public:
        StoryUnitDocumentParser(Result::ProjectCore *ins);
	};
}
