#ifndef XMLPROJECTMANAGER_H
#define XMLPROJECTMANAGER_H

#include "libProjectManager_global.h"

#include <QDomDocument>
#include <QObject>
#include "libProjectManager.h"

namespace Project {

    enum class NodeType{
        ROOT,
        GROUP,
        FILE,
    };

    class ProjectNode : public QStandardItem
    {
    public:
        ProjectNode(NodeType t, const QString &name);
        virtual ~ProjectNode() = default;
        virtual NodeType nodeType() const;

        void setFile(const QString &name);
        QString file() const;

    private:
        NodeType type_indi;
        QString filename;
    };


    class LIBPROJECTMANAGER_EXPORT XMLProjectManager
             : public QObject, public Project::ProjectManager
    {
    public:
        XMLProjectManager(QObject *parent = nullptr);
        virtual ~XMLProjectManager();


        // ProjectManager interface
    public:
        virtual QStandardItemModel* model() const override;
        virtual Config::Configration *configraions() const override;
        virtual QDir projectDir() const override;

        virtual QString suffix() const override;
        virtual QString fmtedProjectPath(const QString &project_dir, const QString &name) const override;
        virtual bool isOpen() const noexcept override;
        virtual bool isModified() const noexcept override;
        virtual void openProject(const QString &project_file) override;
        virtual void newProject(const QString &project_dir, const QString &name) override;
        virtual void closeProject() override;
        virtual void save() override;
        virtual QString projectName() const override;
        virtual void resetProjectName(const QString& name) override;

        virtual QModelIndex newPackage(const QList<QString> &path) override;
        virtual QStringList packagePath(const QModelIndex &path) const override;

        virtual QModelIndex signFile(const QModelIndex &package_path, const QString &name, const QString &path_on_disk) override;
        virtual void rename(const QModelIndex &node, const QString &name) override;
        virtual QList<QModelIndex> filesGather(const QModelIndex &node) const override;
        virtual void moveTo(const QModelIndex &node_frompath, const QModelIndex &target_group, int index) override;
        virtual void deleteTarget(const QModelIndex &node_path) override;

        virtual QList<std::tuple<QString, QFileInfo>> filesWithEnds(const QString &suffix) const override;

        virtual QFileInfo queryInfo(const QModelIndex &path) override;
        virtual QModelIndex queryIndex(const QFileInfo &file) override;

    private:
        Config::Configration *const project_config;
        QStandardItemModel*const project_structure;
        QString filepath_store;
        bool open_status;
        QList<QString> all_files_managed;

        void structure_parser(QDomElement struct_elm, ProjectNode *pnode);
        void structure_severlize(ProjectNode *pnode, QDomElement &elm);

        ProjectNode* node_follows(ProjectNode* pnode, const QList<QString> &path_remains);
        QList<std::tuple<QString, QFileInfo>> nodes_search(ProjectNode *pnode, const QString &suffix) const;

        QModelIndex groups_rebuild(ProjectNode *pnode, const QList<QString> &path_remains);


        virtual QModelIndex query_index(const QFileInfo &file, const QModelIndex &base);
    };

}
#endif // XMLPROJECTMANAGER_H
