#ifndef LIBOCELLI_SCREEN_ITEM_LIST_HPP
#define LIBOCELLI_SCREEN_ITEM_LIST_HPP
//----------------------------------------------------------------------------
///
/// \brief   Navigation screen
///
///
///
/// \date   20220905
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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

#include <ocelli/screen.hpp>
#include <ocelli/button.hpp>
#include <biwak/gdisplay.h>
#include <minutnik/mn_core.hpp>
#include <mn_core.h>
#include <lepto/time.h>          // MSEC_PER_SEC

#define RGBMAX    0xFF

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

class CScreenItemList: public OScreen
{
   private:

      const CMinutnikCore &m_minutnik;
      int border=4;
      int lineDist=19;
      int x2=140;
      int x1=0;
      int m_lastTime[13]{-1};
      lrtimer_t m_updateTimer;
      const TRgb m_backgroundColor
               = {.red=RGBMAX / 6,.green=0 , .blue=RGBMAX / 8};

   public:
      CSignal<void, const CMinutnikCore::SItem &> m_signalAlarm;
      CScreenItemList( const CMinutnikCore &minutnikCore )
         : m_minutnik(minutnikCore)
      {
         m_updateTimer=lrNow();
      }
      virtual void draw() override
      {
         OWidget::display().setFg( white );
         OWidget::display().setFont( getFont11() );
         OWidget::display().drawBar(0,0,239,319, m_backgroundColor, m_backgroundColor);
         OWidget::display().setBg( m_backgroundColor );
         OWidget::display().setDrawBackground(true);
         int row=0;

         for(const auto slot: m_minutnik.getSlotList() )
         {
            OWidget::display().drawText(border+x1,border+(row * lineDist), slot.item.description );
            row++;
         }

         OWidget::display().setFg( red );
         OScreen::draw();
         update();
      };
      void update()
      {
         OWidget::display().setFg( white );
         OWidget::display().setFont( getFont11() );
         int row=0;

         OWidget::display().setBg( m_backgroundColor );
         OWidget::display().setFont( getFont8());
         OWidget::display().setDrawBackground( true );

         for(const auto slot: m_minutnik.getSlotList() )
         {
            int hour, min, sec, mSec;
            char buf[10];

            int time=m_minutnik.slotTimeLeft(slot);
            if( time / 1000 == m_lastTime[row])
            {
               row++;
               continue;
            }

            if(time<=0)
            {
               OWidget::display().setFg( lightRed );
               //m_signalAlarm.emitSignal(slot.item);
               time=0;
            }
            else
            {
               OWidget::display().setFg( cyan );
            }
            mseconds2time(time, hour, min, sec, mSec);

            snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec);
            OWidget::display().drawText( border+x2,border+(row * lineDist)+2,
                                 buf );

            m_lastTime[row] = time / 1000;
            row++;
         }
      }
      void eventLoop()
      {
         if( lrElapsedMSeconds( &m_updateTimer, MSEC_PER_SEC/24 ) )
         {
            update();
         }
         //m_minutnik.eventLoop();
      }
};


//---fin----------------------------------------------------------------------
#endif // ? ! LIBOCELLI_SCREEN_ITEM_LIST_HPP
