#ifndef INCLUDED_ORDERTXT_
#define INCLUDED_ORDERTXT_

#include <iosfwd>
#include <string>
#include <unordered_map>

#include "../typeselector/typeselector.h"
#include "../err/err.h"

class OrderTxt: private TypeSelector, private Err
{
    using Extension = std::pair<Type, std::string>;

    static Extension s_extension[];
    static Extension *const s_lastExtension;

    std::string d_path;         // pathname of the current order.txt 

    Map d_map;                  // Info records for all exercises 
    Map::iterator d_current;
    
    public:
                                        // already in the path-dir
        bool read(std::string const &path, size_t nr); 
        bool contains(std::string const &name) const;

        void prefix(std::string const &path);

        Map const &map() const;

    private:
        std::ostream &errIdx(size_t idx) const;

        bool readOrderTxt(ElementVect &entry);
        bool fill(ElementVect &entry);
        void setType(Element &element);

        bool inspect(ElementVect const &entry);
        void inspectSequence(ElementVect const &vect) const;

        void visit(VectConstIterPair const &pair) const;
        bool inspectHdr(ConstIterVectIter *hdrIter, bool sawHdr, 
                        ConstIterVectIter const &iter,
                        ConstIterVectIter const &end) const;
        bool inspectIH(bool *sawINL, ConstIterVectIter *ihIter, bool sawIH, 
                       ConstIterVectIter const &hdrIter, 
                       ConstIterVectIter const &iter,
                       VectConstIterPair const &pair) const;
        bool inspectINL(ConstIterVectIter *hdrIter, ConstIterVectIter *ihIter,
                        bool sawINL, ConstIterVectIter const &iter,
                        ConstIterVectIter const &end) const;
        void inspectMain(ConstIterVectIter const &iter, 
                         ConstIterVectIter const &end) const;

        static ConstIterVectIter find(ConstIterVectIter const &begin,
                                      ConstIterVectIter const &end,
                                      bool (*checkFun)(Element const &));

        static void setExtensionField(Type type, std::string const &value);
};

inline OrderTxt::Map const &OrderTxt::map() const
{
    return d_map;
}
        
#endif



