


//const char* deviceName = DEVICE_NAME;
//const char* mqtt_server = MQTT_SERVER;
//const int mqtt_port = MQTT_PORT;
//const char* mqtt_topic_in = MQTT_TOPIC_IN;
//const char* mqtt_topic_out = MQTT_TOPIC_OUT;
//const char* mqtt_topic_out_hold[3] = { BUTTON1_HOLD_TOPIC_OUT, BUTTON2_HOLD_TOPIC_OUT, BUTTON3_HOLD_TOPIC_OUT };
//const char* mqtt_payload_out_hold[3] = { BUTTON1_HOLD_PAYLOAD_OUT, BUTTON2_HOLD_PAYLOAD_OUT, BUTTON3_HOLD_PAYLOAD_OUT };
//const int domoticzIdx[3] = {DOMOTICZ_IDX_1, DOMOTICZ_IDX_2, DOMOTICZ_IDX_3}; // initially set to 0, must be defined in config


bool loadConfig() {
  File configFile = SPIFFS.open("/config.json", "r");
  if (!configFile) {
    Serial.println("Failed to open config file");
    return false;
  }

  size_t size = configFile.size();
  if (size > 1024) {
    Serial.println("Config file size is too large");
    return false;
  }

  // Allocate a buffer to store contents of the file.
  std::unique_ptr<char[]> buf(new char[size]);

  // We don't use String here because ArduinoJson library requires the input
  // buffer to be mutable. If you don't use ArduinoJson, you may as well
  // use configFile.readString instead.
  configFile.readBytes(buf.get(), size);

  StaticJsonBuffer<1000> jsonBuffer;
  JsonObject& json = jsonBuffer.parseObject(buf.get());

  if (!json.success()) {
    Serial.println("Failed to parse config file");
    return false;
  }

  strcpy(deviceName, json["devName"]);
  strcpy(mqtt_server, json["mqttHost"]);
  mqtt_port = atoi(json["mqttPort"]);
  strcpy(mqtt_topic_in, json["inTop"]);
  strcpy(mqtt_topic_out, json["outTop"]);
  strcpy(mqtt_topic_out_hold_1, json["outTop_hold1"]);
  strcpy(mqtt_topic_out_hold_2, json["outTop_hold2"]);
  strcpy(mqtt_topic_out_hold_3, json["outTop_hold3"]);
  strcpy(mqtt_payload_out_hold_1, json["outPld_hold1"]);
  strcpy(mqtt_payload_out_hold_2, json["outPld_hold2"]);
  strcpy(mqtt_payload_out_hold_3, json["outPld_hold3"]);
  domoticzIdx[0] = atoi(json["domoIdx1"]);
  domoticzIdx[1] = atoi(json["domoIdx2"]);
  domoticzIdx[2] = atoi(json["domoIdx3"]);
  strcpy(domoticz_out_topic, json["domoOutTop"]);
  relais_impulse[0] = atoi(json["impuls1"]);
  relais_impulse[1] = atoi(json["impuls2"]);
  relais_impulse[2] = atoi(json["impuls3"]);


  //  deviceName = json["devName"];
  //  mqtt_server = json["mqttHost"];
  //  mqtt_port = atoi(json["mqttPort"]);
  //  mqtt_topic_in = json["inTop"];
  //  mqtt_topic_out = json["outTop"];
  //  mqtt_topic_out_hold[0] = json["outTop_hold1"];
  //  mqtt_topic_out_hold[1] = json["outTop_hold2"];
  //  mqtt_topic_out_hold[2] = json["outTop_hold3"];
  //  mqtt_payload_out_hold[0] = json["outPld_hold1"];
  //  mqtt_payload_out_hold[1] = json["outPld_hold2"];
  //  mqtt_payload_out_hold[2] = json["outPld_hold3"];
  //  domoticzIdx[0] = atoi(json["domoIdx1"]);
  //  domoticzIdx[1] = atoi(json["domoIdx2"]);
  //  domoticzIdx[2] = atoi(json["domoIdx3"]);
  //  domoticz_out_topic = json["domoOutTop"];
  //  relais_impulse[0] = json["impuls1"];
  //  relais_impulse[1] = json["impuls2"];
  //  relais_impulse[2] = json["impuls3"];

  Serial.println("Loaded config values:");
  getConfig();
  return true;
}

