//----------------------------------------------------------------------------
///
/// \file   clock.hpp
///
/// \brief  class for graphical clock widget
///
/// \date   20220619
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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


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

#include <math.h>

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

#include <ocelli/digiclock.h>
#include <ocelli/icon.hpp>
#include <ocelli/icon_catalog.h>
#include <lepto/log.h>
#include <stdio.h>                  // snprintf
//#include <theme.hpp>


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


char ODigiClock::m_text[20];

void ODigiClock::draw() /* virtual override */
{
   display().setDrawBackground(true);
   display().clear( black );
   return;
}

void ODigiClock::drawTime(int hour, int minute, int second)
{
   snprintf(m_text, 20, "%02d:%02d:%02d", hour, minute, second);
   display().setFg( green );
   display().setFont(getFont32());
   display().setDrawBackground(true);
   display().drawText(4,100 - m_yShift, m_text);
   return;
}

void ODigiClock::drawDate(int day, int month, int year)
{
   snprintf(m_text, 20, "%02d.%02d.%02d", day, month, year-2000);
   display().setFg(darkGreen);
   display().setFont(getFont24());
   display().setDrawBackground(true);
   display().drawText(16+8+4,132+8+4 - m_yShift, m_text);
   return;
}

void ODigiClock::drawTemp(int index, int temp)
{
   int lowThreshold=1900;     // Show blue font
   int highThreshold=2050;    // Show red font
   static int prevWidth[3]={-1, -1, -1};
   if(index == 2)
   {
      // Food sensor
      lowThreshold=4000;     // Show blue font
      highThreshold=6000;    // Show red font
   }
   int temp1=temp/100;
   if( temp == ODigiClock::INVALID_TEMPERATURE )
   {
      snprintf(m_text, 20, "--.--" "\xF8" "C" );
   }
   else
   {
      snprintf(m_text, 20, "%2d.%02d" "\xF8" "C",
               temp1%100, abs( temp - (temp1*100) )%100 );
   }
   const TRgb *color=&darkGreen;
   if( temp == ODigiClock::INVALID_TEMPERATURE )
   {
      color=&darkDarkGrey;
      if(index==2)
      {
         color=&black;
      }
   }
   else if (temp < lowThreshold )
   {
      color=&blue;
   }
   else if (temp > highThreshold )
   {
      color=&red;
   }

   display().setFg( *color );
   display().setFont(getFont24());
   display().setDrawBackground(true);
   int width=display().textWidth(m_text);
   //int x=16+8+4+4;
   int x=(240-width)/2;
   int y=132+8+4 +24+8+4 - m_yShift
           + (index * (24+8+4));
   if( width != prevWidth[index] )
   {
      display().drawBar(8, y , 240-8, y+24, black);
      prevWidth[index] = width;
   }
   display().drawText(x, y , m_text);
}

void ODigiClock::drawPicto( const OIconRef *m_ref )
{
   if(m_ref)
   {
      m_ref->draw( (240/2)-16,8);
   }
   else
   {
      OWidget::display().drawBar(   (240/2)-16,8,
                                 (240/2)-16+32,8+32, black );
   }
   
   return;
}

void ODigiClock::update( const Ocelli::SEvent &event )
{
   (void)event;
};


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