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

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

#include "../src/board.h"
#include "../src/app.h"
#include "../src/pinmux.h"
#include "writerPlatform.h"

#define TEST_ALL
#define STOP_ON_FAIL

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


TEST_CASE( "Placeholder", "[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": "UART", "name": "uartName", "periphery": "uartName",
                  "consumes":
                  [
                     { "periphery": "PA2" },
                     { "periphery": "PA3" }
                  ]
             },
             {
                  "type": "VART", "name": "vartName", "periphery": "vartName",
                  "consumes":
                  [
                     { "periphery": "PA2" },
                     { "periphery": "PA3" }
                  ]
             }
          ],
          "constructors": [
                 {
                     "type": "UART",
                     "arguments": [
                             { "type": "fixed", "value": "Whatever"}
                     ]
                 },
                 {
                     "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), "placeholder_uart" );
      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "canio",
                   "__periphery": [
                      { "type": "UART", "name": "1", "relation": "placeholder"}
                   ],
                   "periphery": [
                      { "type": "UART", "name": "1", "relation": "placeholder", "attributes": {"overlap": "foo"}},
                      { "type": "UART", "name": "2", "relation": "placeholder", "attributes": {"overlap": "faa"}}
                    ]
               }
      )JSON" ), "intern" );

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

      app.clear();
      board.clear();
      app.setBoardId("myboard");
      board.loadByteArray( QByteArray(boardString), "intern_SPI" );
      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "canio",
                   "__periphery": [
                      { "type": "UART", "name": "1", "joint": "placeholder"}
                   ],
                   "periphery": [
                      { "type": "UART", "name": "1", "relation": "placeholder", "attributes": {"overlap": "uartUart"}},
                      { "type": "VART", "name": "2", "relation": "placeholder", "attributes": {"overlap": "uartUart"}}
                    ]
               }
      )JSON" ), "intern" );

      pinmux.setBoardApp(board, app);
      //pinmux.loadByteArray( QByteArray(boardString), "intern" );

      status=0;
      try
      {
         pinmux.resolveApp( CPinmux::EMethod::linear );
      }
      catch(...)
      {
         status=1;
      }
      //pinmux.errorExit("Foooo");
      REQUIRE( status == 0 );

      app.clear();
      board.clear();
      app.setBoardId("myboard");
      board.loadByteArray( QByteArray(boardString), "intern_SPI" );
      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "canio",
                   "periphery": [
                      { "type": "UART", "name": "1", "relation": "placeholder", "attributes": {"overlap": "uartUart"}},
                      { "type": "GPIO", "name": "1"},
                      { "type": "GPIO", "name": "2"}
                    ]
               }
      )JSON" ), "intern" );

      pinmux.setBoardApp(board, app);
      status=0;
      try
      {
         pinmux.resolveApp( CPinmux::EMethod::linear );
      }
      catch(...)
      {
         status=1;
      }

      REQUIRE( status == 1 );

      //CWriterPlatform writerPlatform(pinmux);
   }

}


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