#include "SAB.h"

SAB::SAB() : _display(_boardConfig.getScreenWidth(), _boardConfig.getScreenHeight(), &Wire),
_screenManager(_display), _rtc(), 
_rtcManager(_rtc), _sdCardManager(_boardConfig.getSPI_SDCard_cs(), _boardConfig.getSPISpeed()), 
_connectivityManager(NULL), 
//_webServerManager(80, &_sdCardManager),
_webServer(80, &_sdCardManager),
_ftpServer(21, &_sdCardManager, "ESP8266", "12345678"),
_pcf(_boardConfig.getI2C_IOExpanderAddress(), Wire),
_ioManager(_pcf),
_taskSchedulerManager(_rtcManager),
_error(0)
{
  //We set the gpio up
  initGPIO();
  //Serial.begin(9600);
  Serial.begin(500000, SERIAL_8N1, SERIAL_TX_ONLY);
  delay(200);
  
  //We initialize the pins for the  I2C communication
  Wire.begin(_boardConfig.getI2C_sda(), _boardConfig.getI2C_scl());
  if(!_rtc.begin()) _error |= RTC_BEGIN_ERR;
  if(!_display.begin(SSD1306_SWITCHCAPVCC, _boardConfig.getI2C_screenAddress())){ _error |= DISP_BEGIN_ERR; }
  if(!_sdCardManager.mountSD()){ _error |= SDCARD_INIT_ERR; Serial.print("Failed to init SDCard : SPI_SPEED : "); Serial.print(8000000); Serial.print(" CS PIN : "); Serial.println(_boardConfig.getSPI_SDCard_cs());}
  _connectivityManager = new ConnectivityManager(_sdCardManager);
  if(!_pcf.begin()){_error |= IO_INIT_ERR;}
}

SAB::SAB(const BoardConfig boardConfig, const unsigned int webServerPort, const unsigned int ftpServerPort) : _boardConfig(boardConfig),
_display(_boardConfig.getScreenWidth(), _boardConfig.getScreenHeight(), &Wire),
_screenManager(_display), 
_rtc(), 
_rtcManager(_rtc), 
_sdCardManager(_boardConfig.getSPI_SDCard_cs(), _boardConfig.getSPISpeed()), 
_connectivityManager(NULL), 
//_webServerManager(webServerPort, &_sdCardManager),
_webServer(webServerPort, &_sdCardManager),
_ftpServer(ftpServerPort, &_sdCardManager, "ESP8266", "12345678"),
_pcf(_boardConfig.getI2C_IOExpanderAddress(), Wire), 
_ioManager(_pcf),
_taskSchedulerManager(_rtcManager),
_error(0)
{
  //We set the gpio up
  initGPIO();
  //Serial.begin(9600);
  Serial.begin(500000, SERIAL_8N1, SERIAL_TX_ONLY);
  delay(200);
  //We initialize the pins for the  I2C communication
  Wire.begin(_boardConfig.getI2C_sda(), _boardConfig.getI2C_scl());
  if(!_rtc.begin()) _error |= RTC_BEGIN_ERR;
  if(!_display.begin(SSD1306_SWITCHCAPVCC, _boardConfig.getI2C_screenAddress())) _error |= DISP_BEGIN_ERR;
  if(!_sdCardManager.mountSD()){ _error |= SDCARD_INIT_ERR; Serial.print("Failed to init SDCard : SPI_SPEED : "); Serial.print(8000000); Serial.print(" CS PIN : "); Serial.println(_boardConfig.getSPI_SDCard_cs());}
  _connectivityManager = new ConnectivityManager(_sdCardManager);
  if(!_pcf.begin()){_error |= IO_INIT_ERR;}
}

void SAB::initGPIO()
{
  pinMode(GPIO_0, INPUT);
}

ScreenManager& SAB::getScreenManager()
{
  return _screenManager;
}

RtcManager& SAB::getRtcManager()
{
  return _rtcManager;
}

SDCardManager& SAB::getSdCardManager()
{
  return _sdCardManager;
}

ConnectivityManager& SAB::getConnectivityManager()
{
  return *_connectivityManager;
}

/*WEBServerManager& SAB::getWebServerManager()
{
  return _webServerManager;
}*/

WEBServer<WEBClient>& SAB::getWebServer()
{
  return _webServer;
}

FTPServer<FTPClient>& SAB::getFtpServer()
{
  return _ftpServer;
}

IOManager& SAB::getIoManager()
{
  return _ioManager;
}

TaskSchedulerManager& SAB::getTaskSchedulerManager()
{
  return _taskSchedulerManager;
}

PowerManager& SAB::getPowerManager()
{
  return _powerManager;
}

BoardConfig SAB::getBoardConfig() const
{
  return _boardConfig;
}

const char *SAB::getSoftVersion() const
{
  return SOFT_VERSION;
}

unsigned char SAB::getError() const
{
  return _error;
}
