<?php


namespace App\Controller;


use App\Service\DocumentResolver;
use App\Service\KeyLighterVersioner;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class HomeController extends AbstractController
{
    /** @var DocumentResolver */
    private $resolver;

    /** @var \App\Service\KeyLighterVersioner  */
    private $versioner;

    /**
     * HomeController constructor.
     *
     * @param DocumentResolver $resolver
     */
    public function __construct(DocumentResolver $resolver, KeyLighterVersioner $versioner)
    {
        $this->resolver = $resolver;
        $this->versioner = $versioner;
    }

    /**
     * @Route("/", name="homepage")
     */
    public function indexAction()
    {
        $document = $this->resolver->getDocument('README');

        return $this->render('homepage.html.twig', compact('document'));
    }

    /**
     * @Route("/download/keylighter.phar")
     * @Route("/download/{version}/keylighter.phar")
     */
    public function downloadPharAction($version = null)
    {
        $directory = $this->versioner->getCurrentDir();
        if ($version) {
            $directory = $this->versioner->getDirectory($version);
        }
        $directory .= DIRECTORY_SEPARATOR;

        return $this->file($directory."keylighter.phar");
    }

    /**
     * @Route("/changelog", name="changelog")
     */
    public function changelogAction()
    {
        $document = $this->resolver->getDocument('changelog');

        return $this->render('document.html.twig', [
            'document' => $document,
            'current'  => 'changelog'
        ]);
    }
}
