<?php

namespace OfficeBrain\Bundle\SubShopBundle\Manager\AbstractManager;

/**
 * @author OfficeBrain 4494 <info@officebrain.com>
 *
 * Description : Sub shop Abstract class
 **/
abstract class SubShopAbstract
{
    public $entityManager;
    public $repository;
    public $request;
    public $projectSetting;
    public $subShopThemeManager;
    public $subShopInquiryManager;
    public $userManager;
    public $emailManager;
    public $recordsPerPage;

    public function __construct($entityManager, $request, $subShopThemeManager, $subShopInquiryManager, $userManager, $emailManager, $searchManager, $formManager, $countries, $languages, $paginatorManager)
    {
        $this->entityManager = $entityManager;
        $this->repository = $this->entityManager->getRepository('OfficeBrainSubShopBundle:SubShop');
        $this->request = $request;
        $this->projectSetting = $this->request->get('project_setting');
        $this->subShopThemeManager = $subShopThemeManager;
        $this->subShopInquiryManager = $subShopInquiryManager;
        $this->userManager = $userManager;
        $this->paginatorManager = $paginatorManager;
        $this->emailManager = $emailManager;
        $this->searchManager = $searchManager;
        $this->formManager = $formManager;
        $this->countries = $countries;
        $this->languages = $languages;

        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $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->recordsPerPage = $this->projectSetting['configuration_rule']['site_management']['option']['sub_shop']['option']['sub_shop_record_per_page']['status'];
        $this->repository->setProjectSetting($this->projectSetting);
        $this->repository->setPaginatorService($this->paginatorManager);
    }

    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->repository);
        unset($this->request);
        unset($this->projectSetting);
        unset($this->subShopThemeManager);
        unset($this->subShopInquiryManager);
        unset($this->paginatorManager);
        unset($this->emailManager);
        unset($this->recordsPerPage);
        unset($this->knpPaginator);
    }

    /**
     * @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 $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: 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 setSubShop($subShop, $setSubShopThemeDesign = false, $inquiryId = 0)
    {
    }

    public function getSubShop()
    {
    }

    public function getSubShopList()
    {
    }

    public function deleteSubShop()
    {
    }

    public function revertSubShop()
    {
    }

    public function approveSubShop()
    {
    }

    public function rejectSubShop()
    {
    }

    public function onHoldSubShop()
    {
    }

    public function searchByStatusSubShop()
    {
    }

    public function searchByDeleteSubShop()
    {
    }

    public function searchByParentIdSubShop($id = 0)
    {
    }

    public function getSecurityToken($length = 64)
    {
        return $this->userManager->getSecurityToken($length);
    }
    
    public function checkDuplicateShopDomain($subShop, $setSubShopThemeDesign = false, $inquiryId = 0)
    {
    }
    
    public function getSubShopByToken()
    {
    }
    
    public function getSubShopListByInstance()
    {
    }
    
    public function searchSubShop()
    {
    }
    public function sendApprovedAcknowledgement($subShop)
    {
    }
    
    public function setSubShopDesignTheme($instanceType = 'sub_shop')
    {
    }
    
    public function prepareInstance($instanceId = 0)
    {
    }
    
    public function updateSubShopStep($step, $value, $instanceId = 0)
    {
    }
    
    public function getSubShopStep()
    {
    }
    
    public function updateSubShopDesign()
    {
    }
    
    public function publishSubShopDesign()
    {
    }
    
    public function getSubShopCms()
    {
    }
    
    public function updateSubShopCriteria($environment)
    {
    }
    
    public function getSubShopCriteria()
    {
    }
    
    public function getSubShopProductSolrStatus()
    {
    }
    
    public function updateSubShopSolrProducts()
    {
    }
    
    public function getSearchQuery($params)
    {
    }
    
    public function getSubShopPromotionalProductsList()
    {
    }
    
    public function getSubShopPromotionalProductsListAjax()
    {
    }
    
    public function updateSubShopPromotionalProducts()
    {
    }
    
    public function getSubShopBrands()
    {
    }
    
    public function getSubShopBrandsAjax($brandApiManager,$paginator)
    {
    }
    
    public function updateSubShopBrands()
    {
    }
    
    public function frontGetBrands($brandApiManager,$locale,$instanceId)
    {
    }
    
    public function frontGetPromotionalProducts($productDetailManager,$locale,$countryId,$instanceId,$type)
    {
    }
    
    public function getStateWiseDistributorsSite($page,$recordPerPage,$zipcode='')
    {
    }

	public function getPostFixDomain()
    {
        /*
        preg_match("/[^\.\/]+\.[^\.\/]+$/", (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && $_SERVER['HTTP_X_FORWARDED_HOST'] != '') ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $this->request->getHost(), $matches);
    	if (isset($matches[0])) {
    		return $matches[0];
    	}
    	else {
    		return $this->request->getHost();
    	}*/
    	$host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && $_SERVER['HTTP_X_FORWARDED_HOST'] != '') ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $this->request->getHost();
    	$host = preg_replace('/^(www\.)/i', '', $host);
		
        	return $host;
    }
    
    public function allowStepAccessConfiguration($subShop = array())
    {
    	
    }
}
