<?php

declare(strict_types=1);

namespace App\Services;

/**
 * Class HtmlContentGenerator
 *
 * Trieda na generovanie nahodneho HTML obsahu
 *
 * @author Radoslav Lukac <dev@seka.sk>
 */
class HtmlContentGenerator
{
    /**
     * Pole povolenenych HTML tagov pre generovanie
     *
     * @var array<string>
     */
    private array $tags = [
        'p',
        'h1',
        'h2',
        'h3',
        'div',
        'strong',
        'em',
        'ul',
        'ol',
    ];

    /**
     * Pole dostupnych farieb pre pouzitie v styloch
     *
     * @var array<string>
     */
    private array $colors = [
        '#2d3748',
        '#4a5568',
        '#718096',
        '#4299e1',
        '#48bb78',
        '#ed8936',
        '#ed64a6',
        '#667eea',
        '#9f7aea',
    ];

    /**
     * Pole Lorem ipsum textov pre nahodny obsah
     *
     * @var array<string>
     */
    private array $loremTexts = [
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
        'Ut enim ad minim veniam, quis nostrud exercitation ullamco.',
        'Duis aute irure dolor in reprehenderit in voluptate velit esse.',
        'Excepteur sint occaecat cupidatat non proident.',
    ];

    /**
     * Generuje nahodny HTML obsah
     *
     * @param int $numberOfElements Pocet elementov na vygenerovanie
     *
     * @return string Vygenerovany HTML kod
     */
    public function generate(int $numberOfElements = 5): string
    {
        $html = '';

        for ($i = 0; $i < $numberOfElements; $i++) {
            $html .= $this->generateElement();
        }

        return $html;
    }

    /**
     * Generuje jeden nahodny HTML element
     *
     * @return string Vygenerovany HTML element
     */
    private function generateElement(): string
    {
        $tag = $this->tags[array_rand($this->tags)];
        $style = $this->generateStyle();

        switch ($tag) {
            case 'ul':
            case 'ol':
                return $this->generateList($tag);
            default:
                return sprintf(
                    '<%1$s style="%2$s">%3$s</%1$s>',
                    $tag,
                    $style,
                    $this->loremTexts[array_rand($this->loremTexts)],
                );
        }
    }

    /**
     * Generuje nahodne CSS styly
     *
     * @return string Vygenerovany CSS styl
     */
    private function generateStyle(): string
    {
        $styles = [
            'color: ' . $this->colors[array_rand($this->colors)],
            'margin: ' . rand(5, 20) . 'px',
            'padding: ' . rand(5, 15) . 'px',
        ];

        if (rand(0, 1)) {
            $styles[] = 'background-color: ' . $this->colors[array_rand($this->colors)] . '20';
        }

        return implode('; ', $styles);
    }

    /**
     * Generuje nahodny zoznam (ul/ol)
     *
     * @param string $tag Typ zoznamu (ul/ol)
     *
     * @return string Vygenerovany HTML zoznam
     */
    private function generateList(string $tag): string
    {
        $items = '';
        $numberOfItems = rand(2, 5);

        for ($i = 0; $i < $numberOfItems; $i++) {
            $items .= sprintf(
                '<li style="%s">%s</li>',
                $this->generateStyle(),
                $this->loremTexts[array_rand($this->loremTexts)],
            );
        }

        return sprintf(
            '<%1$s style="%2$s">%3$s</%1$s>',
            $tag,
            $this->generateStyle(),
            $items,
        );
    }
}
