#include "views.h"

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

  switch(p->powerInfo.powerType)
  {
    case PowerManager::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 : %s:%s:%s\nDate : %s/%s/%d\nFree RAM : %d\nSdCard : %s\nPower : %s\nViews : %u\n"
  , dateTimeFormater(zero_prepended[0],p->dateTime.hour(),'0')
  , dateTimeFormater(zero_prepended[1],p->dateTime.minute(),'0')
  , dateTimeFormater(zero_prepended[2],p->dateTime.second(),'0')
  , dateTimeFormater(zero_prepended[3],p->dateTime.day(),'0')
  , dateTimeFormater(zero_prepended[4],p->dateTime.month(),'0')
  , p->dateTime.year(),ESP.getFreeHeap(),sdCard,power_str, p->nbViews);
  sprintf(pageTitle, "GLOBAL INFO v %s",p->version);
  display.println(time_str);
  display.setCursor(0,56);
  display.println(pageTitle);
  
  return true;
}

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

  ViewAPPacket *p = (ViewAPPacket *) pData;

  if(p->enabled)
    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());
  else
    sprintf(conn_str,"AP connectivity\nis not enabled\nCheck the cfg file\n");
  
  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;

  if(p->enabled)
    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);
  else
    sprintf(conn_str,"STA connectivity\nis not enabled\nCheck the cfg file\n");
  
  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)
{
  ViewIoInfoPacket *p = (ViewIoInfoPacket *) pData;
  char buff[30] = "";

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

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

  char error_str[300];
  
  display.print("Rst reason : ");display.println(ESP.getResetReason());
  display.print("RTC : ");display.println((p->getError() & SAB::RTC_BEGIN_ERR) != 0 ? "ERROR":"OK");
  display.print("DISPLAY : ");display.println((p->getError() & SAB::DISP_BEGIN_ERR) != 0 ? "ERROR":"OK");
  display.print("SDCard : ");display.println((p->getError() & SAB::SDCARD_INIT_ERR) != 0 ? "ERROR":"OK");
  display.print("IO : ");display.println((p->getError() & SAB::IO_INIT_ERR) != 0 ? "ERROR":"OK");
  
  display.setCursor(0,56);
  display.print(F("SYS ERROR CODE : "));display.println(p->getError());
  return true;
}
