<?php
/**
 * Factory for Language Tag Set
 *
 * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
 */
namespace VisualM\TagManagerBundle\Model\TagSet;

use Exception;

use Symfony\Component\Translation\TranslatorInterface;

use VisualM\TagManagerBundle\Form\Type\LanguageTagSetType;

/**
 * Factory for Language Tag Set
 *
 * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
 */
class LanguageTagSetFactory implements MetaTagSetFactoryInterface
{
    protected $translator;

    /**
     * Constructor
     *
     * @param TranslatorInterface $translator
     */
    function __construct(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

    /**
     * {@inheritdoc}
     */
    public function getFormType()
    {
        return LanguageTagSetType::class;
    }

    /**
     * {@inheritdoc}
     */
    public function getFormView()
    {
       return 'VisualMTagManagerBundle:ManageMetaTagSet:language_sub_form.html.twig';
    }

    /**
     * {@inheritdoc}
     */
    public function getTitle()
    {
        return $this->translator->trans('tags.self_referencing', [], 'backend');
    }

    /**
     * {@inheritdoc}
     */
    public function processMetaTag($meta_tag)
    {
        $inhoud = $meta_tag->getInhoud();
        $metatag_preset = $meta_tag->getMetaTagPreset();
        $metatag_set = $metatag_preset->getMetaTagSet();

        $hreflang_tags = array();

        $configuration = $metatag_set->getFactoryConfiguration();
        $self_referencing = ($configuration != null & isset($configuration['selfReferencing'])) ? ($configuration['selfReferencing'] ? true : false) : false;
        if ($self_referencing) {
            preg_match_all('/(\.[a-z]*)/', $_SERVER['SERVER_NAME'], $matches);
            $current_language = $matches[0][count($matches[0]) - 1];
            preg_match('/\w+/', $current_language, $matches);

            $current_language = $matches[0];
            $current_href = ($_SERVER['HTTPS'] == '1' ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

            $hreflang_tags[$current_language] = '<link rel="alternate" href="' . $current_href . '" hreflang="' . $current_language . '">';
        }

        $this->extractHrefLangTags($meta_tag, $hreflang_tags);

        $inhoud = '';
        foreach ($hreflang_tags as $hreflang_tag) {
            $inhoud .= $hreflang_tag . "\n";
        }
        $meta_tag->setInhoud($inhoud);

        return $meta_tag;
    }

    /**
     * Extract HrefLangTags
     *
     * @param MetaTag $meta_tag
     * @param Array|null $hreflang_tags
     *
     * @return Array
     */
    public function extractHrefLangTags($meta_tag, &$hreflang_tags = array())
    {
        $inhoud = $meta_tag->getInhoud();

        $explode_val = '<link';
        $separated = explode($explode_val, $inhoud);
        array_shift($separated);

        foreach ($separated as $key=>$link) {
            preg_match('/hreflang="([^"]+)"/', $link, $matches);
            $hreflang_tags[$matches[1]] = trim($explode_val . $link);
        }

        return $hreflang_tags;
    }
}