void getConfig() {
  Serial.print("devName: ");
  Serial.println(deviceName);
  Serial.print("mqttHost: ");
  Serial.println(mqtt_server);
  Serial.print("mqttPort: ");
  Serial.println(mqtt_port);
  Serial.print("inTop: ");
  Serial.println(mqtt_topic_in);
  Serial.print("outTop: ");
  Serial.println(mqtt_topic_out);
  Serial.print("outTop_hold1: ");
  Serial.println(mqtt_topic_out_hold_1);
  Serial.print("outTop_hold2: ");
  Serial.println(mqtt_topic_out_hold_2);
  Serial.print("outTop_hold3: ");
  Serial.println(mqtt_topic_out_hold_3);
  Serial.print("outPld_hold1: ");
  Serial.println(mqtt_payload_out_hold_1);
  Serial.print("outPld_hold2: ");
  Serial.println(mqtt_payload_out_hold_2);
  Serial.print("outPld_hold3: ");
  Serial.println(mqtt_payload_out_hold_3);
  Serial.print("domoIdx1: ");
  Serial.println(domoticzIdx[0]);
  Serial.print("domoIdx2: ");
  Serial.println(domoticzIdx[1]);
  Serial.print("domoIdx3: ");
  Serial.println(domoticzIdx[2]);
  Serial.print("domoOutTop: ");
  Serial.println(domoticz_out_topic);
  Serial.print("impuls1: ");
  Serial.println(relais_impulse[0]);
  Serial.print("impuls2: ");
  Serial.println(relais_impulse[1]);
  Serial.print("impuls3: ");
  Serial.println(relais_impulse[2]);
}


bool setConfig(char* param, char* value) {

//  char buf[80];
//  int len = strlen(value);
//  for (int i = 0; i < len; i++) {
//    buf[i] = value[i];
//  }
//  buf[len] = '\0';

  Serial.print("param: ");
  Serial.print(param);
  Serial.print(", value: ");
  Serial.println(value);

  if ( strcmp(param, "devName") == 0 ) {
    strcpy(deviceName, value);
  }
  else if ( strcmp(param, "mqttHost") == 0 ) {
    strcpy(mqtt_server, value);
  }
    else if ( strcmp(param, "mqttPort") == 0 ) {
      mqtt_port = atoi(value);
    }
    else if ( strcmp(param, "inTop") == 0 ) {
      strcpy(mqtt_topic_in, value);
    }
    else if ( strcmp(param, "outTop") == 0 ) {
      strcpy(mqtt_topic_out, value);
    }
    else if ( strcmp(param, "outTop_hold1") == 0 ) {
      strcpy(mqtt_topic_out_hold_1, value);
    }
    else if ( strcmp(param, "outTop_hold2") == 0 ) {
      strcpy(mqtt_topic_out_hold_2, value);
    }
    else if ( strcmp(param, "outTop_hold3") == 0 ) {
      strcpy(mqtt_topic_out_hold_3, value);
    }
    else if ( strcmp(param, "outPld_hold1") == 0 ) {
      Serial.print("set outPld_hold1");
      strcpy(mqtt_payload_out_hold_1, value);
    }
    else if ( strcmp(param, "outPld_hold2") == 0 ) {
      Serial.print("set outPld_hold2");
      strcpy(mqtt_payload_out_hold_2, value);
    }
    else if ( strcmp(param, "outPld_hold3") == 0 ) {
      Serial.print("set outPld_hold3");
      strcpy(mqtt_payload_out_hold_3, value);
    }
    else if ( strcmp(param, "domoIdx1") == 0 ) {
      domoticzIdx[0] = atoi(value);
    }
    else if ( strcmp(param, "domoIdx2") == 0 ) {
      domoticzIdx[1] = atoi(value);
    }
    else if ( strcmp(param, "domoIdx3") == 0 ) {
      domoticzIdx[2] = atoi(value);
    }
    else if ( strcmp(param, "domoOutTop") == 0 ) {
      strcpy(domoticz_out_topic, value);
    }
    else if ( strcmp(param, "impuls1") == 0 ) {
      relais_impulse[0] = atoi(value);
    }
    else if ( strcmp(param, "impuls2") == 0 ) {
      relais_impulse[1] = atoi(value);
    }
    else if ( strcmp(param, "impuls3") == 0 ) {
      relais_impulse[2] = atoi(value);
    }
}


bool saveConfig() {
  StaticJsonBuffer<1000> jsonBuffer;
  JsonObject& json = jsonBuffer.createObject();

  json["devName"] = deviceName;
  json["mqttHost"] = mqtt_server;
  json["mqttPort"] = mqtt_port;
  json["inTop"] = mqtt_topic_in;
  json["outTop"] = mqtt_topic_out;
  json["outTop_hold1"] = mqtt_topic_out_hold_1;
  json["outTop_hold2"] = mqtt_topic_out_hold_2;
  json["outTop_hold3"] = mqtt_topic_out_hold_3;
  json["outPld_hold1"] = mqtt_payload_out_hold_1;
  json["outPld_hold2"] = mqtt_payload_out_hold_2;
  json["outPld_hold3"] = mqtt_payload_out_hold_3;
  json["domoIdx1"] = domoticzIdx[0];
  json["domoIdx2"] = domoticzIdx[1];
  json["domoIdx3"] = domoticzIdx[2];
  json["domoOutTop"] = domoticz_out_topic;
  json["impuls1"] = relais_impulse[0];
  json["impuls2"] = relais_impulse[1];
  json["impuls3"] = relais_impulse[2];

  File configFile = SPIFFS.open("/config.json", "w");
  if (!configFile) {
    Serial.println("Failed to open config file for writing");
    return false;
  }

  json.printTo(configFile);
  return true;
}
