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


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


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


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

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

#include <ocelli/screen.hpp>
#include <ocelli/button.hpp>
#include <biwak/touch.h>
#include <biwak/linux/gdisplay_dummy.h>

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

#define TEST_ALL
#define STOP_ON_FAIL

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


TEST_CASE( "Screen", "[default]" ) {
   OScreen screen;

   SECTION( "findWidget" )
   {
      OWidget w1(10, 10, 20, 20);
      OWidget w2(100, 100, 120, 120);
      screen.addWidget( &w1 );
      screen.addWidget( &w2 );

      REQUIRE ( screen.findWidget(10,10) == &w1 );
      REQUIRE ( screen.findWidget(10, 9) == &screen );

      REQUIRE ( screen.findWidget(20,20) == &w1 );
      REQUIRE ( screen.findWidget(21,20) == &screen );

      REQUIRE ( screen.findWidget(110,110) == &w2 );
   }
}

// Click on an button an check if signals are routed correctly
TEST_CASE( "Click Button", "[default]" )
{
   //CGraphicDisplayQt gDisplay(nullptr, 160,160);
   //MainWindow window(nullptr);
   //CGraphicDisplayQt display( window.getLabel(), 240, 320);
   CGraphicDisplayDummy display( 240, 320);

   OWidget::setDisplay( display );
   OScreen screen;
   static Ocelli::EEventType currentEventType=Ocelli::EEventType::none;

   auto slotButtonPressed = [](const Ocelli::SEvent &event)
   {
      currentEventType=event.type;
      return;
   };

   printf("Starting...\n");
   OButton *buttonHome=new OButton(0,0,32,32,"0");
   screen.addWidget( buttonHome );
   Ocelli::SEvent event { 16, 16, Ocelli::EEventType::clicked };
   buttonHome->eventSignal.connect( slotButtonPressed );

   #if 0
   // Screen does not relay events to widgets by itself anymore
   SECTION( "Coordinates" )
   {
      REQUIRE ( currentEventType == Ocelli::EEventType::none );
      printf("clicking\n");
      screen.event( event );
      REQUIRE ( currentEventType == Ocelli::EEventType::clicked );
   }
   #endif
}

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