<?php

namespace KupShop\ComponentsBundle\Twig;

use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Translation\TranslatorInterface;

trait ProducerTooltipTrait
{
    protected TranslatorInterface $translator;

    private function getFormattedAddress(array $producer, string $prefix = ''): string
    {
        if (!empty($prefix)) {
            $prefix .= '_';
        }

        return $producer[$prefix.'company_street'].', '.$producer[$prefix.'company_zip'].' '.$producer[$prefix.'company_city'].', '.$producer[$prefix.'company_country'];
    }

    public function getProducerTooltip(array $producer): string
    {
        $content = [];

        if ($producer['company_name']) {
            $content[] = $this->translator->trans('product.producer', [], 'products');
            $content[] = $producer['company_name'];
            $content[] = $this->getFormattedAddress($producer);
            $content[] = $producer['company_email'];
            $content[] = $producer['company_web'];
        }

        if ($producer['import_company_name'] && $producer['company_name']) {
            $content[] = '<br>';
        }

        if ($producer['import_company_name']) {
            $content[] = $this->translator->trans('product.importer', [], 'products');
            $content[] = $producer['import_company_name'];
            $content[] = $this->getFormattedAddress($producer, 'import');
            $content[] = $producer['import_company_email'];
            $content[] = $producer['import_company_web'];
        }

        if (!empty($content)) {
            return '<p class="wpj-tooltip-producer">'.implode('<br>', $content).'</p>';
        }

        return '';
    }

    #[Required]
    final public function setTranslator(TranslatorInterface $translator): void
    {
        $this->translator = $translator;
    }
}
