#ifndef __screen
#define __screen

#include <Adafruit_ST7789.h>
#include "include/config.h"


class ScreenManager;


enum class Button
{
  Up = 0,
  Menu = 1,
  Down = 2
};


class BaseScreen
{
  public:
    BaseScreen(ScreenManager* screenManager)
    {
      this->screenManager = screenManager;
    }

    virtual ~BaseScreen() {}

    virtual void onShow() = 0;
    virtual void onButton(Button button) = 0;
    virtual void onTick() = 0;

  protected:
    ScreenManager* getScreenManager() { return this->screenManager; }
    Adafruit_GFX* getDisplay();

  private:
    ScreenManager* screenManager;
};



class ScreenManager
{
  public:
    ScreenManager(Adafruit_ST7789* display)
    {
      this->display = display;
    }

    void init();

    inline void button(Button button) { this->getCurrentScreen()->onButton(button); }
    inline void tick() { this->getCurrentScreen()->onTick(); }

    inline BaseScreen* getCurrentScreen() { return this->currentScreen; }
    inline Adafruit_GFX* getDisplay() { return this->display; }


    template<class T> void show()
    {
      if (this->currentScreen != nullptr)
      {
        //currentScreen->onHide();
        delete this->currentScreen;
      }

      this->currentScreen = new T(this);
      this->currentScreen->onShow();
    }


    void displayOff();
    void displayOn();

  private:
    Adafruit_ST7789* display;

    BaseScreen* currentScreen = nullptr;
};

#endif
