<?php

namespace OfficeBrain\Bundle\CmsBundle\Manager\AbstractManager;

abstract class CmsMenuAbstract
{
    protected $entityManager;
    protected $request;
    protected $projectSettings;
    protected $cmsPageManager;
    protected $routerManager;
    protected $countries;
    protected $languages;
    protected $cmsMenuMediaUploadLocation;
    protected $userType;
    public $cmsMenuMasterRepository;
    protected $cmsMenuItemRepository;
    protected $cmsMenuItemLanguageRepository;
    protected $cmsMenuRightsRepository;
    protected $countryCode;
    protected $languageCode;
    protected $countryId;
    protected $languageId;
    protected $categoryApiManager;
    protected $productTagManager;
    protected $searchCriteriaManager;

    public function __construct($entityManager, $request, $cmsPageManager, $routerManager, $categoryApiManager, $productTagManager, $searchCriteriaManager, $countries, $languages, $cmsMenuMediaUploadLocation, $userType)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->projectSetting = $this->request->get('project_setting');
        $this->cmsPageManager = $cmsPageManager;
        $this->routerManager = $routerManager;
        $this->countries = $countries;
        $this->languages = $languages;
        $this->cmsMenuMediaUploadLocation = $cmsMenuMediaUploadLocation;
        $this->userType = $userType;
        
        if (!isset($this->userType['public'])) {
        	$this->userType['public'] = 'Public';
        }

        if (!isset($this->userType['admin'])) {
        	$this->userType['admin'] = 'Admin';
        }

        $this->cultureId = $this->projectSetting['culture_id'];
        $this->localeLanguage = $this->projectSetting['locale_language'];

        if (count(explode('_', $this->localeLanguage)) == 1) {
        	$this->localeLanguage = $this->projectSetting['culture_code'];
        }
        
        if ($this->request->get('instance_id') > 0) {
            $this->projectSetting['instance_id'] = $this->request->get('instance_id');
        }

        $this->getCultureValue();

        $this->countryId = $this->request->get('countryId') > 0 ? $this->request->get('countryId') : $this->getCountryIdByCode($this->countryCode);
        $this->languageId = $this->getLanguageIdByCode($this->languageCode);

        $this->cmsMenuMasterRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:CmsMenuMaster');
        $this->cmsMenuItemRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:CmsMenuItem');
        $this->cmsMenuItemLanguageRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:CmsMenuItemLanguage');
        $this->cmsMenuRightsRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:CmsMenuRights');

        $this->cmsMenuMasterRepository->setProjectSetting($this->projectSetting);
        $this->cmsMenuItemRepository->setProjectSetting($this->projectSetting);
        $this->cmsMenuItemLanguageRepository->setProjectSetting($this->projectSetting);
        $this->cmsMenuRightsRepository->setProjectSetting($this->projectSetting);
        
        
        $this->categoryApiManager = $categoryApiManager;
        $this->productTagManager = $productTagManager;
        $this->searchCriteriaManager = $searchCriteriaManager;
    }

    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->projectSetting);
        unset($this->routerManager);
        unset($this->cmsPageManager);
        unset($this->countries);
        unset($this->languages);
        unset($this->cmsMenuMasterRepository);
        unset($this->countryId);
        unset($this->languageId);
        unset($this->countryCode);
        unset($this->languageCode);
        
        unset($this->categoryApiManager);
        unset($this->productTagManager);
        unset($this->searchCriteriaManager);
    }

    /**
     * @author Employee Id: 4494
     * 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: 4494
     * 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 == $country['short_name']) {
                return $country['id'];
            }
        }
    }

    public function getCountryCodeById($countryId = null)
    {
        $countries = $this->countries;
        
        if (empty($countryId)) {
            $countryId = $this->countryId;
        }
        
        foreach ($countries as $key => $country) {
            if ($countryId == $country['id']) {
                return $country['short_name'];
            }
        }
    }

    /**
     * @author Employee Id: 4494
     * 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 getLanguageCodeById($languageId)
    {
        $languages = $this->languages;
        foreach ($languages as $key => $lan) {
            if ($languageId == $lan['id']) {
                return $key;
            }
        }
    }

    public function getLocateByLanguageId($languageId)
    {
        return $this->getLanguageCodeById($languageId).'_'.$this->getCountryCodeById();
    }

    public function getMenu()
    {
    }

    public function getMenuBySlug($visibilityFilter = false)
    {
    }

    public function getMenuList()
    {
    }

    public function prepareMenu($menuSideBar = true, $menuList = true, $buildTree = false, $visibilityFilter = false)
    {
    }

    public function getMenuSideBar()
    {
    }

    public function setMenuItem()
    {
    }

    public function deleteMenuItem($menuItemId = 0, $prepareMenuReturn = true)
    {
    }

    public function saveMenuItem()
    {
    }

    public function deleteChildMenuItem($parentId)
    {
    }

    public function buildTree($elements, $parentId = 0)
    {
    }

    public function uploadMenuItem()
    {
    }
    
    public function setCmsPageFromStaticPage()
    {
    }
    
    public function create($slug, $menuItems, $instanceId)
    {
    }
}
