#ifndef CANDIS_SCREEN_WINE_HPP
#define CANDIS_SCREEN_WINE_HPP
//----------------------------------------------------------------------------
///
/// \brief   CANDis screen showing can messages
///
///          Static table
///
/// \date   20220903
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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

//#include <platform_generated.h>
//#include <candis.h>
#include <ocelli/screen.hpp>
#include <biwak/gdisplay.h>


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


class CScreenWine: public OScreen
{
   private:
      #if 1 // defined(STM32)
      CPlatform *m_pPlatform;
      #endif
      char m_text[20];

      struct {
         int id;
         int value;
         lrtimer_t measureTime;
         bool updated=false;
      }tempValues[ 8 ];

      static constexpr int BORDER = 4;
      static constexpr int COUNT = sizeof(tempValues) / sizeof(tempValues[0]);

   public:
      #if 1 // defined(STM32)
      CScreenWine( CPlatform *platform )
         :m_pPlatform(platform)
      {
         for(int i1=0; i1 < COUNT; i1++)
         {
            tempValues[i1].id=-1;
            tempValues[i1].value=0;
            tempValues[i1].updated=false;
         }
      }
      #else
      CScreenWine(  )
      {}
      #endif
      int yText(int index)
      {
         return ( BORDER + ( ( 11 + 2 ) * index ) + 3 );
      }
      virtual void draw() override
      {
         OScreen::draw();
         const OIconRef& ref=iconRef_wineSplash_flash;
         if( ref.find() )
         {
            // The buttons of navigation screen are actually 60 pixels high
            // The splash screen is 240x256
            ref.draw(0,0);
         }
         else
         {
            lWarning("Could not find splash screen");
            return;
         }

      }
      void eventLoop()
      {
         for(int i1=0; i1 < COUNT; i1++)
         {
            if ( tempValues[i1].updated )
            {
               showValue( i1, true, true);
               tempValues[i1].updated = false;
            }
            else if (tempValues[i1].id>=0)
            {
               if( lrElapsedSeconds(tempValues[i1].measureTime) > 20 )
               {
                  showValue( i1, true, true, true);
                  tempValues[i1].id=-1;
               }
            }
         }
      }

      void addTemperature(const int id, int temperature)
      {
         int nextFree=-1, index=-1;
         for(int i1=0; i1 < COUNT; i1++)
         {
            if( id == tempValues[i1].id )
            {
               index=i1;
               break;
            }
            else if( (tempValues[i1].id<0) && (nextFree==-1) )
            {
               nextFree=i1;
            }
         }
         if(index<0)
         {
            index=nextFree;
         }
         if(index>=0)
         {
            tempValues[index].id=id;
            tempValues[index].value=temperature;
            tempValues[index].measureTime=lrNow();
            tempValues[index].updated=true;
         }
         return;
      }

      void showValue(int index, bool dry, bool valid = true, bool gone=false)
      {
         int temp=tempValues[index].value;
         const TRgb *color=&darkGreen;

         if(gone)
         {
            strcpy(m_text, "      " );
         }
         else
         {
            int temp1=temp / 100;
            snprintf(m_text, 20, "%2d.%02d" "\xF8" "C", temp1, temp - (temp1*100) );
            if (temp < 1900 )
               color=&blue;
            if (temp > 2050 )
               color=&red;
         }
         OWidget::display().setFg( *color );
         OWidget::display().setFont( getFont8() );
         OWidget::display().setDrawBackground( true );
         OWidget::display().drawText( 120-(8), yText( index ), m_text );

         snprintf(m_text, 20, "0x%03X", tempValues[index].id);
         OWidget::display().setFg( white );
         OWidget::display().drawText(BORDER + 2, yText(index), m_text);
      }
};


//---fin----------------------------------------------------------------------
#endif // ? ! CANDIS_SCREEN_WINE_HPP
