<?php

namespace PHPNative\Ui\Widget;

use PHPNative\Framework\TextRenderer;

class TabContainer extends Container
{
    private Container $tabHeaderContainer;
    private Container $tabContentContainer;
    private array $tabs = [];
    private int $activeTabIndex = 0;

    public function __construct(string $style = '')
    {
        parent::__construct('flex flex-col ' . $style);

        // Create header container for tab buttons
        $this->tabHeaderContainer = new Container('flex flex-row bg-gray-100 border-b border-gray-300');
        $this->addComponent($this->tabHeaderContainer);

        // Create content container
        $this->tabContentContainer = new Container('flex-1');
        $this->addComponent($this->tabContentContainer);
    }

    /**
     * Add a tab with title and content
     *
     * @param string $title Tab title
     * @param Container $content Tab content container
     */
    public function addTab(string $title, Container $content): void
    {
        $tabIndex = count($this->tabs);
        $isActive = $tabIndex === $this->activeTabIndex;

        // Create tab button
        $tabStyle = $isActive
            ? 'bg-white border-t border-l border-r border-gray-300 px-4 py-2'
            : 'bg-gray-200 hover:bg-gray-300 border-t border-l border-r border-transparent px-4 py-2';

        $tabButton = new Button($title, $tabStyle);
        $tabButton->setOnClick(function () use ($tabIndex) {
            $this->setActiveTab($tabIndex);
        });

        $this->tabHeaderContainer->addComponent($tabButton);

        $this->tabs[] = [
            'title' => $title,
            'button' => $tabButton,
            'content' => $content,
        ];

        // Add content to container if it's the active tab
        if ($isActive) {
            $this->tabContentContainer->clearChildren();
            $this->tabContentContainer->addComponent($content);
        }
    }

    /**
     * Set the active tab by index
     *
     * @param int $index Tab index
     */
    public function setActiveTab(int $index): void
    {
        if ($index < 0 || $index >= count($this->tabs)) {
            return;
        }

        $this->activeTabIndex = $index;

        // Update tab button styles
        foreach ($this->tabs as $i => $tab) {
            $isActive = $i === $index;
            $style = $isActive
                ? 'bg-white border-t border-l border-r border-gray-300 px-4 py-2'
                : 'bg-gray-200 hover:bg-gray-300 border-t border-l border-r border-transparent px-4 py-2';

            $tab['button']->setStyle($style);
        }

        // Update content
        $this->tabContentContainer->clearChildren();
        $this->tabContentContainer->addComponent($this->tabs[$index]['content']);
    }

    /**
     * Get the active tab index
     *
     * @return int
     */
    public function getActiveTabIndex(): int
    {
        return $this->activeTabIndex;
    }

    /**
     * Get total number of tabs
     *
     * @return int
     */
    public function getTabCount(): int
    {
        return count($this->tabs);
    }
}
