//----------------------------------------------------------------------------
///
/// \file   formaterCpp.cpp
///
/// \brief  class helping formating some strings for CPP file generation
///
/// \date   20212812
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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

#define  MAKRO_IDENT    35

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


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

#include <QString>

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

#include "formaterCpp.h"
#include <pinmux.h>

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

#if 0
QString CFormaterCpp::getClassName() const
{
   QString className;
   const CConstructor *constructor=m_pool.getConstructor( CPeripheryConstPtr ( &m_periphery) );

   if( constructor && ( ! constructor->getClass().isEmpty() ) )
   {
      className=constructor->getClass();
   }
   else
   {
      QString _type=m_periphery.getPeripheryType().toLower();
      _type[0]=_type[0].toUpper();
      className=QString("C%1").arg(_type);
      int pos;
      while( ( pos = className.indexOf('_') ) !=-1)
      {
         className.remove(pos,1);
         className[pos]=className[pos].toUpper();
      }
   }

   return (className);
}

QString CFormaterCpp::getShortInstanceName() const
{
   QString name;
   QString type { m_periphery.getPeripheryType().toLower() };

   if( m_pReference && (!m_pReference->getName().isEmpty()))
   {
      name=m_pReference->getName();
   }
   else
   {
      name=m_periphery.getName();
   }
   type[0]=type[0].toUpper();

   return { QString("%1%2").arg(type).arg(name) };
}


QString CFormaterCpp::getInstanceName() const
{
   QString name;
   QString start;
   QString type;
   int index;

   if( m_pReference && (!m_pReference->getName().isEmpty()))
   {
      name=m_pReference->getName();
   }
   else
   {
      name=m_periphery.getName();
   }

   type=m_periphery.getPeripheryType().toLower();
   while( ( index=type.indexOf('_') ) >= 0 )
   {
      type.remove(index, 1);
      type[index]=type[index].toUpper();
   }

   CPeripheryConstPtr parent;
   if(m_periphery.getReferenceLink())
   {
      parent=m_periphery.getReferenceLink()->getParent();
   }

   if(parent)
   {
      start=CFormaterCpp(*parent, m_pool).getInstanceName();
      //type[0]=type[0].toUpper();
      start+='_';
   }
   else
   {
      start="m_";
      if(m_pReference->getRelation()==ERelation::placeholder)
      {
         start+="p";
         type[0]=type[0].toUpper();
      }
   }
   return( QString("%1%2%3").arg(start).arg(type).arg(name) );
}


QString CFormaterCpp::getPeripheralArgumentsString(const CPinmux &pinmux) const
{
   QList<QString> argumentList( pinmux.getPeripheralArguments(m_periphery) );
   QString macro;

   for(auto argument: argumentList)
   {
      if(!argument.isEmpty())
      {
         if(macro.length())
            macro+=QString(", \\\n%1").arg(' ', MAKRO_IDENT);
         macro+=argument;
      }
   }

   if(macro.isEmpty())
      macro+="Yes";

   return( macro );
}
#endif

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