#pragma once

#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/core/preferences.h"
#if !defined(USE_RP2) && !defined(USE_HOST)

#ifdef USE_ESP32
#include <esp_system.h>
#endif

namespace esphome::factory_reset {
class FactoryResetComponent final : public Component {
 public:
  FactoryResetComponent(uint8_t required_count, uint16_t max_interval)
      : max_interval_(max_interval), required_count_(required_count) {}

  void dump_config() override;
  void setup() override;
  template<typename F> void add_increment_callback(F &&callback) {
    this->increment_callback_.add(std::forward<F>(callback));
  }

 protected:
  ~FactoryResetComponent() = default;
  void save_(uint8_t count);
  ESPPreferenceObject flash_{};  // saves the number of fast power cycles
  CallbackManager<void(uint8_t, uint8_t)> increment_callback_{};
  uint16_t max_interval_;   // max interval between power cycles in seconds
  uint8_t required_count_;  // The number of boot attempts before fast boot is enabled
};

}  // namespace esphome::factory_reset

#endif  // !defined(USE_RP2) && !defined(USE_HOST)
