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


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


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


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

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

#include "reference.h"
#include "periphery.h"


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


QString CReference::printable() const
{
   QString str;
   str+=QString("Reference: name: '%1', periphery: '%2', type: '%3', id: %4, prefered: '%5', useIrq: '%6', relation: '%7'")
          .arg(getName()).arg(getPeripheryName()).arg(getPeripheryType()).arg(getId()).arg(getPredefined()).arg(getUseIrq()).arg((int)getRelation());
   str+=QString(" -> %1").arg( getPeripheryLink() ? getPeripheryLink()->getId() : 0 );
   return(str);
}

void CReference::dump() const
{
    qWarning("Dumping Reference:");
    CPeripheryTrivia::dump();
    qWarning("   predefined: %s", qPrintable(m_predefined));
    qWarning("   periphery-Link: %d", m_pPeriferyLink ? m_pPeriferyLink->getId() : 0 );
}

void CReference::setPeripheryLink(CPeripheryPtr link)
{
   if( ( ! link ) && (m_pPeriferyLink) )
   {
      //
   }

   m_pPeriferyLink=link;
}

void CReference::link(CPeripheryPtr periphery)
{
   if( getPeripheryLink() )
   {
      dump();
      qFatal("Linking an already linked reference");
   }

   setPeripheryLink(periphery);

   // Linking to nullptr is allowed
   if( periphery )
   {
      if( !periphery->getReferenceLink() )
      {
         periphery->setReferenceLink(this);
      }
   }
}

bool CReference::isExclusive() const
{
   CPeripheryConstPtr parent=getParent();
   bool exclusive=false;
   if(parent)
   {
      exclusive=parent->isExclusive();
   }
   return( (getRelation()==ERelation::exclusive) || exclusive );
}


bool CReference::parseJSON(const QJsonObject &object, const QString boardId)
{
   // if ( object["status"].toString()
   if(!object["predefined"].toObject()[QString("%1").arg(boardId)].isUndefined())
   {
      QString predefined=object[ "predefined" ].toObject()[QString("%1").arg(boardId) ].toString();
      if( predefined == "disabled" )
      {
         return(false);
      }
      else
      {
         setPredefined(predefined);
      }
   }

   if( object["required"].toObject().contains( QString("%1").arg(boardId) ) )
   {
      setRequiredAttribute(object[ "required" ].toObject()[QString("%1").arg(boardId) ].toString());
   }
   else if( object["required"].toObject().contains ( "other" ) )
   {
      setRequiredAttribute(object[ "required" ].toObject()[ "other" ].toString());
   }

   setRelation( object["relation"].toString() );
   setAlternateFunction( object["alternateFunction"].toString() );
   /*
   if(!entries[i2].toObject()["alternateFunction"].toString().isEmpty())
   {
      reference.setAlternateFunction( entries[i2].toObject()["alternateFunction"].toString() );
   }
   */
   /*
   if(!object["alternateFunction"].toString().isEmpty())
   {
      setAlternateFunction( object["alternateFunction"].toString() );
   }
   */

   //reference.setAttributes( jsonObjectToStringMap(object["attributes"].toObject() ) );
   setJsonObject( object );
   addJsonObject( object["attributes"].toObject() );

   return(true);
}


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