#ifndef __display_H__
#define __display_H__

#include "fonts.h"
#include <Wire.h>
#include "SSD1306Wire.h" 

class display
{
private:
    
    const uint8_t MAX_CONTRAST = 255;    
    SSD1306Wire * _display;
    uint8_t _dispaddr;
    int _pin_sda, _pin_scl;
    bool _screen_flipped;
public:
    display(uint8_t dispaddr, int pin_sda, int pin_scl);
    ~display();
    void init();
    void header(String title);
    void header(String title, bool wificonected, bool localvalues, bool lastdatasent, bool timesynced);
    void data(unsigned int x, unsigned int y , float val, String valname);
    void data(unsigned int x, unsigned int y , uint64_t val, String valname);
    void data(unsigned int x, unsigned int y ,bool valid, String val, String valname, unsigned int digits, String valunit);
    void show();
    void clear();
    void flipScreenVert();
    void setBrigthness(uint8_t value);

};

display::display(uint8_t dispaddr, int pin_sda, int pin_scl)
{
    _pin_sda = pin_sda;
    _pin_scl = pin_scl;
    _dispaddr = dispaddr;
    _display = new SSD1306Wire(_dispaddr, _pin_sda, _pin_scl);
}

display::~display()
{
}

void display::init()
{
  //initialise OLED and display Welcome Message ...
  _display->init();
  _display->flipScreenVertically();
  _screen_flipped = true;
  _display->setTextAlignment(TEXT_ALIGN_CENTER);
  _display->setFont(Roboto_Condensed_Bold_16);
  _display->clear();
  _display->setContrast(MAX_CONTRAST);
  _display->drawString(64, 32, "Itsblue.de");
  _display->display();
}

void display::header(String title, bool wificonected, bool localvalues, bool lastdatasent, bool timesynced)
{
  _display->setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
  _display->setColor(WHITE);  
  _display->fillRect(0, 1, 128, 12);
  _display->setColor(BLACK);    
  _display->setFont(ArialMT_Plain_10);
  unsigned int y = 6;
  _display->drawString(64, y, title);
  
  if (wificonected)
  {
    _display->drawString(110,y, "Y");
  }
  else
  {
    _display->drawString(110, y, "!");
  }
  if (localvalues) {
    if (lastdatasent)
    {
      _display->drawString(120, y, " >>");
    }
    else
    {
      _display->drawString(120, y, " x ");
    }
  } else {
    _display->drawString(120, y, " R ");
  }
  if (timesynced)
  {
    _display->drawString(10, y, "(<)");
  }
  else
  {
    _display->drawString(10, y, "( )");
  }
  
}

void display::header(String title)
{
    header(title, false, true, false, false);
}

void display::data(unsigned int x, unsigned int y ,bool valid, String val, String valname, unsigned int digits, String valunit)
{
  _display->setColor(WHITE);
  _display->setTextAlignment(TEXT_ALIGN_CENTER);
  _display->setFont(ArialMT_Plain_10);
  String namestring = valname + ":";
  _display->drawString(x,y, namestring);
  _display->setFont(ArialMT_Plain_16);
  String valstring = "";
  if (valid == true) {
    valstring = val;
  } 
  else 
  {
    for(unsigned int digit=0; digit < digits; digit++){
      valstring+="-";
    }      
  }
  _display->drawString(x,y+10, valstring); 
  unsigned int valwidth = _display->getStringWidth(valstring)/2; 
  _display->setFont(ArialMT_Plain_10);
  _display->setTextAlignment(TEXT_ALIGN_LEFT);
  _display->drawString(x+valwidth,y+10, valunit);  
  _display->setTextAlignment(TEXT_ALIGN_CENTER);  
}

void display::data(unsigned int x, unsigned int y , uint64_t val, String valname)
{
    data(x, y,true, String((unsigned long)val,10), valname, 0, "");
}

void display::data(unsigned int x, unsigned int y , float val, String valname)
{
    data(x, y,true, String(val,1), valname, 0, "");
}

void display::show()
{
    _display->display();
}

void display::clear()
{
    _display->clear();
}

void display::flipScreenVert()
{
  if(_screen_flipped == true)
  { 
    _screen_flipped = false;
    _display->invertDisplay();
  }
  else
  {
    _screen_flipped = true;
    _display->normalDisplay();
    //_display->flipScreenVertically();
  }
}

void display::setBrigthness(uint8_t value)
{
  _display->setBrightness(value);
}

#endif