<?php

namespace OfficeBrain\Bundle\CmsBundle\Manager\AbstractManager;

abstract class CmsPageAbstract
{
    protected $entityManager;
    protected $request;
    protected $projectSetting;
    protected $paginatorManager;
    protected $recordsPerPage;
    protected $countries;
    protected $languages;
    protected $countryCode;
    protected $languageCode;
    protected $countryId;
    protected $languageId;

    public function __construct($entityManager, $request, $paginatorManager, $countries, $languages)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->projectSetting = $this->request->get('project_setting');
        $this->paginatorManager = $paginatorManager;
        $this->recordsPerPage = $this->projectSetting['configuration_rule']['site_management']['option']['cms']['option']['cms_page_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->cmsPageMasterRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:CmsPageMaster');
        $this->cmsPageLanguageRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:CmsPageLanguage');

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

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

    /**
     * @author Employee Id: 4795
     * 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: 4795
     * Description : Get country id from country code
     *
     * @param 1 : string $cultureCode
     *
     * @return int
     * */
    public function getCountryIdByCode($cultureCode)
    {
        $countries = $this->countries;
        foreach ($countries as $key => $country) {
            if ($cultureCode == $country['short_name']) {
                return $country['id'];
            }
        }
    }

    /**
     * @author Employee Id: 4795
     * 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'];
            }
        }
    }

    public function getBlankObject()
    {
    }

    public function setPage($page, $id = null)
    {
    }
    public function getPageList()
    {
    }

    public function getPageById($id)
    {
    }
}
