//----------------------------------------------------------------------------
///
/// \file   peripheryTrivia.cpp
///
/// \brief  foo
///
/// \date   20180215
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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


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

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

#include "periphery.h"
#include <helpers.h>


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


int CPeripheryTrivia::idCounter=1000;


void CPeripheryTrivia::dump()  const
{
    qWarning("   name: %s", qPrintable(m_name));
    qWarning("   periphery name: %s", qPrintable(m_peripheryName));
    qWarning("   periphery type: %s", qPrintable(m_peripheryType));
    qWarning("   relation: %d", (int)m_relation );
    qWarning("   attributes:" );
    for( auto o : getJsonObject() )
    {
       qWarning("      %s = %s", "?", qPrintable( o.toString() ));
    }
}


bool CPeripheryTrivia::matchesType(const QString &type) const
{
   if(getPeripheryType()==type)
   {
      return(true);
   }

   if( getAttribute( "like" ) == type )
   {
      return(true);
   }

   return(false);
}


QString CPeripheryTrivia::getPeripheryBlock( ) const
{
   QString peripheryBlock="X";
   QString peripheryName=getPeripheryName();
   if(peripheryName.length()>=2)
   {
      peripheryBlock=peripheryName[1];
   }
   
   return(peripheryBlock);
}


QString CPeripheryTrivia::getPeripheryNumber( ) const
{
   QString peripheryNumber="X";
   QString peripheryName=getAttribute( "instance" );
   
   if( peripheryName.isEmpty() )
   {
      peripheryName=getPeripheryName();
   }
   
   if(!peripheryName.isEmpty())
   {
      // Find periphery number string in periphery name
      int start=peripheryName.indexOf('.')-1;
      if (start<=0)
         start=peripheryName.size()-1;
      int len=1;
      while( start && peripheryName[start-1].isDigit() )
      {
         start--;
         len++;
      }
      
      // There is periphery that has no number in in its instance name.
      // For example "CAN". But the number still has to be "1"
      if( ! peripheryName[start].isDigit() )
      {
         peripheryNumber="1";
      }
      else
      {
         peripheryNumber=peripheryName.mid(start,len);
      }
   }
   return(peripheryNumber);
}


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