//----------------------------------------------------------------------------
///
/// \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 <QFile>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QDebug>
#include <QCommandLineParser>
#include <QFileInfo>
#include <QDir>
#include <QString>

//--- 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------------------------------------------------------------


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

   SECTION( "find periphery by alternate function" )
   {
      const char *boardString=
      R"JSON(
      {
          "name": "board_alternate_function",
          "version": "1",
          "peripheries": [
             { "periphery": "PA2", "alternateFunctions": { "USART2_TX": "GPIO_AF2_UART2", "CAN1_TX": "GPIO_AF1_UART2"} },
             { "periphery": "PA3", "alternateFunctions": { "USART2_RX": "GPIO_AF2_UART2" } },
             { "periphery": "PB9", "alternateFunctions": { "USART2_RX": "GPIO_AF7_UART2" } },
             {
                  "type": "UART",
                  "name": "uartName",
                  "periphery": "uartName",
                  "consumes":
                  [
                     { "type": "GPIO", "alternateFunction": "USART2_TX", "functionX": "ABC" },
                     { "type": "GPIO", "alternateFunction": "USART2_RX"}
                  ],
                 "attributes": {
                     "configBlock": "uartConfig_UART2"
                 }
             }
          ],
          "constructors": [
                 {
                     "type": "UART",
                     "arguments": [
                             { "type": "fixed", "value": "<attribute:configBlock>" }
                     ],
                     "implementation": [
                        "struct {",
                        "   ePeriphery<periphery:0>,",
                        "   ePin<pin:0>,",
                        "   <alternate function:0>,",
                        "   ePeriphery<periphery:1>,",
                        "   ePin<pin:1>,",
                        "   <alternate function:1>",
                        "}"
                     ]
                 },
                 {
                     "type": "GND",
                     "arguments": [
                         { "type": "GND", "name": "A1" },
                         { "type": "GND", "name": "A1" }
                     ]
                 }
         ],
         "final": "yes"
      }
      )JSON";

      app.clear();
      board.clear();
      app.setBoardId("myboard");
      board.loadByteArray( QByteArray(boardString), "function_uart" );
      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "canio",
                   "periphery": [
                      { "type": "UART", "name": "1",
                           "attributes": {
                               "speedo": "24Mhz"
                           }
                      }
                    ]
               }
      )JSON" ), "intern" );

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

      CPeripheryPtr p=board.findPeripheryByPeripheryName( QString("uartName") );
      REQUIRE ( p != nullptr);
      CConstructorPool constructors;
      constructors.loadByteArray( QByteArray(boardString), "intern" );
      CWriterPlatform writer(pinmux, constructors, "");

      qWarning() << ">>>" << writer.getPeripheralArgumentsString( *p );
      REQUIRE( writer.getPeripheralArgumentsString( *p )
               == "uartConfig_UART2");

      qWarning() << ">>>" << writer.getImplementation(*p);
      REQUIRE( writer.getImplementation(*p) == "struct {\n   ePeripheryA,\n   ePin2,\n   GPIO_AF2_UART2,\n   ePeripheryA,\n   ePin3,\n   GPIO_AF2_UART2\n}\n\n");

      REQUIRE( true == true );
   }

}


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