#ifndef CANDIS_SCREEN_MINUTNIK_HPP
#define CANDIS_SCREEN_MINUTNIK_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>
#include <ocelli/clock.hpp>
#include <ocelli/button.hpp>
#include <ocelli/ocelli.hpp>        // OApp
#include <minutnik/mn_core.hpp>
#include <biwak/barrel_organ.h>
#include "git_version.h"
#include <list>
#include <common.hpp>         // EScreen
#include <media.hpp>


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


#define     m_pButtonAdd      (&m_buttonAdd)
#define     m_pButtonNum      (&m_buttonNum)

class CScreenMinutnik: public OScreen
                     , public CMinutnikCore
{
   private:
      OClock *m_pClock;
#if 0
      OButton *m_pButtonAdd;
      OButton *m_pButtonNum;
#else
      OButton m_buttonAdd;
      OButton m_buttonNum;

#endif

   public:
      CBarrelOrganBeep& m_boBuzzer;
      CSignal<void, const SItem &> m_signalAlarm;
      CSignal<void, const EScreen> signalSetScreen;

      CScreenMinutnik( /*CCanDis &candis*/ CBarrelOrganBeep& buzzer )
         :m_buttonAdd( 240-60, 320-60-60,
                  239, 320-60, "+", OButton::EButtonStyle::flat)
         ,m_buttonNum( 0, 320-60-60,
                     59, 320-60, "2", OButton::EButtonStyle::flat)
         ,m_boBuzzer( buzzer )
      {
         int width=240-(8*2);
         addWidget(  m_pClock = new OClock( 8, 0, width, width, "" /*"MINUTNIK"*/ ) );
         #if 0
         addWidget(  m_pButtonAdd = new OButton( 240-60, 320-60-60,
                  239, 320-60, "+", OButton::EButtonStyle::flat) );
         addWidget(  m_pButtonNum = new OButton( 0, 320-60-60,
                  59, 320-60, "2", OButton::EButtonStyle::flat) );
         #else
            addWidget( &m_buttonAdd );
            addWidget( &m_buttonNum );
         #endif
         m_pButtonAdd->eventSignal.connect(this, &CScreenMinutnik::slotAdd);
         m_pButtonNum->eventSignal.connect(this, &CScreenMinutnik::slotNum);
      }
      void updateCounter()
      {
         static char buf[5];
         snprintf(buf, sizeof(buf), "%d", slotCount());
         m_pButtonNum->setText(buf);
      }
      virtual void draw() override
      {
         updateCounter();
         m_pClock->setState( getStateText() );
         m_pClock->setTopic( slotDescription() );
         const OIconRef *icon=getIcon();
         if( icon == nullptr )
         {
            icon=&iconRef_bretzel48_flash;
         }
         m_pClock->setIcon( icon );
         OScreen::draw();

         // Version information kind of misslocated here
         #if 0
         OWidget::display().setFg( white );
         OWidget::display().setFont( getFont6() );
         OWidget::display().drawText( 120 , 320-60-8,
                           GIT_SHORT, eTextFlagsHCenter | eTextFlagsVCenter );
         #endif
      }
      void updateFields()
      {
         m_pClock->setState( getStateText() );
         m_pClock->setTopic( slotDescription() );
         m_pClock->setIcon( getIcon() );
         m_pClock->drawState();
         m_pClock->drawTopic();
         m_pClock->drawIcon();
      }
      virtual void eventLoop() override
      {
         switch( getState() )
         {
            case Init:
            {
               // Calculate state
               resortIssues();
               updateFields();
               break;
            }
            case Idle:
            {
               break;
            }
            case Running:
            {
               int hour, min, sec, mSec, subSec;
               //m_candis.m_rtc1.getTime(hour, min, sec, mSec);
               int time=slotTimeLeft();
               mseconds2time(time, hour, min, sec, mSec);
               subSec=(mSec*59)/999;
               if(time>=0)
               {
                  m_pClock->update(hour, min, sec, subSec);
               }
               break;
            }
            case Alarm:
            {
               alarm();
            }
            default:
               break;
         }
      }
      void hiddenEventLoop()
      {
         if( isAnyTimerActive() )
         {
            slotTimeLeft();
            if( getState() == Alarm )
            {
               alarm();
            }
         }
      }
      void alarm()
      {
         // Fake an touch event so screen does not
         CTouch::SEvent event;
         event.type=CTouch::EEventType::none;
         OApp::instance()->event( event );
         setState(Init);
         m_signalAlarm.emitSignal( currentItem() );
         removeItem();
         resortIssues();
      }
      void showValue(int index, bool dry, bool valid = true, bool gone=false)
      {
         return;
      }

      const char *getStateText()
      {
         switch( getState() )
         {
            case Init : return ( "INIT" );
            case Idle : return ( "IDLE" );
            case Running : return ( "RUNNING" );
            case Alarm : return( "ALARM" );
         }
         return("UNKNOWN");
      }
      void slotAdd(const Ocelli::SEvent &event)
      {
         if( slotCount() >= 13 )
         {
            m_boBuzzer.setBeepMode( Barrel::Beep::singleDownUp );
         }
         else
         {
            signalSetScreen.emitSignal(EScreen::items);
         }
         return;
      }
      void slotNum(const Ocelli::SEvent &event)
      {
         if( slotCount() <= 0 )
         {
            m_boBuzzer.setBeepMode( Barrel::Beep::singleDownUp );
         }
         else
         {
            signalSetScreen.emitSignal(EScreen::itemList);
         }
         return;
      }
      void slotAddItem(const CMinutnikCore::SItem &item)
      {
         addItem(item);
         signalSetScreen.emitSignal(EScreen::minutnik);
         return;
      }
};


//---fin----------------------------------------------------------------------
#endif // ? ! CANDIS_SCREEN_MINUTNIK_HPP
