#pragma once

#include "Widget.hpp"

class RoomCardWidgetBase : public Widget {
public:
    explicit RoomCardWidgetBase(const WidgetConfig& config);
    ~RoomCardWidgetBase() override;

    void onKnxValue(float value) override;      // Temperature update
    void onKnxSwitch(bool value) override;      // Not used directly

    // Sub-button specific handling
    void onSubButtonStatus(uint8_t index, bool value);
    void sendSubButtonToggle(uint8_t index);

    // Clear LVGL objects
    virtual void clearLvglObject();

protected:
    // Parent (screen) for sub-buttons to avoid clipping
    lv_obj_t* screenParent_ = nullptr;

    // Common elements
    lv_obj_t* roomLabel_ = nullptr;
    lv_obj_t* tempLabel_ = nullptr;

    // Sub-button elements
    lv_obj_t* subButtonObjs_[MAX_SUBBUTTONS] = {};
    lv_obj_t* subButtonIcons_[MAX_SUBBUTTONS] = {};
    bool subButtonStates_[MAX_SUBBUTTONS] = {};

    // Cached config values
    char roomName_[MAX_TEXT_LEN] = {0};
    char tempFormat_[MAX_TEXT_LEN] = {0};

    // Common helpers
    void parseTextBase();
    void updateSubButtonColor(uint8_t index);
    void updateTemperature(float value);

    // Virtual methods for layout-specific sub-button positioning
    virtual void calculateSubButtonPosition(uint8_t index, const SubButtonConfig& cfg, int16_t& x, int16_t& y) = 0;
    void createSubButtonsCommon();
    void applySubButtonStyle();

    // Event handlers
    static void cardClickCallback(lv_event_t* e);
    static void subButtonClickCallback(lv_event_t* e);

    // UTF-8 encoding helper
    static int encodeUtf8(uint32_t codepoint, char* buf);
};
