#ifndef _CANDIS_EVAL_H_
#define _CANDIS_EVAL_H_
//----------------------------------------------------------------------------
///
/// \brief   CANDis eval app
///
///          Bringup for CANDis board.
///
/// \date   20210829
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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

//#if defined( STM32 )
#if 1
    #include <platform_generated.hpp>
#else
    #include <platform_empty.h>
#endif


#include <hwinfo/hwinfo.h>

#include <biwak/flash_spi.hpp>
#include <biwak/touch_tsc2007.h>
#include <biwak/barrel_organ.h>
#include <biwak/flash_i2c.hpp>
#include <biwak/mcp7940.hpp>
#if defined( STM32 ) || 1
   #include <biwak/usb.hpp>
   #include <biwak/usb_device.h>
   #include <biwak/usb_class.h>
#endif

#if ! defined STM32
   #include <biwak/flash_file.hpp>
#endif

#include <ocelli/ocelli.hpp>

#include <candisConfig.hpp>
#include <common.hpp>

#include <minutnik/mn_core.hpp>


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


#define SELECT_USE_CAN        true
#define SELECT_USE_USB        false

class CMinutnikCore;
class OScreenNavi;
class OScreenStandby;
class CScreenCanMon;
class CScreenMinutnik;
class OScreenAlarm;
class CScreenItems;
class CScreenItemList;
class CScreenInfo;
class CScreenMainMenu;
class CScreenSettings;
class CScreenThermometer;
class CScreenChart;
class CScreenWine;
class CScreenPlant;

class CVagus;

class CMinutnik: public CPlatform
{
   private:
      CRetain *m_pRetainHwData;
      CRetain *m_pRetainConfig;

      SEepromHwData m_hwData{ };
      SEepromConfigVagusExpanded m_config;

      CMcp7940    m_externRtc;
      CBarrelOrganBeep  m_boBuzzer;
      CBarrelOrganFade  m_boBacklight;
      CRetain *m_pRetainCalibration;
      SCalibrationConfig m_calibration;

   public:
      CTouchTsc2007  m_touch;
      #if defined STM32
         CFlashSpi      m_flash;
      #else
         CFlashFile     m_flash;
      #endif

   private:
      OApp m_ocelliApp;

      OScreenNavi *m_pScreenNavi;
      OScreenStandby *m_pScreenStandby;
      OScreenAlarm *m_pScreenAlarm;
      CScreenItems *m_pScreenItems;
      CScreenItemList *m_pScreenItemList;
      CScreenCanMon *m_pScreenCanMon;
      CScreenMinutnik *m_pScreenMinutnik;
      CScreenMainMenu *m_pScreenMainMenu;
      CScreenInfo *m_pScreenInfo;
      CScreenThermometer *m_pScreenThermometer;
      CScreenSettings *m_pScreenSettings;
      CScreenChart *m_pScreenChart;
      CScreenWine *m_pScreenWine;
      CScreenPlant *m_pScreenPlant;

      OScreen* m_screenList[ (int)EScreen::end ]{0};
         //[minutnik]=&
         //&m_pScreenMinutnik,
      //};
      /*
         thermometer,
         light,
         settings,
         busstop,
         chart,
         wine,
         info,
         plant,
      };
      */

      bool m_forceBootLoader;
      bool m_forceTouchCalibration=false;
      #if defined STM32 || 1
         CUsbDevice *m_pUsbDevice;
         CUsbClass *m_pUsbHid;
      #endif

      //CPhonyBL *m_pPhonyBl=nullptr;
      lrtimer_t m_lrtimerRtcSync=0;
      lrtimer_t m_lrtimerSendVersion=0;

   public:
      CSignal<void, const SCanMessage *> signalCANMessage1;
      CSignal<void, const SCanMessage *> signalCANMessage2;

      CMinutnik();
      ~CMinutnik();
      void setupEepromConfigs();
      int storeConfig();
      int restoreConfig();
      int resetConfig();

      void introStart();
      void introEnd();
      void addScreen( OScreen* screen, EScreen escreen );
      void setupUi();
      void setupBootloaderUi();
      void prepareApplication();
      void startCAN();
      void startUSB();
      void loadSettings();
      void eventLoop();
      int printHWInfo();

      void slotAlarm(const CMinutnikCore::SItem &slot);
      /*
      void slotAdd(const Ocelli::SEvent &event);
      void slotNum(const Ocelli::SEvent &event);
      */
      void slotBack(const Ocelli::SEvent &event);
      void slotInfo(const Ocelli::SEvent &event);
      void slotMenu(const Ocelli::SEvent &event);
      void slotStandby(const Ocelli::SEvent &event);
      void slotSetScreen(const enum EScreen screen);
      void slotQuitAlarm(const Ocelli::SEvent& event);

      int commandConfig(int argc, const char *argv[]);
      void setDefaultConfig();
      void usageConfig();
      void showBootloaderInfo();
      void slotReboot(const Ocelli::SEvent&);
      void leaveStandby();

      void slotSettingsChanged();
      bool isBootloaderActive();
      bool isAlarmActive();
      
      int eraseFlash(int argc, const char *argv[]);
      //virtual bool digestWriteObject( const Tdt::CMessage &msg ) override final;
      CTouchTsc2007* getTouch()
      {
         return( &m_touch );
      }
      CGraphicDisplay* getDisplay()
      {
         return( &OWidget::display() );
         //m_gdisplayIli93411 );
      }
      void loadCalibrations();
      void performTouchCalibration();
      int forceTouchCalibration(int argc, const char *argv[]);
};


//---fin----------------------------------------------------------------------
#endif // ? ! _CANDIS_EVAL_H_
