<?php

namespace OfficeBrain\Bundle\SearchBundle\Manager\Extended;

use Symfony\Component\HttpFoundation\Request;
use OfficeBrain\Bundle\SearchBundle\Manager\AbstractManager\BrowseCategoryAbstract;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class BrowseCategoryExtended extends BrowseCategoryAbstract
{
	private $recordPerPage;	
	private $subCategory;
	private $categoryId;
	

    public function __construct($entityManager, $request, $categoryManager, $paginatorManager, $serviceContainer)
    {
    	parent::__construct($entityManager, $request, $categoryManager, $paginatorManager, $serviceContainer);
    	$this->projectSetting = $this->request->get('project_setting');
    	$this->recordPerPage = $this->projectSetting['configuration_rule']['search']['search_page']['record_per_page'];
    	$this->request = $request;
    	$this->serviceContainer = $serviceContainer;
    }
    
    
    /**
     * @author Employee Id: 4532
     * Description : This function is used for get All sub Category list of given category Id
     *
     * @param categoryId
     * 
     * @return array
     *
     * @throws Exception
     *
     **/
    
    public function getSubCategoryList($categoryId){
    	
    	$subCategoriesList = array();
    	$this->subCategory = array();
    	
    	$searchObj = $this->serviceContainer->get('office_brain_search_manager');
    	$categories = $searchObj->getCategorySubCategoryList();

    	$this->subCategoryList($categories,$categoryId);
    	
    	return $this->subCategory;
    	
    }
    
    /**
     * @author Employee Id: 4532
     * Description : This function is used for get All sub Category list of given category name and also parent category name and id
     *
     * @param categoryId, category list array
     *
     * @return array
     *
     * @throws Exception
     *
     **/
    
    private function subCategoryList($categories,$categoryId){
    	$isExist = false;
    	if(is_array($categories)&&count($categories)>0){
    	foreach ($categories as $cat){
    		if($cat['pms_id'] == $categoryId){
    			if(isset($cat['child']))
    			{
    				$this->subCategory['child'] =  $cat['child'];
    			}
    			$this->subCategory['name'] =  $cat['cat_name'];
    			$this->subCategory['slug'] =  $cat['slug'];
    			$this->subCategory['cat_name'] =  $cat['cat_name'];
    			$isExist =  $cat['parent_id'];
    			break; 		
    		}else{
    			if(isset($cat['child'])){
    				$isExist = $this->subCategoryList($cat['child'],$categoryId);
    				if($isExist == $cat['local_id'])
    				{
    					$this->subCategory['parent']= array('cat_name'=>$cat['cat_name'],'pms_id'=>$cat['pms_id'],'slug'=>$cat['slug']);
    				}
    			}
    		}
    	}
    	}
    	return $isExist;
    }
    
    /**
     * @author Employee Id: 4532
     * Description : This function is used for get category id by category slug
     *
     * @param slug
     *
     * @return categoryid
     *
     * @throws Exception
     *
     **/
    
    public function getCategoryNameFromSlug($slug){
    	 
    	$subCategoriesList = array();
    	 
    	$searchObj = $this->serviceContainer->get('office_brain_search_manager');
    	$categories = $searchObj->getCategorySubCategoryList();
    	return $this->getCategoryNameId($categories,$slug);
    }
    
    private function getCategoryNameId($categories,$slug){
        $slug = trim($slug);
        if(is_array($categories)&&count($categories)>0){
            foreach ($categories as $cat){
                if($cat['slug'] == $slug){
                   return $cat['cat_name'];
                }        
            }
        }
        
    }

    public function getCategoryIdFromSlug($slug){
         
        $subCategoriesList = array();
         
        $searchObj = $this->serviceContainer->get('office_brain_search_manager');
        $categories = $searchObj->getCategorySubCategoryList();
        $this->getCategoryId($categories,$slug);
        
        return $this->categoryId;
         
    }

    /**
     * @author Employee Id: 4532
     * Description : This function is used for get category id by category slug
     *
     * @param $categories,slug
     *
     * @return categoryid
     *
     * @throws Exception
     *
     **/
    
    private function getCategoryId($categories,$slug){
    	$slug = trim($slug);
    	if(is_array($categories)&&count($categories)>0){
	    	foreach ($categories as $cat){
	    		if($cat['slug'] == $slug){
	    			$this->categoryId =  $cat['pms_id'];
	    			return;
	    		}else{
	    			if(isset($cat['child'])){
	    				$this->getCategoryId($cat['child'],$slug);
	    			}
	    		}    		
	    	}
    	}
    	
    }
    
    /**
     * @author Employee Id: 4488
     * Description : This function is used for get All sub Category list of given category Id
     *
     * @param categoryId
     *
     * @return array
     *
     * @throws Exception
     *
     **/
    
    public function getCategoryNameFromId($categoryId){
    	 
    	$subCategoriesList = array();
    	$this->subCategory = array();
    	 
    	$searchObj = $this->serviceContainer->get('office_brain_search_manager');
    	$categories = $searchObj->getCategorySubCategoryList();
    
    	$this->getCategoryName($categories,$categoryId);
    	return $this->subCategory;
    	 
    }
    // 4488 get category name
    private function getCategoryName($categories,$categoryId){
    	$isExist = false;
    	foreach ($categories as $cat){
    		if (in_array($cat['pms_id'], $categoryId)) {
    			$this->subCategory[]=  $cat;
    			$isExist =  $cat['parent_id'];
    			
//     		}
//     		if($cat['pms_id'] == $categoryId){
//     			if(isset($cat['child']))
//     			{
//     				$this->subCategory['child'] =  $cat['child'];
//     			}
//     			$this->subCategory['cate_info'] =  $cat;
//     			$isExist =  $cat['parent_id'];
//     			break;
    		}else{
    			if(isset($cat['child'])){
    				$isExist = $this->subCategoryList($cat['child'],$categoryId);
    				if($isExist == $cat['local_id'])
    				{
    					$this->subCategory['parent']= array('cat_name'=>$cat['cat_name'],'pms_id'=>$cat['pms_id'],'slug'=>$cat['slug']);
    				}
    			}
    		}
    	}
    	return $isExist;
    }
}