/******************************************************************************
** Copyright (c) 2025 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
//! \file DtElementsRecord.h

#pragma once

#include <makArchives/DtSerialize.h>
#include <makArchives/DtElementDefinitionRecord.h>

#include <boost/serialization/unordered_map.hpp>

#include <iostream>
#include <unordered_map>

namespace makArchives
{

//! Record containing a collection of element definition records
//! \ingroup makArchives
class DT_DLL_MAKARCHIVES DtElementsRecord
{
public:
   using ElementDefinitionRecords = std::vector<DtElementDefinitionRecord> ;

   using ElementDefinitionsToFiles = std::unordered_map<std::string, std::string>;

   //! CTOR
   DtElementsRecord();

   //! DTOR
   virtual ~DtElementsRecord();

public:
   //!@{
   //! Modify myElementDefinitions
   void setElementDefinitions(const ElementDefinitionRecords& elementDefinitions);
   void addElementDefinitions(const ElementDefinitionRecords& elementDefinitions);
   void removeElementDefinition(const DtElementDefinitionRecord elementDefinition);
   void replaceOrAddElementDefinition(const DtElementDefinitionRecord& elemRec);
   //!@}
   
   // Mark all these element definitions are belonging to the hierarchy
   void setAllIsHierTo(bool isHier);

   // Update all the records to a file name *if* they don't have one already aka empty
   // (usally done on load so we know which records go with which files)
   void setAllFileName(const std::string& fileName);

   const ElementDefinitionRecords& elementDefinitions() const;
   ElementDefinitionRecords& elementDefinitions();

   //! Set myWriteFilePaths, enables serialisation of myElementDefinitionToFilePathMap
   //! Only used for temporary session back ups
   void setWriteFilePaths( bool shouldWrite );

   //! Set file paths from myElementDefinitionToFilePathMap into myElementDefinitions
   //! Only used for temporary session back ups
   void copyFilePathsToDefinitions();
protected:
   friend class boost::serialization::access;

   template<class Archive>
   void serialize(Archive & ar, const unsigned int version)
   {
      ar & BOOST_SERIALIZATION_NVP(myElementDefinitions);

      if( version == 2 && myWriteFilePaths )
      {
         ar & BOOST_SERIALIZATION_NVP( myElementDefinitionToFilePathMap );
      }
   }

   bool myWriteFilePaths;
   ElementDefinitionRecords myElementDefinitions;
   ElementDefinitionsToFiles myElementDefinitionToFilePathMap;
};
}

BOOST_CLASS_VERSION(makArchives::DtElementsRecord, 2)
