#ifndef LIBOCELLI_SCREEN_BOOTLOADER_HPP
#define LIBOCELLI_SCREEN_BOOTLOADER_HPP
//----------------------------------------------------------------------------
///
/// \brief   Bootloader screen
///
/// \date   20221224
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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

#include <ctype.h>     // strupr

#include <ocelli/screen.hpp>
#include <ocelli/button.hpp>
#include <ocelli/progress.hpp>
#include <biwak/gdisplay.h>
#include <phonybl.h>


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

class CScreenBootloader: public OScreen
{
   private:
      OProgress *m_pProgress;
      OButton *m_pButtonReboot;
      CPhonyBL *m_pPhonyBl;
      int m_flashSize;

   public:

      CScreenBootloader( CPhonyBL *bootLoader, int flashSize )
         :m_pPhonyBl( bootLoader )
         ,m_flashSize( flashSize )
      {
         addWidget ( m_pButtonReboot = new OButton(
                  OPoint(120,320-32, center), OPoint( 240,64, sizes), "REBOOT") );
         addWidget ( m_pProgress = new OProgress(
                  OPoint(120,320-96, center), OPoint( 200,32, sizes)) );
         m_pButtonReboot->eventSignal.connect(this, &CScreenBootloader::slotReboot);
         m_pPhonyBl->writeSignal.connect(this, &CScreenBootloader::slotWrite);
      }

      void slotReboot(const Ocelli::SEvent& event)
      {
         if( event.type == Ocelli::EEventType::clicked )
         {
            #if defined(STM32)
               NVIC_SystemReset();
            #endif
         }
      }

      void slotWrite(int address)
      {
         int progress = ( ( 100 * address ) / m_flashSize );
         m_pProgress->setProgress( progress );
      }

      virtual void draw() override
      {
         OWidget::display().setFg( green );
         OWidget::display().setFont( getFont11() );
         OWidget::display().clear(black);
         OWidget::display().setBg( black );
         OWidget::display().setDrawBackground(true);

         static const char *text[]{
            "MEDIA-LIBRARY ",
            "DOES NOT MATCH ",
            "FIRMWARE REVISION!",
            "",
            "PLEASE FLASH",
            "MEDIA-LIBRARY",
            "VIA USB",
            "",
            "BOOTLOADER RUNNING",
            "",
            "",
            "MEDIA FLASH LEVEL:"
         };

         //m_gdisplayIli93411.setFont( getFont8());
         for(int i1=0; i1<arraySize(text); i1++)
         {
            OWidget::display().drawText(120, 16 + (i1*16), text[i1]
                              , eTextFlagsHCenter | eTextFlagsVCenter);
         }


         OScreen::draw();
      }
      OProgress &progress()
      {
         return(*m_pProgress);
      }
      void eventLoop()
      {
      }
};


//---fin----------------------------------------------------------------------
#endif // ? ! LIBOCELLI_SCREEN_INFO_HPP
