/*
 * Stairs
 * Copyright 2017 (c) Mark van Renswoude
 *
 * https://git.x2software.net/pub/Stairs
*/
#include "./system.h"
#include <ArduinoJson.h>
#include <FS.h>
#include "../debug.h"
#include "../global.h"
#include "../config.h"



void SystemSettings::toJson(Print &print)
{
  DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(8) + JSON_OBJECT_SIZE(5));

  JsonObject& root = jsonBuffer.createObject();

  root["ntpServer"] = ntpServer();
  root["ntpInterval"] = ntpInterval();

  root["lat"] = latitude();
  root["lng"] = longitude();

  JsonObject& pins = root.createNestedObject("pins");
  pins["ledAP"] = pinLEDAP();
  pins["ledSTA"] = pinLEDSTA();
  pins["apButton"] = pinAPButton();
  pins["pwmSDA"] = pinPWMDriverSDA();
  pins["pwmSCL"] = pinPWMDriverSCL();

  root["pwmAddress"] = pwmDriverAddress();
  root["pwmFrequency"] = pwmDriverFrequency();
  root["mapsAPIKey"] = mapsAPIKey();

  root.printTo(print);
}


bool SystemSettings::fromJson(char* data, bool* changed)
{
  if (changed != nullptr)
    *changed = false;

  DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(8) + JSON_OBJECT_SIZE(5) + 500);
  JsonObject& root = jsonBuffer.parseObject(data);

  if (!root.success())
    return false;

  const char* jsonNTPServer = root["ntpServer"];
  uint32_t jsonNTPInterval = root["ntpInterval"];

  double jsonLat = root["lat"];
  double jsonLng = root["lng"];

  JsonObject& pins = root["pins"];
  uint8_t jsonPinLEDAP = pins["ledAP"];
  uint8_t jsonPinLEDSTA = pins["ledSTA"];
  uint8_t jsonPinAPButton = pins["apButton"];
  uint8_t jsonPinPWMDriverSDA = pins["pwmSDA"];
  uint8_t jsonPinPWMDriverSCL = pins["pwmSCL"];

  uint8_t jsonPWMDriverAddress = root["pwmAddress"];
  uint16_t jsonPWMDriverFrequency = root["pwmFrequency"];
  const char* jsonMapAPIKey = root["mapsAPIKey"];

  if (jsonNTPServer == nullptr) jsonNTPServer = DefaultNTPServer;
  if (jsonNTPInterval == 0) jsonNTPInterval = 5;

  if (jsonPinLEDAP == 0) jsonPinLEDAP = pinLEDAP();
  if (jsonPinLEDSTA == 0) jsonPinLEDSTA = pinLEDSTA();
  if (jsonPinAPButton == 0) jsonPinAPButton = pinAPButton();
  if (jsonPinPWMDriverSDA == 0) jsonPinPWMDriverSDA = pinPWMDriverSDA();
  if (jsonPinPWMDriverSCL == 0) jsonPinPWMDriverSCL = pinPWMDriverSCL();

  if (jsonPWMDriverAddress == 0) jsonPWMDriverAddress = pwmDriverAddress();
  if (jsonPWMDriverFrequency == 0) jsonPWMDriverFrequency = pwmDriverFrequency();


  if ((jsonPinLEDAP != pinLEDAP()) ||
      (jsonPinLEDSTA != pinLEDSTA()) ||
      (jsonPinAPButton != pinAPButton()) ||
      (jsonPinPWMDriverSDA != pinPWMDriverSDA()) ||
      (jsonPinPWMDriverSCL != pinPWMDriverSCL()) ||
      (jsonPWMDriverAddress != pwmDriverAddress()) ||
      (jsonPWMDriverFrequency != pwmDriverFrequency()) ||
      (!sameStr(jsonMapAPIKey, mapsAPIKey())) ||
      (jsonLat != latitude()) ||
      (jsonLng != longitude()) ||
      (!sameStr(jsonNTPServer, ntpServer())) ||
      (jsonNTPInterval != ntpInterval()))
  {
    latitude(jsonLat);
    longitude(jsonLng);
    pinLEDAP(jsonPinLEDAP);
    pinLEDSTA(jsonPinLEDSTA);
    pinAPButton(jsonPinAPButton);
    pinPWMDriverSDA(jsonPinPWMDriverSDA);
    pinPWMDriverSCL(jsonPinPWMDriverSCL);
    pwmDriverAddress(jsonPWMDriverAddress);
    pwmDriverFrequency(jsonPWMDriverFrequency);
    mapsAPIKey(jsonMapAPIKey);
    ntpServer(jsonNTPServer);
    ntpInterval(jsonNTPInterval);

    if (changed != nullptr)
      *changed = true;
  }

  return true;
}