<?php

namespace Lc\ShopBundle\Services;

use Cocur\Slugify\Slugify;
use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\PageInterface;
use Lc\ShopBundle\Context\TaxRateInterface;
use Lc\ShopBundle\Context\UnitInterface;

class Utils
{
        protected $em ;
        protected $globalParam ;

        /*public function getUnitsListFormatedForType()
        {
                $choices = [] ;
                foreach ($this->unitsArray as $unit=>$unitInfo){
                        $choices[$unitInfo['wording_unit']] = $unit;
                };
                return $choices;
        }

        public function getUnitsList()
        {
                return $this->unitsArray;
        }

        public function getUnitProperty($unit, $property)
        {
                $unitsArray = $this->getUnitsList() ;

                if(isset($unitsArray[$unit][$property])) {
                        return $unitsArray[$unit][$property] ;
                }

                return false ;
        }



        public function getUnitWording($unit, $type = 'wording_short')
        {
                return $this->getUnitProperty($unit, $type) ;
        }

        public function getUnitRef($unit)
        {
                return $this->getUnitProperty($unit, 'ref') ;
        }

        public function getUnitCoefficient($unit)
        {
                return $this->getUnitProperty($unit, 'coefficient') ;
        }*/


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

        public function getUnitsList()
        {
                $unitsList =array();
                $units = $this->em->getRepository(UnitInterface::class)->findAll();
                foreach ($units as $unit){
                        $unitsList[$unit->getId()]['unit'] = $unit->getUnit();
                        $unitsList[$unit->getId()]['wordingUnit'] = $unit->getWordingUnit();
                        $unitsList[$unit->getId()]['wording'] = $unit->getWording();
                        $unitsList[$unit->getId()]['wordingShort'] = $unit->getWordingShort();
                        $unitsList[$unit->getId()]['coefficient'] = $unit->getCoefficient();
                        $unitsList[$unit->getId()]['unitReference'] = $unit->getUnitReference()->getId();
                }

                return $unitsList;
        }

        public function getTaxRatesList()
        {
                $taxRatesList =array();
                $taxRates = $this->em->getRepository(TaxRateInterface::class)->findAll();
                foreach ($taxRates as $taxRate){
                        $taxRatesList[$taxRate->getId()]['title'] = $taxRate->getTitle();
                        $taxRatesList[$taxRate->getId()]['value'] = $taxRate->getValue();
                }

                $taxRatesList['default']['title'] = $this->globalParam->getCurrentMerchantTaxRate()->getTitle();
                $taxRatesList['default']['value'] = $this->globalParam->getCurrentMerchantTaxRate()->getValue();
                return $taxRatesList;
        }

        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']) ;
        }

        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 slugify($string)
        {
                $slugify = new Slugify();
                return $slugify->slugify($string) ;
        }

}