<?php

namespace Lc\ShopBundle\Services;

use Cocur\Slugify\Slugify;
use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\PageInterface;
use Lc\ShopBundle\Context\TaxRateInterface;
use Lc\ShopBundle\Context\UnitInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\ParameterBag;

class Utils
{
        protected $em ;
        protected $parameterBag ;

        public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag)
        {
                $this->em = $em ;
                $this->parameterBag = $parameterBag ;
        }

        // @todo : À supprimer et passer dans DateUtils (gérer du coup le cas du modèle DeliverySlot qui dépend de cette fonction)
        public static function getDayByNumber($number, $lang = 'fr')
        {
                if($lang == 'fr') {
                        $daysArray = [
                                1 => 'Lundi',
                                2 => 'Mardi',
                                3 => 'Mercredi',
                                4 => 'Jeudi',
                                5 => 'Vendredi',
                                6 => 'Samedi',
                                7 => 'Dimanche'
                        ] ;
                }
                else {
                        $daysArray = [
                                1 => 'Monday',
                                2 => 'Tuesday',
                                3 => 'Wednesday',
                                4 => 'Thursday',
                                5 => 'Friday',
                                6 => 'Saturday',
                                7 => 'Sunday',
                        ] ;
                }

                if(isset($daysArray[$number])) {
                        return $daysArray[$number] ;
                }

                return '' ;
        }

        public function getElementByDevAlias($devAlias, $class = PageInterface::class)
        {
                $class = $this->em->getClassMetadata($class)->getName();
                return $this->em->getRepository($class)->findOneByDevAlias($devAlias) ;
        }


        public function isServerLocalhost()
        {
                return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) ;
        }

        public function getCookieDomain()
        {
                return ($this->isServerLocalhost()) ? null : $this->parameterBag->get('app.cookie_domain_distant') ;
        }

        function limitText($text, $limit) {
                if (str_word_count($text, 0) > $limit) {
                        $words = str_word_count($text, 2);
                        $pos = array_keys($words);
                        $text = substr($text, 0, $pos[$limit]) . '...';
                }
                return $text;
        }

        public function isBot()
        {
                if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
                        return TRUE;
                } else {
                        return FALSE;
                }
        }

        public function slugify($string)
        {
                $slugify = new Slugify();
                return $slugify->slugify($string) ;
        }

}
