#ifndef SCREENMANAGER_H
#define SCREENMANAGER_H
#include "definition.h"
#include <Adafruit_SSD1306.h>

class ScreenManager
{
  friend class SAB;
  public:
    enum Error {OK, MALLOC_FAILED, VIEW_NOT_FOUND, VIEW_FUNC_UNDEFINED, VIEW_FAILED_TO_EXECUTE, CURRENT_VIEW_UNDEFINED};
  
    boolean addView(boolean (*viewLogicFunction)(Adafruit_SSD1306&, void*), void *pData, const unsigned char UID);
    boolean removeView(const unsigned char UID);
    boolean displayView(const int UID = -1);
    boolean displayNextView();
    void invertDisplayColor(const boolean inverted);
    void orientDisplay(const Orientation orientation);
    void dimDisplay(const boolean dimmed);
    void clearDisplay(const boolean bufferOnly = false);
    void sleep();
    void wakeUp();
    
    Error getError() const;
    const char* getErrorMessage() const;
    boolean isDisplayColorInverted() const;
    Orientation getDisplayOrientation() const;
    boolean isDisplayDimmed() const;
    int getCurrentViewUID() const;
    unsigned char getViewCount();

    
    void iterateThroughList();
    
  protected:
    ScreenManager(Adafruit_SSD1306 &display);
  private:
    void *createEmptyList();
    boolean addNewLinkAtTheEnd(ViewLinkedList *viewLinkedList, ViewLink viewLink);
    boolean removeLinkByUID(ViewLinkedList *viewLinkedList, const unsigned char UID);
    ViewLink* getLinkByUID(ViewLinkedList viewLinkedList, const unsigned char UID);
    boolean isListEmpty(ViewLinkedList viewLinkedList);
    static boolean displayError(Adafruit_SSD1306 &display, void *pData);
    
    Adafruit_SSD1306 &_displayRef;
    ViewLinkedList _viewLinkedList;
    Error _error;
    boolean _displayColorInverted;
    boolean _displayDimmed;
    ViewLink* _currentView;
    ViewLink _viewNotFound, _viewFuncUndefined, _currentViewUndefined, _viewFunctionFailedToExecute;

    //This structure contains the error message as well as the UID from the view that caused the error if available.
    typedef struct {const __FlashStringHelper* errorMessage; int viewUID;} ErrorInfo;
};

#endif //SCREENMANAGER_H
