<?php

namespace OfficeBrain\Bundle\ServiceGroupBundle\Manager\AbstractManager;

use OfficeBrain\Bundle\CoreBundle\Manager\AbstractManager\CoreAbstract;

/**
 * Description of serviceGroupBaseAbstract.
 *
 * @author software
 */
class ServiceGroupBaseAbstract
{
    protected $entityManager;
    protected $session;
    protected $request;

    protected $projectSetting;
    protected $configurationRule;

    protected $instanceId;
    protected $instanceType;

    protected $cultureId;
    protected $cultureCode;
    protected $countries;
    protected $languages;
    protected $countryId;
    protected $languageId;
    protected $countryCode;
    protected $languageCode;

    protected $userId;
    protected $createdUid;
    protected $upatedUid;

    protected $serviceContainer;
    protected $coreManager;

    public function __construct($entityManager, $request, $session, $serviceContainer, $countries, $languages, $coreManager)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->sessionManager = $session;
        $this->countries = $countries;
        $this->languages = $languages;
        $this->coreManager = $coreManager;

        $this->projectSetting = $this->request->get('project_setting');
        $this->configurationRule = $this->projectSetting['configuration_rule']['promo_discount'];

        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];

        $this->userId = $this->projectSetting['user_id'];
        $this->createdUid = $this->userId;
        $this->upatedUid = $this->userId;

        $this->paginatorService = $serviceContainer->get('knp_paginator');
        $this->serviceContainer = $serviceContainer;

        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];

        $this->culture = $this->request->get('culture', $this->cultureCode);

        $this->getCultureValue();

        $this->countryId = $this->getCountryIdByCode($this->countryCode);
        $this->languageId = $this->getLanguageIdByCode($this->languageCode);
        if(isset($this->projectSetting['instance_theme_meta']['product_category']['custom_label'])){
        	$this->categoryCustomlabel = $this->projectSetting['instance_theme_meta']['product_category']['custom_label'];
        }
    }

    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->sessionManager);

        unset($this->projectSetting);
        unset($this->configurationRule);

        unset($this->instanceId);
        unset($this->instanceType);

        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->countries);
        unset($this->languages);
        unset($this->countryId);
        unset($this->languageId);
        unset($this->countryCode);
        unset($this->languageCode);

        unset($this->userId);
        unset($this->createdUid);
        unset($this->upatedUid);

        unset($this->paginatorService);
        unset($this->coreManager);
    }

    public function getLanguageId()
    {
        return $this->languageId;
    }

    public function getCultureValue()
    {
        $cultures = array();

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

        return $cultures;
    }

    public function getCountryIdByCode($cultureCode)
    {
        $countries = $this->countries;
        foreach ($countries as $key => $country) {
            if ($cultureCode == $key) {
                return $country['id'];
            }
        }
    }

    public function getLanguageIdByCode($languageCode)
    {
        $languages = $this->languages;
        foreach ($languages as $key => $lan) {
            if ($languageCode == $key) {
                return $lan['id'];
            }
        }
    }

    public function getCountryField($culture, $searchField, $returnField)
    {
        $countries = $this->countries;
        foreach ($countries as $key => $country) {
            if ($culture == $country[$searchField]) {
                if ($returnField != 'code') {
                    return $country[$returnField];
                } else {
                    return $key;
                }
            }
        }
    }

    public function getLanguageField($language, $searchField, $returnField)
    {
        $languages = $this->languages;
        foreach ($languages as $key => $lan) {
            if ($language == $lan[$searchField]) {
                if ($returnField != 'code') {
                    return $lan[$returnField];
                } else {
                    return $key;
                }
            }
        }
    }
}
