<?php

declare(strict_types=1);

namespace Gls\GlsPoland\PrestaShop\Hook\Admin;

use Gls\GlsPoland\Asset\AssetManagerInterface;
use Gls\GlsPoland\PrestaShop\Hook\HookInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;

final class ActionAdminControllerSetMedia implements HookInterface
{
    public const HOOK_NAME = 'actionAdminControllerSetMedia';

    private $assetManager;

    public function __construct(AssetManagerInterface $assetManager)
    {
        $this->assetManager = $assetManager;
    }

    /**
     * @param array{request: Request} $parameters
     */
    public function execute(array $parameters): void
    {
        $request = $parameters['request'] ?? null;
        if (!$request instanceof Request) {
            return;
        }

        $route = $request->get('_route') ?? $this->guessRoute($request);

        if (null === $route) {
            return;
        }

        $javaScripts = $this->getJavaScriptsByRoute($route);
        $styleSheets = $this->getStyleSheetsByRoute($route);

        foreach ($javaScripts as $javaScript) {
            $this->assetManager->registerJavaScript($javaScript);
        }

        foreach ($styleSheets as $styleSheet) {
            $this->assetManager->registerStyleSheet($styleSheet);
        }
    }

    private function getJavaScriptsByRoute(string $route): array
    {
        return [];
    }

    private function getStyleSheetsByRoute(string $route): array
    {
        return [];
    }

    private function guessRoute(Request $request): ?string
    {
        $controller = $request->query->get('controller');
        if ('AdminOrders' !== $controller) {
            return null;
        }

        return $this->guessAdminOrdersRoute($request->query);
    }

    private function guessAdminOrdersRoute(ParameterBag $query): string
    {
        if ($query->has('vieworder')) {
            return 'admin_orders_view';
        }

        if ($query->has('addorder')) {
            return 'admin_orders_create';
        }

        return 'admin_orders_index';
    }
}
