#if ! defined ( LIBOCELLI_SCREEN )
#define LIBOCELLI_SCREEN
//----------------------------------------------------------------------------
///
/// \file   screen.hpp
///
/// \brief  class for graphical screen
///
/// \date   20220619
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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


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

#include <lepto/list.h>
#include <lepto/log.h>

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

#include <ocelli/widget.hpp>


//---Declaration---------------------------------------------------------------


class OScreen: public OWidget
{

private:

   CList<OWidget *> m_list;
   OWidget *m_pressedWidget = nullptr;
   bool m_isSelectable = true;

public:
   OScreen( )
      :OWidget(OPoint(0,0), OPoint(240, 320, ECoordinateType::sizes) )
      ,m_list(10)
   {
   }
   void addWidget(OWidget *widget)
   {
      m_list << widget;
   }
   OWidget *findWidget(int x, int y)
   {
      OWidget *foundWidget=this;

      /* Only one widget can be found. Its assumed that the last added widget
       * that matches a coordinate wins the race. Widgets are "stacked up"
       */
      for(auto widget: m_list)
      {
         if(widget->lap(x , y))
         {
            foundWidget=widget;
         }
      }
      return( foundWidget );
   }
   virtual void activate()
   {
   }
   virtual void eventLoop()
   {
   }
   virtual void draw() override
   {
      for(auto widget: m_list)
      {
         widget->draw();
      }
   }
#if 1
   #if 0
   void event(CTouch::SEvent &event)
   {
      bool found=false;
      for(auto widget: m_list)
      {
         if(widget->lap(event.x, event.y))
         {
            //widget->event(event);
            referEvent(widget, event);
            found=true;
            break;
         }
      }
      if(!found)
      {
         referEvent(nullptr, event);
      }

      update(event);
      return;
   }
   #endif
#else
   void event(CTouch::SEvent &event)
   {
      OWidget *widget;
      if( ( widget = findWidget(event.x, event.y) ) )
      {
         widget->event(event);
      }
   }
#endif
   virtual void update(const Ocelli::SEvent &event) override
   {
   };
   bool containsWidget( OWidget *searchWidget ) const
   {
      for(auto widget: m_list)
      {
         if( widget == searchWidget )
         {
            return( true );
         }
      }
      return( false );
   }
   bool isSelectable() const
   {
      return( m_isSelectable );
   }
   void setSelectable( bool selectable )
   {
      m_isSelectable = selectable;
   }
};


//---fin-----------------------------------------------------------------------
#endif // ! ? LIBOCELLI_SCREEN
