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


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


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

#include <ocelli/screen.hpp>
#include <ocelli/button.hpp>
#include <ocelli/digiclock.h>
#include <biwak/gdisplay.h>
#include <biwak/rtc.hpp>
#include <lepto/time.h>          // MSEC_PER_SEC
#include <theme.hpp>
#include <media.hpp>

#define TEMP_EXPIRE_SECONDS       ( 40 )


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


void utc2local(struct tm* t);


class OScreenStandby: public OScreen
{
   private:
      CRtc *m_pRtc;
      ODigiClock m_digiClock;
      lrtimer_t m_lrtimerUpdate=0;

      lrtimer_t m_timerTempSet1=0;
      int m_temperature1=ODigiClock::INVALID_TEMPERATURE;

      lrtimer_t m_timerTempSet2=0;
      int m_temperature2=ODigiClock::INVALID_TEMPERATURE;
      
      lrtimer_t m_timerTempSet3=0;
      int m_temperature3=ODigiClock::INVALID_TEMPERATURE;
      
      const OIconRef *m_icon=&iconRef_steak32_flash;
      bool m_steakFound=false;
      
   public:
      CSignal<void> m_signalLeaveStandby;
      OScreenStandby( CRtc *rtc )
         :m_pRtc( rtc )
         ,m_digiClock(0,0,239,319)
      {
         addWidget( &m_digiClock );
         eventSignal.connect(this, &OScreenStandby::slotPressed);
         m_digiClock.eventSignal.connect(this, &OScreenStandby::slotPressed);

         if(m_icon->find())
         {
            //m_digiClock.setIcon(m_icon);
            m_steakFound = true;
         }

      }
      virtual void draw() override
      {
         // Cant guarantee that background color was not changed. there is
         // nothing like activateScreen() yet.
         OWidget::display().setBg( black );
         OScreen::draw();
         drawTime( true );
         drawDate( true );
         drawTemp( true );
      }
      void drawDate( bool force = false )
      {
         int year, month, day;
         int loop=0;
         static int pDay=-1;
         // STM32 issue; delayed.
         do{
            m_pRtc->getDate(year, month, day);
            loop++;
         }while( (year==2000) && (loop < 100) );

         if( ( day != pDay ) || force )
         {
            m_digiClock.drawDate( day, month, year);
            pDay=day;
         }
      }
      void drawTime( bool force = false )
      {
         //int hour, minute, second;
         struct tm t;
         static int phour, pminute, psecond=-1;
         m_pRtc->getTime( t.tm_hour, t.tm_min, t.tm_sec );
         utc2local( &t );
         {
            if( ( phour != t.tm_hour) || ( pminute != t.tm_min )
                     || ( psecond != t.tm_sec ) || force )
            {
               m_digiClock.drawTime( t.tm_hour, t.tm_min, t.tm_sec );
               phour=t.tm_hour;
               pminute=t.tm_min;
               psecond=t.tm_sec;
            }
         }
      }
      void drawTemp( bool force = false )
      {
         static int pTemp1=ODigiClock::INVALID_TEMPERATURE;
         static int pTemp2=ODigiClock::INVALID_TEMPERATURE;
         static int pTemp3=ODigiClock::INVALID_TEMPERATURE;
         
         if( ( m_temperature1 != pTemp1 ) || force )
         {
            m_digiClock.drawTemp( 0, m_temperature1 );
            pTemp1 = m_temperature1;
         }
         if( ( m_temperature2 != pTemp2 ) || force )
         {
            m_digiClock.drawTemp( 1, m_temperature2 );
            pTemp2 = m_temperature2;
         }
         if( ( m_temperature3 != pTemp3 ) || force )
         {
            m_digiClock.drawTemp( 2, m_temperature3 );
            pTemp3 = m_temperature3;
         }
      }
      void eventLoop()
      {
         if (lrElapsedMSeconds(&m_timerTempSet1, MSEC_PER_SEC * TEMP_EXPIRE_SECONDS ))
         {
            m_temperature1 = ODigiClock::INVALID_TEMPERATURE;
         }
         if (lrElapsedMSeconds(&m_timerTempSet2, MSEC_PER_SEC * TEMP_EXPIRE_SECONDS ))
         {
            m_temperature2 = ODigiClock::INVALID_TEMPERATURE;
         }
         if (lrElapsedMSeconds(&m_timerTempSet3, MSEC_PER_SEC * TEMP_EXPIRE_SECONDS ))
         {
            m_temperature3 = ODigiClock::INVALID_TEMPERATURE;
         }
         if (lrElapsedMSeconds(&m_lrtimerUpdate, MSEC_PER_SEC/8))
         {
            drawTime();
            drawDate();
            drawTemp();
            if( m_temperature3 != ODigiClock::INVALID_TEMPERATURE )
            {
               m_digiClock.drawPicto( m_icon );
               //OApp::instance()->touchEvent();
            }
            else
            {
               m_digiClock.drawPicto( nullptr );
            }
         }
      }
      void setTemperature1( int temperature )
      {
         m_timerTempSet1=lrNow();
         m_temperature1=temperature;
      }
      void setTemperature2( int temperature )
      {
         m_timerTempSet2=lrNow();
         m_temperature2=temperature;
      }
      void setTemperature3( int temperature )
      {
         m_timerTempSet3=lrNow();
         m_temperature3=temperature;
      }
      void slotPressed(const Ocelli::SEvent& event)
      {
         OApp::instance()->setPreviousScreen();
      }
      virtual void update(const Ocelli::SEvent &event) override
      {
         if( event.type == Ocelli::EEventType::clicked )
         {
            m_signalLeaveStandby.emitSignal();
         }
      }
};


//---fin----------------------------------------------------------------------
#endif // ? ! LIBOCELLI_SCREEN_STANDBY_HPP
