#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

/*
hunger*
sleapyness
Light
Attention icon !
Games
floof
Status (hours, love)
Pet
*/

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

uint8_t oldHunger = 0;
uint8_t newHunger = 120;
uint8_t maxHunger = 120;

bool puroDeath = false;



static const unsigned char PROGMEM idlePuro1[] = { 
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00110000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b01001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b10001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000010, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000010, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000100, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00100100, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b01010100, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b01001000, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b01000000, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b11000000, 0b10000000, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00011100, 0b00100000, 0b10000000, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000100, 0b00000000, 0b00000011, 0b11100000, 0b00010000, 0b10000000, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00001010, 0b00010000, 0b00001100, 0b00000000, 0b00001001, 0b00000000, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00001001, 0b00101000, 0b01110000, 0b00000000, 0b00001010, 0b00000000, 0b01111000, 0b00000000,
0b00000000, 0b00000000, 0b10010001, 0b01001001, 0b10000000, 0b00000000, 0b00101010, 0b00000111, 0b10001000, 0b00000000,
0b00000000, 0b00000001, 0b01010000, 0b10000110, 0b00000000, 0b00000001, 0b11010111, 0b00111000, 0b00111000, 0b00000000,
0b00000000, 0b11111110, 0b00100000, 0b00001000, 0b00000000, 0b00000010, 0b00000000, 0b11000000, 0b00001000, 0b00000000,
0b00000111, 0b00000000, 0b00000000, 0b00010001, 0b00000000, 0b00011100, 0b00000000, 0b01000000, 0b00001000, 0b00000000,
0b00011000, 0b00000000, 0b00000000, 0b00010000, 0b10000000, 0b00100000, 0b00000000, 0b00000000, 0b00111000, 0b00000000,
0b00001000, 0b00000000, 0b01111100, 0b00100000, 0b01111111, 0b11000000, 0b00000000, 0b00000000, 0b00001000, 0b00000000,
0b00000100, 0b00000000, 0b11001110, 0b00100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001000, 0b00000000,
0b00000010, 0b00000001, 0b10000110, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001000, 0b00000000,
0b00000001, 0b00000011, 0b10110111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001000, 0b00000000,
0b00000001, 0b11000111, 0b10000111, 0b00000000, 0b00000000, 0b00000000, 0b01000000, 0b00000000, 0b00111000, 0b00000000,
0b00000010, 0b00001111, 0b11001111, 0b00000000, 0b00000000, 0b00000000, 0b01000000, 0b00000011, 0b11101000, 0b00000000,
0b00000001, 0b00001111, 0b11111110, 0b00000000, 0b00000000, 0b00000000, 0b00100000, 0b00011100, 0b00001000, 0b00000000,
0b00000000, 0b10001111, 0b11111110, 0b00000000, 0b00000000, 0b00000000, 0b00111111, 0b11110000, 0b00001000, 0b00000000,
0b00000001, 0b00001111, 0b11111110, 0b00000000, 0b00000000, 0b00000000, 0b00100000, 0b00011100, 0b00001000, 0b00000000,
0b00000000, 0b10001111, 0b11001111, 0b00000000, 0b00000000, 0b00000000, 0b01000000, 0b00000011, 0b11101000, 0b00000000,
0b00000000, 0b11000111, 0b10000111, 0b00000000, 0b00000000, 0b00000000, 0b01000000, 0b00000000, 0b00111000, 0b00000000,
0b00000001, 0b00000011, 0b10110111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001000, 0b00000000,
0b00000010, 0b00000001, 0b10000110, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001000, 0b00000000,
0b00000100, 0b00000000, 0b11001110, 0b00100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001000, 0b00000000,
0b00001000, 0b00000000, 0b01111100, 0b00100000, 0b01111111, 0b11000000, 0b00000000, 0b00000000, 0b00001000, 0b00000000,
0b00011000, 0b00000000, 0b00000000, 0b00010000, 0b10000000, 0b00100000, 0b00000000, 0b00000000, 0b00111000, 0b00000000,
0b00000111, 0b00000000, 0b00000000, 0b00010001, 0b00000000, 0b00011100, 0b00000000, 0b01000000, 0b00001000, 0b00000000,
0b00000000, 0b11111110, 0b00100000, 0b00001000, 0b00000000, 0b00000010, 0b00000000, 0b11000000, 0b00001000, 0b00000000,
0b00000000, 0b00000001, 0b01010000, 0b10000110, 0b00000000, 0b00000001, 0b11010111, 0b00111000, 0b00111000, 0b00000000,
0b00000000, 0b00000000, 0b10010001, 0b01001001, 0b10000000, 0b00000000, 0b00101000, 0b00000111, 0b10001000, 0b00000000,
0b00000000, 0b00000000, 0b00001001, 0b00101000, 0b01110000, 0b00000000, 0b00001000, 0b00000000, 0b01111000, 0b00000000,
0b00000000, 0b00000000, 0b00001010, 0b00010000, 0b00001100, 0b00000000, 0b00001000, 0b00000000, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000100, 0b00000000, 0b00000011, 0b11100000, 0b00010000, 0b00000000, 0b00001000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00011100, 0b00100000, 0b00000000, 0b00010000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b11000000, 0b00000000, 0b01100000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00100000, 0b00000000, 0b10000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00010000, 0b00000111, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00010011, 0b00001000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001010, 0b10000100, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001010, 0b01100010, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000100, 0b00011100, 0b00000000, 0b00000000

};

void setup() {
  Serial.begin(9600);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


  display.clearDisplay();     //pulisce display
}

void loop() {  

  idlePuroAnim();

  if(oldHunger != newHunger) {
    display.clearDisplay();
    idlePuroAnim();
    displayHunger();
  }
  if(!newHunger) puroDeath = true;
    Serial.println(newHunger);
  if(puroDeath) for(;;){
    /*
    codice da eseguire in caso di morte
    */
  }
  delay(500);
  //newHunger--;
  
  /*
  quando dovrai fare l'animazione, per evitare di dover aspettare la fine dell'animazione fai una cosa come
  nel casco e ci metti un 'whilie per il blink', così nel frattempo che si aspetta per il cambio frame 
  comunque si controlla per la pressione del pulsante
  */
}

void idlePuroAnim(){
  
  display.drawBitmap(20, (display.height()-52) / 2, idlePuro1, 80, 52, 1);   //display idlePuro bitmap 1
  display.display();          //aggiorna buffer

}

void displayHunger(){
  uint8_t upperHunger = newHunger/2, lowerHunger = newHunger-upperHunger;
  if(newHunger == 1)  display.drawPixel(11, 60, SSD1306_WHITE);
  else{
    if(upperHunger > 0) display.drawLine(10, 60, 10, 61-upperHunger, SSD1306_WHITE);
    if(upperHunger > 0) display.drawLine(11, 60, 11, 61-lowerHunger, SSD1306_WHITE);
  }
  display.display();
  oldHunger = newHunger;
}