<?php

namespace KupShop\KupShopBundle\Util\Locale;

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\ServiceTrait;
use KupShop\KupShopBundle\Util\System\BundleFinder;
use KupShop\KupShopBundle\Util\System\PathFinder;

class TemplateTranslator
{
    use ServiceTrait;

    /**
     * @var WarningGettextTranslator[]
     */
    private $translations;

    /**
     * @return WarningGettextTranslator
     */
    public function get($language)
    {
        if (!isset($this->translations[$language])) {
            $this->translations[$language] = $this->loadTranslations($language);
        }

        return $this->translations[$language];
    }

    public function mergeTranslations(&$translations, $file)
    {
        if (file_exists($file)) {
            $loadedTranslations = \Gettext\Translations::fromPoFile($file);
            if ($translations) {
                $this->removeDisabled($translations);
                $translations->mergeWith($loadedTranslations);
            } else {
                $translations = $loadedTranslations;
            }
        }
    }

    /**
     * @return WarningGettextTranslator
     */
    public function loadTranslations($language)
    {
        global $cfg;

        $translator = new WarningGettextTranslator();

        // TODO: Synfony service
        $pathFinder = PathFinder::getService();
        $bundleFinder = ServiceContainer::getService(BundleFinder::class);

        /** @var \Gettext\Translations $translations */
        $translations = null;

        // Load local user translations from database
        $path = $pathFinder->tmpPath($language.'_db.po');
        $this->mergeTranslations($translations, $path);

        // Load local translations from shop folder
        $path = $pathFinder->shopPath("lang/{$language}.po");
        $this->mergeTranslations($translations, $path);

        // Load local translations named like shared from shop folder
        $templates = $cfg['Path']['smarty_tpl']['theme'];
        $file = "lang/{$language}.{$templates}.po";
        $path = $pathFinder->shopPath($file);
        $this->mergeTranslations($translations, $path);

        // Load translations from inside of bundles
        foreach ($bundleFinder->getExistingBundlesPath('Resources/lang/') as $bundlePath) {
            $this->mergeTranslations($translations, $bundlePath.$language.'.po');
        }

        // Load shared translations from engine template folder
        $path = $pathFinder->engineWebPath($file);
        $this->mergeTranslations($translations, $path);

        // Load shared translations from engine common folder
        $path = $pathFinder->engineWebPath("lang/{$language}.po");
        $this->mergeTranslations($translations, $path);

        if ($translations) {
            $translator->loadTranslations($translations);
        }

        return $translator;
    }

    /**
     * @param \Gettext\Translations $translations
     */
    protected function removeDisabled($translations)
    {
        $it = $translations->getIterator();
        for ($it->rewind(); $it->valid();) {
            if ($it->current()->isDisabled()) {
                $it->offsetUnset($it->key());
            } else {
                $it->next();
            }
        }
    }
}
