<?php

declare(strict_types=1);

namespace App\Framework\Console\Examples;

use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\ConsoleInput;
use App\Framework\Console\ConsoleOutput;
use App\Framework\Console\Spinner;
use App\Framework\Console\SpinnerStyle;
use App\Framework\Core\ValueObjects\Duration;
use App\Framework\DateTime\Timer;

class SpinnerExample
{
    public function __construct(
        private readonly Timer $timer
    ) {
    }

    #[ConsoleCommand(name: 'demo:spinner', description: 'Zeigt eine Demonstration der Spinner-Komponente')]
    public function showSpinnerDemo(ConsoleInput $input, ConsoleOutput $output): int
    {
        $output->writeInfo('Demonstration der Spinner-Komponente');
        $output->newLine();

        // Einfacher Spinner
        $spinner = new Spinner($output, 'Lade Daten...');
        $spinner->start();

        // Simuliere Arbeit
        for ($i = 0; $i < 10; $i++) {
            $this->timer->sleep(Duration::fromMilliseconds(100));
            $spinner->update();
        }

        $spinner->success('Daten erfolgreich geladen!');

        // Spinner mit verschiedenen Stilen
        $styles = [
            SpinnerStyle::DOTS,
            SpinnerStyle::LINE,
            SpinnerStyle::BOUNCE,
            SpinnerStyle::ARROW,
        ];

        foreach ($styles as $style) {
            $output->writeLine("Spinner-Stil: {$style->name}");
            $spinner = new Spinner($output, 'Verarbeite...', $style);
            $spinner->start();

            for ($i = 0; $i < 15; $i++) {
                $this->timer->sleep(Duration::fromMilliseconds(100));

                if ($i === 5) {
                    $spinner->setMessage('Fast fertig...');
                }

                $spinner->update();
            }

            $spinner->success('Fertig!');
        }

        // Fehlerhafter Prozess
        $spinner = new Spinner($output, 'Verbinde mit Server...');
        $spinner->start();

        for ($i = 0; $i < 8; $i++) {
            $this->timer->sleep(Duration::fromMilliseconds(150));
            $spinner->update();
        }

        $spinner->error('Verbindung fehlgeschlagen!');

        $output->writeSuccess('Demonstration abgeschlossen!');

        return 0;
    }
}
