<?php

namespace KupShop\KupShopBundle\Util\Compat;

use KupShop\DevelopmentBundle\Util\Tests\TestAppKernel;
use KupShop\KupShopBundle\KupShopAppKernel;
use KupShop\KupShopBundle\Util\KupShopContainer;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpKernel\Kernel;

class ServiceContainer
{
    /** @var Kernel */
    private static $kernel;

    /**
     * @return Kernel
     */
    public static function getKernel()
    {
        if (!self::$kernel) {
            global $kernel;

            if ($kernel instanceof KupShopAppKernel) {
                return self::$kernel = $kernel;
            }

            if (defined('TEST_SUITE') && TEST_SUITE == 'engine') {
                $kernel = new TestAppKernel('test_engine', true);
            } else {
                if (!empty(getenv('SYMFONY_ENV'))) {
                    $env = getenv('SYMFONY_ENV');
                } elseif (isDevelopment()) {
                    if (defined('TEST_SUITE')) {
                        $env = 'test';
                    } else {
                        $env = 'dev';
                    }
                } else {
                    $env = 'prod';
                }

                if ($env == 'dev') {
                    Debug::enable();
                }

                $kernel = new \AppKernel($env, $env != 'prod');
            }

            try {
                $kernel->boot();
            } catch (\Exception $e) {
                $kernel = null;

                throw $e;
            }

            self::$kernel = $kernel;
        }

        return self::$kernel;
    }

    /**
     * @return Container
     */
    public static function getContainer()
    {
        $container = self::getKernel()->getContainer();

        if (isFunctionalTests()) {
            return $container->get('test.service_container');
        }

        return $container;
    }

    public static function getKupShopContainer()
    {
        return self::getContainer()->get(KupShopContainer::class);
    }

    /**
     * @template T
     *
     * @param $service string|class-string<T>
     *
     * @return mixed|T
     *
     * @deprecated Vždy použij DependencyInjection! Pouze v adminu a class.*.php je povoleno tohle používat
     */
    public static function getService($service, $notExists = 1)
    {
        // vzit si tu servisu z kupshop containeru jenom kdyz neni v normalnim containeru
        if (!self::getContainer()->has($service) && self::getKupShopContainer()->has($service)) {
            return self::getKupShopContainer()->get($service);
        }

        return self::getContainer()->get($service, $notExists);
    }

    /**
     * @deprecated Use SymfonyBridge::getCurrentRequest
     */
    public static function getCurrentRequest()
    {
        return SymfonyBridge::getCurrentRequest();
    }

    public static function destroy()
    {
        global $kernel;
        $kernel = null;

        return self::$kernel = null;
    }

    public static function setKernel(Kernel $kernel)
    {
        self::$kernel = $kernel;
    }
}
