#include "tasks.h"

boolean task_blink(void *pData)
{
  SAB *p = (SAB *) pData;
  p->getIOManager().getPcf().togglePin(PCF8574::P2);

  return true;
}

boolean task_sys_info(void *pData)
{
  View1Packet *p = (View1Packet *) pData;

  uint32_t freeRAM;
  uint16_t biggestContigMemBlock;
  uint8_t HEAPfrag;
  ESP.getHeapStats(&freeRAM, &biggestContigMemBlock, &HEAPfrag);

  TimeSpan ts(p->sab->getUpTime());

  Serial.printf_P(PSTR("BATT SENSING...\nSoftware version : %s\nUp Time : %d d %d h %d m %d s\nSystem error : %u\nStation IP : %s, %d dBm\nAP IP : %s\nDevice MAC : %s\nWEB Server clients : %u\nMemory info:\n\tFree RAM : %u\n\tHeap frag : %u\n\tMax block %u\n"),
  p->sab->getSoftVersion(),
  ts.days(), ts.hours(), ts.minutes(), ts.seconds(),
  p->sab->getError(),
  p->sab->getConnectivityManager().localIP().toString().c_str(),
  p->sab->getConnectivityManager().RSSI(),
  p->sab->getConnectivityManager().softAPIP().toString().c_str(),
  p->sab->getConnectivityManager().macAddress().c_str(),
  p->sab->getWebServerManager().getWEBServer().getConnectedClientsCount(),
  freeRAM,
  HEAPfrag,
  biggestContigMemBlock);
  p->powerInfo = p->sab->getPowerManager().getPowerInfo();

  if(p->sab->getOTAManager().isEnabled())
  {
    OTAUpdater::UpdateInfo updateInfo = p->sab->getOTAManager().getOTAUpdater().fetchUpdateInfo(p->sab->getSoftVersion());
    if(updateInfo.version != NULL)
    {
      Serial.printf("OTA : %s -> version(%s)\n", updateInfo.HTTPUpdateInfoToString(), updateInfo.version);
    }
    else
    {
      Serial.printf("OTA : %s\n", updateInfo.HTTPUpdateInfoToString());
    }
  }
  else
  {
    Serial.println("OTA updates disabled");
  }
  
  return true;
}

boolean task_esp_reset_restart(void * pData)
{
  (void)pData;
  ESP.restart();
  
  return true;
}

boolean task_post_data_logger(void * pData)
{
  DataLogger *p = (DataLogger *) pData;
  //This routine is here to test the new HttpClient class
  //HttpClient httpClient("192.168.0.17", "/esp8266/dataLogger.php", 1234);

  if(p->counter == 0)
  {
    Dictionary<DictionaryHelper::StringEntity> getData;
    getData.add("key1", DictionaryHelper::StringEntity("value1"));
    getData.add("key2", DictionaryHelper::StringEntity(NULL));
    getData.add("key3", DictionaryHelper::StringEntity("value3"));
    getData.add("key4", NULL);
  
    Dictionary<DictionaryHelper::StringEntity> postData;
    postData.add("post1", DictionaryHelper::StringEntity("postvalue1"));
    postData.add("post2", DictionaryHelper::StringEntity(NULL));
    postData.add("post3", DictionaryHelper::StringEntity("postvalue3"));
    postData.add("post4", NULL);
    
  
    if(p->client.sendHttpQuery(HttpClient::HttpRequestMethod::POST, &getData, &postData) == 0)
    {
      Serial.println("Send successful");
      p->rdy = false;
    }
  }
  else if(p->client.isReplyAvailable() != HttpClient::HTTP_CODE::UNDEFINED_CODE && !p->rdy)
  {
    Serial.printf("Code : %d, counter : %u\n", p->client.isReplyAvailable(), p->counter);
    p->rdy = true;
  }

  if(p->rdy)
  {
    char buff[100];
    p->client.readHttpBody((uint8_t *)buff, 100);
    Serial.print(buff);
  }
  
  p->counter++;

  if(p->counter == 30)
    p->counter = 0;

  return true;
}

boolean testTask(void *pData)
{
  SAB *pSab = (SAB *)pData;

  return true;
}
