<?php

namespace OfficeBrain\Bundle\CmsBundle\Manager\AbstractManager;

abstract class CmsSearchCriteriaAbstract
{
    protected $entityManager;
    protected $request;
    protected $projectSetting;
    protected $paginatorManager;
    protected $recordsPerPage;
    protected $countries;
    protected $languages;
    protected $countryCode;
    protected $languageCode;
    protected $countryId;
    protected $languageId;
    protected $cmsSearchCriteriaRepository;
    protected $cmsSearchCriteriaLanguageRepository;
    protected $translatorManager;

    public function __construct($entityManager, $request, $paginatorManager, $translatorManager, $countries, $languages)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->projectSetting = $this->request->get('project_setting');
        $this->paginatorManager = $paginatorManager;
        $this->translatorManager = $translatorManager;
        $this->recordsPerPage = $this->projectSetting['configuration_rule']['site_management']['option']['cms']['option']['cms_search_criteria_record_per_page']['status'];
        $this->countries = $countries;
        $this->languages = $languages;
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->localeLanguage = $this->projectSetting['locale_language'];
        
        if (count(explode('_', $this->localeLanguage)) == 1) {
        	$this->localeLanguage = $this->projectSetting['culture_code'];
        }
        
        $this->getCultureValue();
        
        if ($this->request->get('instance_id') > 0) {
            $this->projectSetting['instance_id'] = $this->request->get('instance_id');
        }

        $this->countryId = $this->getCountryIdByCode($this->countryCode);
        $this->languageId = $this->getLanguageIdByCode($this->languageCode);

        $this->cmsSearchCriteriaRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:CmsSearchCriteria');
        $this->cmsSearchCriteriaLanguageRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:CmsSearchCriteriaLanguage');

        $this->cmsSearchCriteriaRepository->setProjectSetting($this->projectSetting);
    }

    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->projectSetting);
        unset($this->countries);
        unset($this->languages);
        unset($this->cmsSearchCriteriaRepository);
        unset($this->cmsSearchCriteriaLanguageRepository);
        unset($this->countryId);
        unset($this->languageId);
        unset($this->countryCode);
        unset($this->languageCode);
        unset($this->translatorManager);
    }

    /**
     * @author Employee Id: 4589
     * Description : Seprate culture with language and country.
     *
     * @param string $culture
     *
     * @return array
     */
    public function getCultureValue()
    {
        $cultures = array();

        if ($this->localeLanguage && $this->localeLanguage != '' && (int) $this->localeLanguage == 0) {
            list($this->languageCode, $this->countryCode) = explode('_', $this->localeLanguage);
        }

        return $cultures;
    }

    /**
     * @author Employee Id: 4589
     * Description : Get country id from country code
     *
     * @param 1 : string $localeLanguage
     *
     * @return int
     * */
    public function getCountryIdByCode($localeLanguage)
    {
        $countries = $this->countries;
        foreach ($countries as $key => $country) {
            if ($localeLanguage == $key) {
                return $country['id'];
            }
        }
    }

    /**
     * @author Employee Id: 4589
     * Description : Get language id from language code
     *
     * @param 1 : string $languageCode
     *
     * @return int
     * */
    public function getLanguageIdByCode($languageCode)
    {
        $languages = $this->languages;
        foreach ($languages as $key => $lan) {
            if ($languageCode == $key) {
                return $lan['id'];
            }
        }
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to get field value from country parameter
     *
     * @param 1 : Culture
     * @param 2 : Search Field
     * @param 3 : Return Field
     *
     * @return : String
     *
     * @throws Exception : Null
     **/
    public function getCountryField($culture, $searchField, $returnField)
    {
        foreach ($this->countries as $key => $country) {
            if ($culture == $country[$searchField]) {
                if ($returnField != 'code') {
                    return $country[$returnField];
                } else {
                    return $key;
                }
            }
        }
    }

    public function setSearchCriteria($searchCriteria)
    {
    }
    public function getSearchCriteriaList()
    {
    }

    public function getSearchCriteriaById()
    {
    }
    
    public function formatAttribute($attributes)
    {
    	$k = 0;
        $attributeJson = array();
        foreach ($attributes as $attribute) {
            if (trim($attribute['value']) != '') {
            	if($attribute['name'] == 'category_filter[]') {
            		$attributeJson[$attribute['name']][$k++] = $attribute['value'];
            	}
            	else {
            		$attributeJson[$attribute['name']] = $attribute['value'];
            	}
            }
        }
        unset($k);
        return $attributeJson;
    }
}
