<?php

namespace OfficeBrain\Bundle\EcatalogueBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\Bundle\EcatalogueBundle\Form\EcatalogueOrderType;
use OfficeBrain\Bundle\EcatalogueBundle\Entity\EcatalogueOrder;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;



/**
 *
 * @author 4488
 * @Route("{_locale}/")
 */
class EcatalogueController extends Controller
{
	/**
	 * @author Employee Id: 4488
	 * set Default Value
	 */
	
	public function __construct()
	{
		$this->entityManager=null;
		$this->ecatalogueManager=null;
		$this->coreManager=null;
		$this->request=null;
		$this->responseArray['status']=null;
		$this->projectSetting=null;
		$this->per_page=null;
		$this->translator = null;
		$this->session= null;
		$this->ecatalogueSupplierId= null;
	}
	public function initAction()
	{
		$this->request=$this->getRequest();
		$this->session= $this->request->getSession();
		$this->entityManager = $this->getDoctrine()->getEntityManager();
		$this->location=$this->get('kernel')->getRootDir().'/../web/images/uploads/';
		$this->translator = $this->get('translator');
		$this->projectSetting=$this->request->get('project_setting');
		$this->projectUserSetting = $this->request->get('project_user_setting');
		
		$this->ecatalogueManager=$this->get("office_brain_ecatalogue_manager");
		$this->categoryService = $this->get('office_brain_search_manager');
		$this->ecatalogue_per_page=$this->projectSetting['configuration_rule']['promo_discount']['option']['ecatalogue']['no_of_record_per_page'];
		$this->ecatalogueConfigurationField=$this->projectSetting['configuration_rule']['promo_discount']['option']['ecatalogue'];
		$this->ecatalogueAccessField= $this->projectSetting['access_rule']['promo_discount']['option']['ecatalogue'];
		$this->responseArray['ecatalogueAccessField'] = $this->ecatalogueAccessField;
		$this->responseArray['ecatalogueConfigurationField']=$this->ecatalogueConfigurationField;
		$this->userId = $this->projectSetting['user_id'];
		$this->ecatalogueSupplierId=$this->request->get('ecatalogueSupplierId');
		$this->ecatalogueCategoryId=$this->request->get('ecatalogueCategoryId');
		$this->signUpManager = $this->get('office_brain_user_sign_up_manager');
		$this->userManager = $this->get('office_brain_user_manager');
	}
	/**
	 * @author Employee Id: 4488 -
	 * OB eCommerce Product -  Front Ecatalogue List
	 *
	 * @Route("ecatalog/list",
	 * 	name="office_brain_ecataloguebundle_ecatalogue_frontend_list",
	 *  options={"expose"=true}
	 * )
	 * @Template("OfficeBrainEcatalogueBundle:Ecatalogue:ecatalogueList.html.twig")
	
	 */
	public function frontEndEcatalogueListAction()
	{
		$this->initAction();
		$usertype=$this->userId>0?'registered_user':'guest_user';
		switch ($this->ecatalogueConfigurationField[$usertype]['status'])
		{
			case '1':
				$this->responseArray['categoryData']=$this->getAllCategory();
				$this->responseArray['supplierData']=$this->ecatalogueManager->getMappingEcatalogueSupplier($this->userManager);
				$this->responseArray['recordSet']=$this->ecatalogueManager->getActiveEcatalogueList($this->userId);
				return $this->responseArray;
				break;
			case '0':
				throw new AuthenticationCredentialsNotFoundException();
// 				return $this->redirect($this->generateUrl('office_brain_userbundle_login_login'));
				break;
		}
	}
	/**
	 * @author Employee Id: 5058 -
	 * OB eCommerce Product -  home page Ecatalogue section latest five List
	 *
	 * @Route("ecatalog/latest_five_list",
	 * 	name="office_brain_ecataloguebundle_ecatalogue_latestfivelist",
	 *  options={"expose"=true}
	 * )
	 * @Template("OfficeBrainEcatalogueBundle:Ecatalogue:ecatalogueLatestFiveList.html.twig")
	
	 */
	public function frontEndEcatalogueLatestFiveListAction()
	{
		$this->initAction();
		$this->responseArray['recordSet']=$this->ecatalogueManager->getActiveLatestFiveEcatalogueList();
		return $this->responseArray;
	}
	/**
	 * @author Employee Id: 4488 -
	 * OB eCommerce Product -  Front Ecatalogue List
	 *
	 * @Route("ecatalog/supplier/list/{ecatalogueSupplierId}",
	 * 	name="office_brain_ecataloguebundle_ecatalogue_frontend_ecataloguesupplierlist",
	 *  options={"expose"=true}
	 * )
	 * @Template("OfficeBrainEcatalogueBundle:Ecatalogue:ecatalogueRow.html.twig")
	
	 */
	public function ecatalogueSupplierListAction()
	{
		$this->initAction();
		$this->responseArray['recordSet']=$this->ecatalogueManager->getSupplierEcatalogueList($this->userId,$this->ecatalogueSupplierId);
		return $this->responseArray;
	}
	/**
	 * @author Employee Id: 4488 -
	 * OB eCommerce Product -  Front Ecatalogue List
	 *
	 * @Route("ecatalog/category/list/{ecatalogueCategoryId}",
	 * 	name="office_brain_ecataloguebundle_ecatalogue_frontend_ecataloguecategorylist",
	 *  options={"expose"=true}
	 * )
	 * @Template("OfficeBrainEcatalogueBundle:Ecatalogue:ecatalogueRow.html.twig")
	
	 */
	public function ecatalogueCategoryListAction()
	{
		$this->initAction();
		$this->responseArray['recordSet']=$this->ecatalogueManager->getCategoryEcatalogueList($this->userId,$this->ecatalogueCategoryId);
		return $this->responseArray;
	}
	public  function getAllCategory()
	{
		$this->finalArr = array();
		$this->finalMapping=array();
		$categoryListData=$this->categoryService->getCategorySubCategoryList();
		if(count($categoryListData))
		{
			$this->getAllCategoryInOneLevel($categoryListData);
		}
		$categoryMapping=$this->ecatalogueManager->getMappingEcatalogueCategory();
		foreach ($categoryMapping as $k=>$v)
		{
			foreach ($this->finalArr as $key=>$value)
			{
				if($v == $key)
				{
					$this->finalMapping[$key]=$value;
				} 
			}
			
		}
		return $this->finalMapping;
	}
	private function getAllCategoryInOneLevel($categoryListArray)
	{
				foreach ($categoryListArray as $key => $val) {
					if (isset($val['child'])) {
						$this->finalArr[$val['pms_id']] = $val['cat_name'];
						$this->getAllCategoryInOneLevel($val['child']);
					} else {
						$this->finalArr[$val['pms_id']] = $val['cat_name'];
					}
				}
		return true;
	}
}
