//----------------------------------------------------------------------------
///
/// \file   periphery_list.cpp
///
/// \brief  class describing the periphery of an MCU board
///
/// \date   20180129
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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


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

#include <QString>
#include <QDebug>
#include <QFile>

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

#include "periphery_list.h"
#include "app.h"


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


CPeripheryPtr CPeripheryList::findPeripheryByPeripheryName(
      const QString  &peripheryName)
{
   for (QSharedPointer <CPeriphery> &cperiphery: peripheryList)
   {
      if( (cperiphery->getPeripheryName() == peripheryName) )
      {
         return(cperiphery.data());
      }
   }

   return(nullptr);
}

EMatchMode CPeripheryList::getMatchMode( CReference &reference, const CReference &rootReference ) const
{
   CPeripheryPtr periphery;
   EMatchMode eMatchMode=EMatchMode::undefined;

   if( ! rootReference.getPredefined().isEmpty() )
   {
      eMatchMode=EMatchMode::predefined;
   }
   else if( ! reference.getAlternateFunction().isEmpty() )
   {
      eMatchMode=EMatchMode::alternateFunction;
   }
   else if( ! reference.getPeripheryName().isEmpty() )
   {
      eMatchMode=EMatchMode::peripheryName;
   }
   else if( ! reference.getPeripheryType().isEmpty() )
   {
      eMatchMode=EMatchMode::peripheryType;
   }
   else
   {
      qWarning("No property given in reference to find periphery.");
      qWarning("   Name: %s", qPrintable( reference.getName() ) );
      qFatal("Exit...");
   }
   return(eMatchMode);
}

void CPeripheryList::dump(const QString &title) const
{
   qWarning("Dumping Pinmux: %s", qPrintable(title));

   for(const auto &periphery: peripheryList)
   {
      qWarning() << "   " << periphery->printable();
      for(const auto &consumes: periphery->getConsumesListConst() )
      {
         qWarning() << "      " << consumes.printable() << " -> " <<
                       ( consumes.getPeripheryLink()?
                          QString::number(consumes.getPeripheryLink()->getId()) : "Not-linked" )
                       ;
      }
   }
}

void CPeripheryList::dump(QTextStream &stream) const
{
   stream << "Dumping board pinmux: \n\n";

   for(const auto &periphery: peripheryList)
   {
      stream << "   " << periphery->printable() << "\n";
      for(const auto &consumes: periphery->getConsumesListConst() )
      {
         stream << "      " << consumes.printable() << " -> " <<
                       ( consumes.getPeripheryLink() ?
                          QString::number(consumes.getPeripheryLink()->getId()) : "Not-linked" )
                        << "\n";
      }
   }
}


QSharedPointer <CPeriphery> CPeripheryList::getPeripheryTypeCount( const QString& peripheryType, int count, int used )
{
   count=0;
   used=0;
   QSharedPointer<CPeriphery> free;
   
   for (QSharedPointer <CPeriphery> &cperiphery: peripheryList)
   {
      if( ( cperiphery->getPeripheryType() == peripheryType ) )
      {
         if( cperiphery->isUsed() )
         {
            used++;
         }
         else
         {
            free=cperiphery;
         }
         count++;
      }
   }
   
   return(free);
}


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