<?php

namespace KupShop\KupShopBundle\Context;

use KupShop\I18nBundle\Entity\Language;
use KupShop\KupShopBundle\Config;

class LanguageContext implements ContextInterface
{
    /** @var string */
    protected $activeId;

    /** @var Language[] */
    protected $supported;

    protected ?array $languages = null;

    public function __construct()
    {
    }

    public function remember(string $id)
    {
    }

    public function activate(string $id)
    {
        $this->activeId = $id;

        return true;
    }

    /**
     * @return Language
     */
    public function getActive()
    {
        $active = $this->getActiveId();

        // use `getAll` so inactive language can be used (for example tmp wpjshop domain with not active language)
        // validation should be done explicitly when remembering or loading language
        $supported = $this->getAll();

        if (!isset($supported[$active])) {
            $active = key($supported);
        }

        $result = $supported[$active];

        // TODO: Dočasně kontrolujeme, že je všude vyplněný language code
        if (isDevelopment() && empty($result->getLocale())) {
            throw new \Exception('Prázdný language code');
        }

        return $result;
    }

    /**
     * @return Language[]
     */
    public function getSupported()
    {
        if (!$this->supported) {
            $this->supported = $this->loadSupported();
        }

        return $this->supported;
    }

    /**
     * @return array<string, Language>
     */
    public function getAll(): array
    {
        if ($this->languages !== null) {
            return $this->languages;
        }

        $lang = $this->getDefaultFromConfig();
        $locale = Config::get()->getFromArray('Lang')['language_code'] ?? 'cs_CZ';

        return $this->languages = [$lang => (new Language())->setId($lang)->setName($lang)->setLocale($locale)];
    }

    /**
     * @return bool
     */
    protected function validate($id)
    {
        return array_key_exists($id, $this->getSupported());
    }

    /**
     * @return Language[]
     */
    protected function loadSupported()
    {
        return $this->getAll();
    }

    protected function loadActive()
    {
        return $this->getDefaultFromConfig();
    }

    public function getDefaultId(): string
    {
        return $this->getDefaultFromConfig();
    }

    public function translationActive()
    {
        if ($this->getActiveId() == $this->getDefaultId()) {
            return false;
        }

        return true;
    }

    protected function getDefaultFromConfig()
    {
        return Config::get()->getFromArray('Lang')['language'];
    }

    public function getActiveId(): string
    {
        if (is_null($this->activeId)) {
            $this->activeId = $this->loadActive();
        }

        return $this->activeId;
    }

    public function getInheritance(string $language): array
    {
        return [$language];
    }

    public function clearCache(): void
    {
        $this->activeId = null;
    }

    public function isValid(string $id): bool
    {
        return $this->validate($id);
    }
}
