#include "TabViewWidget.hpp"
#include "../WidgetManager.hpp"
#include "esp_log.h"

static const char* TAG = "TabViewWidget";

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

TabViewWidget::~TabViewWidget() {
}

lv_obj_t* TabViewWidget::create(lv_obj_t* parent) {
    // Determine tab position based on config
    // We can reuse iconPosition property: 0=Top, 1=Bottom, 2=Left, 3=Right
    lv_dir_t tabPos = LV_DIR_TOP;
    if (config_.iconPosition == 1) tabPos = LV_DIR_BOTTOM;
    else if (config_.iconPosition == 2) tabPos = LV_DIR_LEFT;
    else if (config_.iconPosition == 3) tabPos = LV_DIR_RIGHT;

    // Use iconSize as tab height (e.g., 50px)
    lv_coord_t tabHeight = config_.iconSize > 0 ? config_.iconSize * 10 : 50;

    // LVGL v9 API: Only parent as argument
    obj_ = lv_tabview_create(parent);
    lv_tabview_set_tab_bar_position(obj_, tabPos);
    lv_tabview_set_tab_bar_size(obj_, tabHeight);
    
    lv_obj_set_pos(obj_, config_.x, config_.y);
    lv_obj_set_size(obj_, config_.width > 0 ? config_.width : 300,
                          config_.height > 0 ? config_.height : 200);

    ESP_LOGI(TAG, "Created TabView at %d,%d", config_.x, config_.y);
    return obj_;
}

void TabViewWidget::applyStyle() {
    if (obj_ == nullptr) return;
    
    // Apply styling to the main container
    applyCommonStyle();
    
    // Style the tab buttons (Tab Bar in v9)
    lv_obj_t* bar = lv_tabview_get_tab_bar(obj_);
    
    lv_obj_set_style_bg_color(bar, lv_color_make(
        config_.bgColor.r, config_.bgColor.g, config_.bgColor.b), 0);
        
    lv_obj_set_style_text_color(bar, lv_color_make(
        config_.textColor.r, config_.textColor.g, config_.textColor.b), 0);
}
