<?php

namespace KupShop\OrderDiscountBundle\Resources\upgrade;

use KupShop\I18nBundle\Translations\SettingsTranslation;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class MarketingUpgrade extends \UpgradeNew
{
    public function check_GoogleVerificationTransformed()
    {
        return $this->checkDataMigration(10);
    }

    /** Migrate google verification codes */
    public function upgrade_GoogleVerificationTransformed()
    {
        $dbcfg = \Settings::getDefault();
        $value = $dbcfg->loadValue('analytics');
        if (!$value) {
            $value = [];
        }
        $transformedCodes = [];
        if (isset($value['google_site_verification'])) {
            $explodedCode = explode(';', $value['google_site_verification']['id']);
            foreach ($explodedCode as $code) {
                if ($code != '') {
                    $transformedCodes[] = ['value' => $code, 'note' => ''];
                }
            }
            // TODO: Odmazat nepotrebny settingy - google_site_verification;
        }

        foreach ($value['google_site_verifications'] ?? [] as $item) {
            if ($item['value'] != '') {
                $transformedCodes[] = $item;
            }
        }

        $value['google_site_verifications'] = $transformedCodes;

        $dbcfg->saveValue('analytics', $value);

        \Settings::clearCache();

        $this->commitDataMigration(10);

        $this->upgradeOK();
    }

    public function check_GoogleVerificationTransformed2()
    {
        return $this->checkDataMigration(11);
    }

    /** Migrate google verification codes */
    public function upgrade_GoogleVerificationTransformed2()
    {
        if (findModule(\Modules::CURRENCIES)) {
            $languageContext = ServiceContainer::getService(LanguageContext::class);
            $settingsTranslations = ServiceContainer::getService(SettingsTranslation::class);

            foreach ($languageContext->getSupported() as $language) {
                if ($language->getId() != $languageContext->getDefaultId()) {
                    $data = $settingsTranslations->setLanguageId($language->getId())->get();

                    if (!isset($data['analytics'])) {
                        continue;
                    }

                    $transformedCodes = [];
                    $value = $data['analytics'];

                    if (isset($value['google_site_verification'])) {
                        $explodedCode = explode(';', $value['google_site_verification']['id']);
                        foreach ($explodedCode as $code) {
                            if ($code != '') {
                                $transformedCodes[] = ['value' => $code, 'note' => ''];
                            }
                        }
                        // TODO: Odmazat nepotrebny settingy - google_site_verification;
                    }

                    // Do not save empty array
                    if (!$transformedCodes) {
                        continue;
                    }

                    $value['google_site_verifications'] = $transformedCodes;

                    $settingsTranslations->save(['analytics' => $value]);
                }
            }
        }

        \Settings::clearCache();

        $this->commitDataMigration(11);

        $this->upgradeOK();
    }
}
