<?php

namespace App\Service\Cron\UI;

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Console user interface
 *
 * Use it to make communicate cron-jobs with command output
 */
class ConsoleUI implements CronUIInterface
{
    protected ProgressBar $progressBar;

    public function __construct(
        private OutputInterface $output
    ) {
        $this->progressBar = new ProgressBar($output, 0);
    }

    /**
     * @inheritDoc
     */
    public function print(string $message): void
    {
        $this->output->writeln($message);
    }

    /**
     * @inheritDoc
     */
    public function progress(int $i, int $total): void
    {
        $this->progressBar->setMaxSteps($total);
        $this->progressBar->setProgress($i);

        if ($i === $total) {
            $this->print(''); // to force a line break after the progression bar ends
        }
    }
}
