#include "views.h"

boolean view_1(Adafruit_SSD1306 &display, void *pData)
{
  char time_str[300] = "", power_str[15] = "", sdCard[10] = "";
  View1Packet *p = (View1Packet *) pData;

  switch(p->powerInfo.powerType)
  {
    case USB:
	  sprintf(power_str,"USB");
      break;
    default:
      sprintf(power_str,"BATT(%u%%)", p->powerInfo.level);
  }

  if(p->sdCardSize != 0.0)
    sprintf(sdCard,"%.2fGB",p->sdCardSize);
  else
    sprintf(sdCard,"NO CARD");
    
  sprintf(time_str,"Time : %d:%d:%d\nDate : %d/%d/%d\nFree RAM : %d\nSdCard : %s\nPower : %s\nViews : %u\n", p->dateTime.hour(), p->dateTime.minute(), p->dateTime.second(), p->dateTime.day(), p->dateTime.month(), p->dateTime.year(),ESP.getFreeHeap(),sdCard,power_str, p->nbViews);
  display.println(time_str);
  display.setCursor(0,56);
  display.println(F("GLOBAL INFO"));
  
  return true;
}

boolean view_2(Adafruit_SSD1306 &display, void *pData)
{
  char conn_str[300];

  ViewAPPacket *p = (ViewAPPacket *) pData;
  
  sprintf(conn_str,"Ip addr : %u.%u.%u.%u\nMac addr : \n%s\nConns : %u\nSSID : %s\n" ,p->ipAddr[0], p->ipAddr[1], p->ipAddr[2], p->ipAddr[3], p->macAddr.c_str(), p->nbOfCon, p->ssid.c_str());
  display.println(conn_str);
  
  display.setCursor(0,56);
  display.println(F("AP INFO"));

  return true;
}

boolean view_3(Adafruit_SSD1306 &display, void *pData)
{
  char conn_str[300];

  ViewSTAPacket *p = (ViewSTAPacket *) pData;
  
  sprintf(conn_str,"Ip addr : %u.%u.%u.%u\nMac addr : \n%s\nSignal : %d dBm\n", p->ipAddr[0], p->ipAddr[1], p->ipAddr[2], p->ipAddr[3], p->macAddr.c_str(), p->sigStrength);
  display.println(conn_str);
  
  display.setCursor(0,56);
  display.println(F("STA INFO"));

  return true;
}

boolean memInfo(Adafruit_SSD1306 &display, void *pData)
{
  char dispBuffer[300];
  uint32_t freeMem;
  uint16_t biggestContigMemBlock;
  uint8_t frag;
  ESP.getHeapStats(&freeMem, &biggestContigMemBlock, &frag);
  sprintf(dispBuffer, "Free RAM : %u\nHeap frag : %u\nMax block : %u\nFree Flash : %u\nProg size : %u\nCPU Freq : %u MHz\n", freeMem, frag, biggestContigMemBlock, ESP.getFreeSketchSpace(), ESP.getSketchSize(), ESP.getCpuFreqMHz());
  display.println(dispBuffer);
  
  display.setCursor(0,56);
  display.println(F("SYS INFO"));
  return true;
}

boolean dummy(Adafruit_SSD1306 &display, void *pData)
{
  return false;
}

boolean ioInfo(Adafruit_SSD1306 &display, void *pData)
{
  boolean *p = (boolean *) pData;

  display.setCursor(0,2);
  display.print(F("0 1  2  3  4  5  6  7"));
  
  p[0] ? display.fillRect(0,15,9,30,WHITE) : display.drawRect(0,15,9,30,WHITE);//0
  p[1] ? display.fillRect(13,15,9,30,WHITE) : display.drawRect(13,15,9,30,WHITE);//1
  p[2] ? display.fillRect(30,15,9,30,WHITE) : display.drawRect(30,15,9,30,WHITE);//2
  p[3] ? display.fillRect(48,15,9,30,WHITE) : display.drawRect(48,15,9,30,WHITE);//3
  p[4] ? display.fillRect(66,15,9,30,WHITE) : display.drawRect(66,15,9,30,WHITE);//4
  p[5] ? display.fillRect(84,15,9,30,WHITE) : display.drawRect(84,15,9,30,WHITE);//5
  p[6] ? display.fillRect(102,15,9,30,WHITE) : display.drawRect(102,15,9,30,WHITE);//6
  p[7] ? display.fillRect(119,15,9,30,WHITE) : display.drawRect(119,15,9,30,WHITE);//7
  
  display.setCursor(0,56);
  display.println(F("I/O INFO"));
  return true;
}
