<?php

/**
 * Created by PhpStorm.
 * User: ondra
 * Date: 8.1.18
 * Time: 15:22.
 */

namespace KupShop\I18nBundle\Translations;

use KupShop\ContentBundle\Util\MenuUtil;
use KupShop\I18nBundle\Exception\TranslationNotUniqueException;
use KupShop\I18nBundle\Util\TranslationCustomDataTrait;
use KupShop\KupShopBundle\Util\System\UniqueUrlUtil;
use Query\Operator;
use Query\QueryBuilder;
use Symfony\Contracts\Service\Attribute\Required;

class MenuLinksTranslation extends BaseTranslation implements TranslationCustomDataInterface
{
    use TranslationCustomDataTrait;

    protected $columns = [
        'name' => ['alias' => 'Jméno', 'maxlength' => 100],
        'name_short' => ['alias' => 'Krátký název', 'maxlength' => 50],
        'link' => ['alias' => 'Odkaz', 'maxlength' => 255],
        'url' => ['alias' => 'URL', 'maxlength' => 255],
        'figure' => ['alias' => 'Viditelné', 'select' => [null => 'Nenastavovat', 'Y' => 'Ano', 'N' => 'Ne']],
        'meta_title' => ['alias' => 'SEO název (title)', 'maxlength' => 70],
        'meta_description' => ['alias' => 'SEO popis (description)', 'maxlength' => 250],
        'meta_keywords' => ['alias' => 'Klíčová slova (keywords)', 'maxlength' => 100],
    ];

    protected $tableName = 'menu_links';

    protected $nameColumn = 'name';

    protected $tableAlias = 'ml';

    public bool $supportsHideNotTranslated = true;

    #[Required]
    public UniqueUrlUtil $uniqueUrlUtil;

    public function saveSingleObject(string $languageID, $objectID, array $values, ?string $sourceLanguageID = null, $clearCache = true): bool
    {
        if ($url = $values['url'] ?? false) {
            if ($uniqueTxt = $this->uniqueUrlUtil->validateUniqueUrl(UniqueUrlUtil::MENULINKS, $objectID, $url, $languageID)) {
                throw new TranslationNotUniqueException($uniqueTxt);
            }
        }

        $return = parent::saveSingleObject($languageID, $objectID, $values);

        if ($clearCache) {
            $this->clearCache($languageID);
        }

        return $return;
    }

    public function getJoinType(): string
    {
        // vzducky vracime leftJoin, protoze menu_links nejsme schopni spravne ofiltrovat jen pomoci zmeny typu joinu
        // kvuli skupinam (TYPE_GROUP), ktere se neprekladaji
        return 'leftJoin';
    }

    public function customizeJoinQueryBuilder(QueryBuilder $qb, string $langId): void
    {
        if (!$this->supportsHideNotTranslated) {
            return;
        }

        $dbcfg = \Settings::getDefault();
        if (($dbcfg->hide_not_translated_objects[$this->getTableName()] ?? null) == 'Y') {
            $translationTableAlias = $this->getTableAlias().'_'.$langId;
            // odfiltruju pryc neprelozene polozky, ale polozky, ktere jsou TYPE_GROUP necham
            // protoze ty se neprekladaji
            $qb->andWhere(
                Operator::orX(
                    Operator::equals(['ml.type' => MenuUtil::TYPE_GROUP]),
                    Operator::andX(
                        Operator::not(
                            Operator::equals(['ml.type' => MenuUtil::TYPE_GROUP]),
                        ),
                        "{$translationTableAlias}.id IS NOT NULL"
                    )
                )
            );
        }
    }

    protected function postprocessValues(array $values): array
    {
        if (isset($values['url'])) {
            $values['url'] = ltrim($values['url'], '/');
        }

        return $values;
    }

    public function clearCache($languageID = ''): bool
    {
        clearCache('menu-'.$languageID);

        return true;
    }
}
