//----------------------------------------------------------------------------
///
/// \file   writerPlatform.h
///
/// \brief  See implementation for further details
///
/// \date   20190926
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

#include <QList>

//---Own------------------------------

#include <pinmux.h>
#include <board.h>
#include <writer.h>
#include <constructor.h>


//--- Declaration -------------------------------------------------------------


class CWriterPlatform
      : public CWriter
{
   private:
      QList <QString> typeList;
      CConstructorPool &m_constructorPool;
      QString m_filePostfix;
      QString m_postfix;

   public:
      explicit CWriterPlatform(CPinmux &pinmux, CConstructorPool &constructorPool, const QString basePath);
      void initFiles();
      void init()
      {
         createSortedTypesList();
         m_constructorPool.setPathList( pinmux().getBoard()->getPathList() );
         return;
      }
      QString getImplementationFileName();
      QString getHeaderFileName();
      void createSortedTypesList();
      virtual int writeFiles() override;
      int writeHeader();
      int writeImplementation();
      void printPreInitFunctions(QTextStream& out) const;
      void printPostInitFunctions(QTextStream& out) const;
      QString getClassName( const CPeriphery &periphery ) const;
      QString getShortInstanceName( const CPeriphery &periphery ) const;
      QString getInstanceName( const CPeriphery &periphery ) const;

      const QString getConsumingArguments( const CPeriphery &periphery, bool indirect ) const;
      const QString getConstructingArguments(const CPeriphery &periphery, CPeriphery::EBinding binding ) const;

      QList<QString> getPeripheralArguments( const CPeriphery &periphery ) const;
      QString getPeripheralArgumentsString( const CPeriphery &periphery ) const;

      QString formatPeripheryDefinition(const CPeriphery &periphery
               , const CReference *pReference, const QString &type);
      QString formatPeripheryConstructor(const CPeriphery &periphery
               , const CReference *pReference, const QString &type, int counter);
      QString macroLine(const QString &name, const QString &value);
      QString peripheralPins(const CPeriphery &periphery);

      QString getImplementation(const CPeriphery &periphery) const;
      QString getDefines(const CPeriphery &periphery) const;
      //CConstructorPool &getConstructorPool(){return(m_constructorPool);};
      const CConstructor *getConstructor(CPeripheryConstPtr periphery) const
      {
         return(m_constructorPool.getConstructor(periphery));
      }
      bool loadByteArray(const QByteArray &array, const QString &fileName)
      {
         return(m_constructorPool.loadByteArray(array, fileName) );
      }
      const QString &fillAttributes(const CPeriphery &periphery, QString &arguments) const;
      const CReference *getSubPeriphery(const CPeriphery &startPeriphery, const QString indexText) const;
      QString replaceSubLabels(QString text, const CPeriphery &periphery, const CConstructor *constructor) const;
      void fillMap(QMap<QString, QString> &map, const QStringList &initFunctions
                   ,const CPeriphery &periphery, const CConstructor &constructor );
      void setPostfix(QString postfix)
      {
         m_postfix=postfix;
         if( ! postfix.isEmpty() )
         {
            m_filePostfix="_"+postfix.toLower();
         }
         else
         {
            m_filePostfix.clear();
         }
         return;
      };
};


//---fin----------------------------------------------------------------------
