#ifndef FanMqttSettingsService_h
#define FanMqttSettingsService_h

#include <HttpEndpoint.h>
#include <FSPersistence.h>
#include <SettingValue.h>

#define LIGHT_BROKER_SETTINGS_FILE "/config/brokerSettings.json"
#define LIGHT_BROKER_SETTINGS_PATH "/rest/brokerSettings"

class FanMqttSettings {
 public:
  String mqttPath;
  String name;
  String uniqueId;

  static void read(FanMqttSettings& settings, JsonObject& root) {
    root["mqtt_path"] = settings.mqttPath;
    root["name"]      = settings.name;
    root["unique_id"] = settings.uniqueId;
  }

  static StateUpdateResult update(JsonObject& root, FanMqttSettings& settings) {
    settings.mqttPath = root["mqtt_path"] | SettingValue::format("homeassistant/fan/#{unique_id}");
    settings.name     = root["name"]      | SettingValue::format("light-#{unique_id}");
    settings.uniqueId = root["unique_id"] | SettingValue::format("light-#{unique_id}");
    return StateUpdateResult::CHANGED;
  }
};

class FanMqttSettingsService : public StatefulService<FanMqttSettings> {
 public:
  FanMqttSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
  void begin();

 private:
  HttpEndpoint<FanMqttSettings>  _httpEndpoint;
  FSPersistence<FanMqttSettings> _fsPersistence;
};

#endif  // end FanMqttSettingsService_h
