#include "LedWidget.hpp"

LedWidget::LedWidget(const WidgetConfig& config)
    : Widget(config)
{
}

lv_obj_t* LedWidget::create(lv_obj_t* parent) {
    obj_ = lv_led_create(parent);

    lv_obj_set_pos(obj_, config_.x, config_.y);
    if (config_.width > 0 && config_.height > 0) {
        lv_obj_set_size(obj_, config_.width, config_.height);
    }

    return obj_;
}

void LedWidget::applyStyle() {
    if (obj_ == nullptr) return;

    // LED-specific styling
    lv_obj_set_style_radius(obj_, LV_RADIUS_CIRCLE, 0);
    lv_led_set_color(obj_, lv_color_make(
        config_.bgColor.r, config_.bgColor.g, config_.bgColor.b));
    lv_led_set_brightness(obj_, config_.bgOpacity);

    if (config_.borderWidth > 0 && config_.borderOpacity > 0) {
        lv_obj_set_style_border_width(obj_, config_.borderWidth, 0);
        lv_obj_set_style_border_color(obj_, lv_color_make(
            config_.borderColor.r, config_.borderColor.g, config_.borderColor.b), 0);
        lv_obj_set_style_border_opa(obj_, config_.borderOpacity, 0);
    } else {
        lv_obj_set_style_border_width(obj_, 0, 0);
    }

    // Shadow
    applyShadowStyle();
}

void LedWidget::onKnxSwitch(bool value) {
    if (obj_ == nullptr) return;
    if (config_.textSource != TextSource::KNX_DPT_SWITCH) return;

    uint8_t brightness = value ? (config_.bgOpacity > 0 ? config_.bgOpacity : 255) : 0;
    lv_led_set_brightness(obj_, brightness);
}
