<?php


namespace App\Service;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;

class Page
{
    private KernelInterface $kernel;

    /**
     * @param KernelInterface $kernel
     */
    public function __construct(KernelInterface $kernel)
    {
        $this->kernel = $kernel;
    }

    public function getPage(string $page)
    {
        $parsedown = new \Parsedown();
        $return['js'] = [];
        $return['css'] = [];
        if(file_exists($this->kernel->getCacheDir() . '/pages/_header.md')) {
            $return['header'] = $parsedown->text(file_get_contents($this->kernel->getCacheDir() . '/pages/_header.md'));
        } elseif(file_exists($this->kernel->getCacheDir() . '/pages/_header.html')) {
            $return['header'] = file_get_contents($this->kernel->getCacheDir() . '/pages/_header.html');
        } else {
            throw new NotFoundHttpException('Some required files were not found');
        }

        if(file_exists($this->kernel->getProjectDir() . '/public/assets/js/_header.js')) {
            $return['js'][] = [
                'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/js/_header.js'), true)),
                'file' => '/assets/js/_header.js'
            ];
        }
        if(file_exists($this->kernel->getProjectDir() . '/public/assets/css/_header.css')) {
            $return['css'][] = [
                'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/css/_header.css'), true)),
                'file' => '/assets/css/_header.css'
            ];
        }

        if(file_exists($this->kernel->getCacheDir() . '/pages/_nav.md')) {
            $return['nav'] = $parsedown->text(file_get_contents($this->kernel->getCacheDir() . '/pages/_nav.md'));
        } elseif(file_exists($this->kernel->getCacheDir() . '/pages/_nav.html')) {
            $return['nav'] = file_get_contents($this->kernel->getCacheDir() . '/pages/_nav.html');
        } else {
            throw new NotFoundHttpException('Some required files were not found');
        }

        if(file_exists($this->kernel->getProjectDir() . '/public/assets/js/_nav.js')) {
            $return['js'][] = [
                'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/js/_nav.js'), true)),
                'file' => '/assets/js/_nav.js'
            ];
        }
        if(file_exists($this->kernel->getProjectDir() . '/public/assets/css/_nav.css')) {
            $return['css'][] = [
                'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/css/_nav.css'), true)),
                'file' => '/assets/css/_nav.css'
            ];
        }

        if(file_exists($this->kernel->getCacheDir() . '/pages/_footer.md')) {
            $return['footer'] = $parsedown->text(file_get_contents($this->kernel->getCacheDir() . '/pages/_footer.md'));
        } elseif(file_exists($this->kernel->getCacheDir() . '/pages/_footer.html')) {
            $return['footer'] = file_get_contents($this->kernel->getCacheDir() . '/pages/_footer.html');
        } else {
            throw new NotFoundHttpException('Some required files were not found');
        }

        if(file_exists($this->kernel->getProjectDir() . '/public/assets/js/_footer.js')) {
            $return['js'][] = [
                'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/js/_footer.js'), true)),
                'file' => '/assets/js/_footer.js'
            ];
        }
        if(file_exists($this->kernel->getProjectDir() . '/public/assets/css/_footer.css')) {
            $return['css'][] = [
                'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/css/_footer.css'), true)),
                'file' => '/assets/css/_footer.css'
            ];
        }

        if(file_exists($this->kernel->getCacheDir() . '/pages/' . $page . '.md')) {
            $return['content'] = $parsedown->text(file_get_contents($this->kernel->getCacheDir() . '/pages/' . $page . '.md'));
        } elseif(file_exists($this->kernel->getCacheDir() . '/pages/' . $page . '.html')) {
            $return['content'] = file_get_contents($this->kernel->getCacheDir() . '/pages/' . $page . '.html');
        } else {
            throw new NotFoundHttpException('Some required files were not found');
        }

        if(file_exists($this->kernel->getProjectDir() . '/public/assets/js/' . $page . '.js')) {
            $return['js'][] = [
                'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/js/' . $page . '.js'), true)),
                'file' => '/assets/js/' . $page . '.js'
            ];
        }
        if(file_exists($this->kernel->getProjectDir() . '/public/assets/css/' . $page . '.css')) {
            $return['css'][] = [
                'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/css/' . $page . '.css'), true)),
                'file' => '/assets/css/' . $page . '.css'
            ];
        }

        $titles = json_decode(file_get_contents($this->kernel->getCacheDir() . '/pages/titles.json'), true);
        $return['title'] = $titles[$page] ?? '';
        $return['status'] = '200';

        return $return;
    }
}