#pragma once

#include <memory>
#include <string>
#include <vector>
#include <SDL3/SDL.h>
#include "ui/service_menu.h" // Necesario para las enums y para acceder al estado del menú
#include "utils.h"           // Para Color


// Forward declarations
class Text;
class MenuOption;

class MenuRenderer
{
public:
    MenuRenderer(const ServiceMenu *menu_state, std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text);

    // Métodos principales de la vista
    void render(const ServiceMenu *menu_state);
    void update(const ServiceMenu *menu_state);

    // Método para notificar al renderer que el layout puede haber cambiado
    void onLayoutChanged(const ServiceMenu *menu_state);
    void setLayout(const ServiceMenu *menu_state);

    // Getters
    const SDL_FRect &getRect() const { return rect_; }

private:
    // --- Referencias a los renderizadores de texto ---
    std::shared_ptr<Text> element_text_;
    std::shared_ptr<Text> title_text_;

    // --- Variables de estado de la vista (layout y animación) ---
    SDL_FRect rect_{};
    SDL_FRect border_rect_{};
    size_t width_ = 0;
    size_t height_ = 0;
    size_t options_height_ = 0;
    size_t options_padding_ = 0;
    size_t options_y_ = 0;
    size_t title_height_ = 0;
    size_t title_padding_ = 0;
    size_t upper_height_ = 0;
    size_t lower_height_ = 0;
    size_t lower_padding_ = 0;
    Uint32 color_counter_ = 0;

    // --- Variables para animación de resize ---
    SDL_FRect rect_anim_from_{};
    SDL_FRect rect_anim_to_{};
    int resize_anim_step_ = 0;
    int resize_anim_steps_ = 8;
    bool resizing_ = false;

    // --- Anchos precalculados ---
    int group_menu_widths_[5]{};

    // --- Métodos privados de la vista ---
    void setAnchors(const ServiceMenu *menu_state);
    SDL_FRect calculateNewRect(const ServiceMenu *menu_state);
    void resize(const ServiceMenu *menu_state);
    void setSize(const ServiceMenu *menu_state);
    void updateResizeAnimation();
    void precalculateMenuWidths(const std::vector<std::unique_ptr<MenuOption>> &all_options, const ServiceMenu *menu_state);
    int getMenuWidthForGroup(ServiceMenu::SettingsGroup group) const;
    Color getAnimatedSelectedColor();
    void updateColorCounter();
    SDL_FRect setRect(SDL_FRect rect);
};
