#pragma once

#include <vector>
#include <utility>
#include <string>
#include <memory>
#include "utils.h"
#include "ui_message.h"

// Forward Declarations
class Text;
class MenuOption;
class MenuRenderer; // <-- Nuevo

class ServiceMenu
{
public:
    enum class SettingsGroup
    {
        VIDEO,
        AUDIO,
        SETTINGS,
        SYSTEM,
        MAIN
    };

    enum class GroupAlignment
    {
        CENTERED,
        LEFT
    };

    // --- Constantes públicas que el Renderer podría necesitar ---
    static constexpr size_t OPTIONS_HORIZONTAL_PADDING_ = 20;
    static constexpr size_t MIN_WIDTH_ = 240;
    static constexpr size_t MIN_GAP_OPTION_VALUE_ = 30;

    static void init();
    static void destroy();
    static ServiceMenu *get();

    void toggle();
    void render();
    void update();
    void reset();

    // --- Lógica de navegación ---
    void setSelectorUp();
    void setSelectorDown();
    void adjustOption(bool adjust_up);
    void selectOption();
    void moveBack();

    // --- Getters para que el Renderer pueda leer el estado ---
    bool isEnabled() const { return enabled_; }
    const std::string &getTitle() const { return title_; }
    SettingsGroup getCurrentGroup() const { return current_settings_group_; }
    GroupAlignment getCurrentGroupAlignment() const;
    const std::vector<MenuOption *> &getDisplayOptions() const { return display_options_; }
    const std::vector<std::unique_ptr<MenuOption>> &getAllOptions() const { return options_; }
    size_t getSelectedIndex() const { return selected_; }
    const std::vector<std::pair<std::string, std::string>> &getOptionPairs() const { return option_pairs_; }
    size_t countOptionsInGroup(SettingsGroup group) const;

private:
    // --- Lógica de estado del menú (Modelo) ---
    bool enabled_ = false;
    std::vector<std::unique_ptr<MenuOption>> options_;
    std::vector<MenuOption *> display_options_;
    std::vector<std::pair<std::string, std::string>> option_pairs_;

    SettingsGroup current_settings_group_;
    SettingsGroup previous_settings_group_;
    std::string title_;
    size_t selected_ = 0;
    size_t main_menu_selected_ = 0;

    // --- Mensaje de reinicio ---
    std::unique_ptr<UIMessage> restart_message_ui_;
    bool last_pending_changes_ = false;

    // --- La Vista ---
    std::unique_ptr<MenuRenderer> renderer_;

    // --- Métodos de lógica interna ---
    void updateDisplayOptions();
    void updateOptionPairs();
    void initializeOptions();
    void updateMenu();
    void applySettings();
    void applyVideoSettings();
    void applyAudioSettings();
    void applySettingsSettings();
    MenuOption *getOptionByCaption(const std::string &caption) const;
    void AdjustListValues();
    void playMenuSound();
    std::string settingsGroupToString(SettingsGroup group) const;

    // --- Singleton ---
    ServiceMenu();
    ~ServiceMenu() = default;
    ServiceMenu(const ServiceMenu &) = delete;
    ServiceMenu &operator=(const ServiceMenu &) = delete;
    static ServiceMenu *instance_;
};
