//----------------------------------------------------------------------------
///
/// \file   main.cpp
///
/// \brief  main file for pinmux
///
/// \date   20180129
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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


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

#if defined ( CATCH2 )
#include <catch2/catch_test_macros.hpp>
#elif defined ( CATCH )
#include <catch/catch.hpp>
#else
#error "Either 'catch' or 'catch2' has to be installed"
#endif
#include <QDebug>

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

#include "../src/board.h"
#include "../src/app.h"
#include "../src/pinmux.h"
#include "writerPlatform.h"
#include "writerConfigHeader.h"
#include "writerDesc.h"
#include "formaterCpp.h"

#define TEST_ALL
#define STOP_ON_FAIL

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


TEST_CASE( "Predefined_2", "[default]" )
{
   CPinmux pinmux;
   CApp app;
   CBoard board;

   SECTION( "Predefined with fabricated periphery in prefered" )
   {
      const char *boardString=
      R"JSON(
      {
          "name": "myboard_prefered",
          "version": "1",
          "peripheries": [
                  { "type": "GPIO",   "periphery": "PA12", "pin": "D1" },
                  { "type": "GPIO",   "periphery": "PA11", "pin": "D0" },
                  { "type": "GPIO",   "periphery": "PA13", "pin": "D2" },
                  { "type": "GPIO",   "periphery": "PA14", "pin": "D2" },
                  {
                     "type": "LED",
                     "periphery": "LED_FAB",
                     "relation": "fabricate",
                     "consumes": [
                         { "type": "GPIO", "name": "X" }
                     ]
                  }
         ],
         "constructors": [
           {
               "type": "LED",
               "arguments": [ { "type": "consumes", "value": "direct" } ]
           },
           {
               "type": "GPIO",
               "arguments": [
                  { "type": "fixed", "value": "eGpioPeripheral<PeripheryBlock>" },
                  { "type": "fixed", "value": "eGpioPin<PeripheryNumber>" }
               ]
           }
         ],
         "final": "yes"
      }
      )JSON";

      app.clear();
      board.clear();
      app.setBoardId("myboard");
      board.loadByteArray( QByteArray(boardString), "predefined" );

      // The fabricated-mark like '$224' should get removed automatically
      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "canio",
                   "periphery": [
                      { "type": "LED", "name": "b1",
                        "predefined": { "myboard": "LED_FAB[224] PA12" }
                      },
                      { "type": "LED", "name": "b2",
                        "predefined": { "myboard": "LED_FAB PA11" }
                      },
                      { "type": "LED", "name": "b3",
                        "predefined": { "myboard": "LED_FAB[6] PA13" }
                      }
                    ]
               }
      )JSON" ), "intern" );

      pinmux.setBoardApp(board, app);
      pinmux.resolveApp();

      CConstructorPool constructors;
      CReferencePtr ptr;
      QString args;
      QString predefined;
      constructors.loadByteArray( QByteArray(boardString), "intern" );

      CWriterPlatform platform(pinmux, constructors, "");
      CWriterDesc desc(pinmux, "unit_tests_desc.txt");

      ptr=app.findReferenceByName( QString("b1") );
      REQUIRE( ptr );
      args= platform.getPeripheralArgumentsString( *ptr->getPeripheryLink() );
      predefined=desc.predefined( *ptr->getPeripheryLink() );
      INFO( "Is: " << qPrintable( args ) );
      REQUIRE( platform.getPeripheralArgumentsString( *ptr->getPeripheryLink() )
               == "eGpioPeripheralA, eGpioPin12");
      INFO( "Predefined: " << qPrintable( predefined ) );
      REQUIRE( predefined == "LED_FAB PA12");

      ptr=app.findReferenceByName( QString("b2") );
      REQUIRE( ptr );
      args= platform.getPeripheralArgumentsString( *ptr->getPeripheryLink() );
      predefined=desc.predefined( *ptr->getPeripheryLink() );
      INFO( "Is: " << qPrintable( args ) );
      REQUIRE( platform.getPeripheralArgumentsString( *ptr->getPeripheryLink() )
               == "eGpioPeripheralA, eGpioPin11");
      INFO( "Predefined: " << qPrintable( predefined ) );
      REQUIRE( predefined == "LED_FAB PA11");

      ptr=app.findReferenceByName( QString("b3") );
      REQUIRE( ptr );
      args= platform.getPeripheralArgumentsString( *ptr->getPeripheryLink() );
      predefined=desc.predefined( *ptr->getPeripheryLink() );
      INFO( "Is: " << qPrintable( args ) );
      REQUIRE( platform.getPeripheralArgumentsString( *ptr->getPeripheryLink() )
               == "eGpioPeripheralA, eGpioPin13" );
      INFO( "Predefined: " << qPrintable( predefined ) );
      REQUIRE( predefined == "LED_FAB PA13");

   }

}


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