#pragma once

#include "lvgl.h"
#include "esp_wifi.h"
#include <string>
#include <vector>

class WifiSetting {
public:
    static WifiSetting& instance();

    void show();
    void hide();
    bool isVisible();
    void createPasswordDialogUI();

private:
    WifiSetting() = default;
    WifiSetting(const WifiSetting&) = delete;
    WifiSetting& operator=(const WifiSetting&) = delete;

    void createUI();
    void refreshNetworkList();
    void refreshSavedNetworks();
    void updateStatusDisplay();
    void showPasswordDialog(const char* ssid);
    void hidePasswordDialog();

    static void onCloseClick(lv_event_t* e);
    static void onNetworkSelect(lv_event_t* e);
    static void onScanClick(lv_event_t* e);
    static void onDisconnectClick(lv_event_t* e);
    static void onConnectClick(lv_event_t* e);
    static void onCancelClick(lv_event_t* e);
    static void onDeleteSavedClick(lv_event_t* e);
    static void onKeyboardReady(lv_event_t* e);

    lv_obj_t* overlay_ = nullptr;
    lv_obj_t* statusLabel_ = nullptr;
    lv_obj_t* disconnectBtn_ = nullptr;
    lv_obj_t* networkList_ = nullptr;
    lv_obj_t* savedList_ = nullptr;
    lv_obj_t* passwordDialog_ = nullptr;
    lv_obj_t* passwordInput_ = nullptr;
    lv_obj_t* keyboard_ = nullptr;

    std::string selectedSSID_;
    std::vector<wifi_ap_record_t> scannedNetworks_;

    bool visible_ = false;
};
