//----------------------------------------------------------------------------
///
/// \file   writerLayout.cpp
///
/// \brief  class describing the PCB layout
///
///         For each periphery/bus show to which MCU-pins it is connected
///
/// \date   20201011
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


//---Defines------------------------------------------------------------------


#define  MAKRO_IDENT    35


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


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

#include <QMap>
#include <QFile>
#include <QTextStream>
#include <QSharedPointer>

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

#include "writerLayout.h"
#include "board.h"
#include "app.h"

//--- Implementation ----------------------------------------------------------


int CWriterLayout::writeFiles()
{
   QMap <QString, int> typeCounter;
   QString line;
   QFile file( getBasePath() + "arena_layout.txt");
   if (!file.open(QFile::WriteOnly | QFile::Truncate))
      qFatal("Could not open header file for writing");
   QTextStream out(&file);

      for(const auto &periphery: board().listConst())
   {
      if(periphery->doInstantiate())
      {
         QString type=periphery->getPeripheryType();
         typeCounter[type]++;
      }
   }

   out << "Name: " << pinmux().getApp()->getName() << "\n";

   for(const auto &type: typeCounter.keys() )
   {
      int counter=0;

      for(const auto &periphery: board().listConst())
      {
         CReferencePtr pReference=periphery->getReferenceLink();
         if( periphery->doInstantiate() && ( periphery->getPeripheryType()==type ) )
         {
            out << formatPeriphery(*periphery, pReference.data(), type, counter);
            counter++;
         }
      }
      out << "\n";
   }

   file.close();

   return(0);
}

QString CWriterLayout::peripheralPins(const CPeriphery &periphery, const QString indent)
{
   QString macro;

   if( !periphery.getAttribute( "pin" ).isEmpty() )
   {
      macro+=QString("%1%2\n").arg(indent).arg(periphery.getAttribute( "pin" ) );
   }

   for(const auto &consumes: periphery.getConsumesListConst() )
   {
      macro+=indent;
      macro+=QString("%1").arg(getWiring(consumes));
      macro+="\n";
   }

   return( macro );
}


QString CWriterLayout::formatPeriphery(const CPeriphery &periphery
                                  , const CReference *pReference
                                  , const QString &type, int counter)
{
   QString line;
   QString name, pname;

   if(pReference && (!pReference->getName().isEmpty()))
      name=pReference->getName();
   else
      name=periphery.getName();

   QString pins=peripheralPins( periphery, "      " );
   if(!pins.isEmpty())
   {
      line+=QString("%1 '%2' ID:%3: {\n").arg(type).arg(name).arg(periphery.getId());
      line+=peripheralPins( periphery, "   " );
      line+=QString("}\n");
   }

   return(line);
}

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