<?php

namespace ServerManager\UI;

use PHPNative\Tailwind\Data\Icon as IconName;
use PHPNative\Ui\Widget\Container;
use PHPNative\Ui\Widget\Icon;
use PHPNative\Ui\Widget\Label;

class LoadingIndicator extends Container
{
    private Icon $icon;
    private Label $label;
    private bool $loading = false;

    public function __construct(string $style = '')
    {
        parent::__construct('flex flex-row items-center gap-1 px-2 ' . $style);

        $this->icon = new Icon(IconName::sync, 16, 'py-2 text-blue-600');
        $this->label = new Label('Laden...', 'py-2 text-gray-700');

        $this->addComponent($this->icon);
        $this->addComponent($this->label);

        $this->setVisible(false);
    }

    public function setLoading(bool $loading): void
    {
        if ($this->loading === $loading) {
            return;
        }

        $this->loading = $loading;
        $this->setVisible($loading);
    }

    public function isLoading(): bool
    {
        return $this->loading;
    }
}
