/*
 * SPDX-FileCopyrightText: 2026 Suwatchai K. <suwatchai@outlook.com>
 *
 * SPDX-License-Identifier: MIT
 */

#ifndef CORE_UPDATER_OTA_UPDATER_H
#define CORE_UPDATER_OTA_UPDATER_H

#include <Arduino.h>

#if __has_include(<OTAStorage.h>)

#include <OTAStorage.h>
#define FIREBASE_OTA_STORAGE
#define FIREBASE_OTA_UPDATER getOTAUpdater()

#elif defined(ESP32) || defined(ESP8266) || defined(CORE_ARDUINO_PICO)
#define FIREBASE_OTA_UPDATER Update
#endif

#if defined(FIREBASE_OTA_STORAGE)

class OTAUpdaterClass
{
public:
    OTAUpdaterClass();
    ~OTAUpdaterClass();
    bool begin(int size, int command = 0);
    bool end();
    size_t write(uint8_t *data, size_t len);
    void setOTAStorage(uint32_t addr);
    bool isInit();

private:
    OTAStorage *storage = nullptr;
    uint32_t addr = 0;
    size_t write(uint8_t b);
    void close();
    void apply();
};

// Meyers singleton: a single shared instance across all translation units on
// every C++ standard (no C++17 inline-variable requirement), safe even when
// this header/implementation is included into multiple source files.
inline OTAUpdaterClass &getOTAUpdater()
{
    static OTAUpdaterClass instance;
    return instance;
}
#endif
#endif