<?php

namespace App\Controller;

use App\Service\KeyLighterVersioner;
use Kadet\Highlighter\KeyLighter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class StylesAction
{
    private $versioner;

    public function __construct(KeyLighterVersioner $versioner)
    {
        $this->versioner = $versioner;
    }

    /**
     * @Route("/keylighter.css", name="keylighter_stylesheet")
     */
    public function __invoke()
    {
        $path = $this->getCssFilePath();

        return new Response(file_get_contents($path), Response::HTTP_OK, [
            'Content-Type' => 'text/css',
        ]);
    }

    private function getCssFilePath()
    {
        switch (true) {
            case version_compare(KeyLighter::VERSION, '0.9.0', '>='):
                $base = $this->versioner->getCurrentDir();
                return sprintf("%s/%s", $base, 'Styles/Html/dist/keylighter.css');

            default:
                $base = $this->versioner->getDirectory('dev-master');
                return sprintf("%s/%s", $base, 'Styles/Html/dist/keylighter.css');
        }
    }
}
