//----------------------------------------------------------------------------
///
/// \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 "formaterCpp.h"

#define TEST_ALL
#define STOP_ON_FAIL

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

#if 1
TEST_CASE( "Fabricate", "[default]" ) {
   CPinmux pinmux;
   CApp app;
   CBoard board;

   SECTION( "find periphery by alternate function" )
   {
      const char *boardString=
      R"JSON(
      {
          "name": "board_placeholder",
          "version": "1",
          "peripheries": [
             { "type": "GPIO", "periphery": "PA2" },
             { "type": "GPIO", "periphery": "PA3" },
             { "type": "GPIO", "periphery": "PB9" },
             {
                 "type": "OUTPUT", "name": "OUTPUT", "relation": "fabricate",
                 "consumes": [   { "type": "GPIO", "name": "OUTPUT" }   ]
             }
          ],
          "constructors": [
              {
                  "type": "GPIO",
                  "constructorArgument": "eGpioPeripheral<PeripheryBlock>, eGpioPin<PeripheryNumber>",
                  "attributeName": "<unlikely>",
                  "arguments": [
                         { "type": "fixed", "value": "eGpioPeripheral<PeripheryBlock>" },
                         { "type": "fixed", "value": "eGpioPin<PeripheryNumber>" }
                  ]
              },
              {   "type": "OUTPUT", "constructorArgument": "",
                    "arguments": [
                           { "type": "consumes", "value": "direct" }
                    ]
              }
         ],
         "final": "yes"
      }
      )JSON";

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

      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "canio",
                   "periphery": [
                      { "type": "OUTPUT", "name": "1" },
                      { "type": "OUTPUT", "name": "2" },
                      { "type": "OUTPUT", "name": "3" }
                    ]
               }
      )JSON" ), "intern" );

      pinmux.setBoardApp(board, app);
      int status=0;
      try
      {
         pinmux.resolveApp();
      }
      catch(...)
      {
         status=1;
      }
      REQUIRE ( status == 0);
      CConstructorPool constructors;
      constructors.loadByteArray( QByteArray(boardString), "intern" );
      CWriterPlatform platform(pinmux, constructors, "");

      CPeripheryPtr p;
      QString prevArguments;
      for(int i1=0; i1<3; i1++)
      {
         p=app.getReferenceList()[i1].getPeripheryLink();

         qWarning() << "###: " << platform.getInstanceName( *p ) << "/" << p->getId() << platform.getPeripheralArgumentsString( *p );
         REQUIRE ( platform.getInstanceName( *p ) == QString("m_output%1").arg(i1+1) );
         if(!prevArguments.isEmpty())
         {
            REQUIRE ( prevArguments != platform.getPeripheralArgumentsString( *p ) );
         }
         prevArguments=platform.getPeripheralArgumentsString( *p );
      }

   }

}
#endif

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