<?php

namespace OfficeBrain\CustomBundle\FlyerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;
use OfficeBrain\Bundle\FlyerBundle\Controller\FlyerAdminController as baseFlyerAdminController;
use Symfony\Component\Translation\Translator;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use OfficeBrain\CustomBundle\FlyerBundle\Entity\FlyerCategory;
use OfficeBrain\CustomBundle\FlyerBundle\Form\FlyerCategoryType;
use OfficeBrain\Bundle\CoreBundle\CustomeHandler as FormErrorManagercustom;

class FlyerAdminController extends baseFlyerAdminController
{
	private $request;
	private $responseArray;
	private $manager;
	private $flyerAccessField;
	private $flyerConfigurationField;
	private $projectSetting;
	private $translator;
	private $FormErrorManagercustom;
	
	public function __construct() {
		parent::__construct();
		$this->FormErrorManagercustom = new FormErrorManagercustom\JsonError ();
		
	}
	/**
	 * @author Employee Id: 5429
	 *
	 * Description: Create various objects required action in this class
	 *
	 * @return NULL
	 */
	public function initAction()
	{
		parent::initAction();
		
		$this->countryInfo = $this->container->getParameter('country');
		
		$this->manager = $this->get('office_brain_custom_flyer_manager');
		$this->responseArray = array();
	}

	private function prepareFormError($form)
	{
		return array (
				'form_name' => $form->getName (),
				'form_status' => 'error',
				'form_errors' => $this->FormErrorManagercustom->prepareErrorArray ( $this->get ( 'validator' )->validate ( $form ) )
		);
	}
/**
	 *
	 * @author Employee Id: 5429
	 *
	 * Description : List flyer category
	 *
	 * @param  : null
	 *
	 * @return Arraypublic function __construct($entityManager, $request, $translator, $knpPaginator, $country, $language)
	 *
	 * @throws null
	 *
	 * @Route("/admin/{_locale}/flyer/listcategory", name="office_brain_flyerbundle_flyeradmin_listflyercategory")
	 *
	 * @Template("OfficeBrainCustomBundleFlyerBundle:FlyerAdmin:listFlyerCategory.html.twig")
	 *
	 **/
	public function listFlyercategoryAction()
	{
		
		$this->initAction();
		$page  = $this->request->get('page', '1');
		$recordsPerPage =  $this->flyerConfigurationField['no_of_record_per_page'];
		$this->responseArray = $this->manager->getFlyerCategoryList($page,$recordsPerPage);
		$this->responseArray['page'] = $page;
		$this->responseArray['records_per_page'] = $recordsPerPage;
		$this->responseArray['flyerAccessField'] = $this->flyerAccessField;
		//echo "<pre>";print_r($this->flyerConfigurationField);die();
		$this->responseArray['flyerConfigurationField'] = $this->flyerConfigurationField;
		return $this->responseArray;
	}

	/**
	 * @author 5429
	 *
	 * @Route("admin/{_locale}/published-flyercategory/{id}", name="office_brain_flyerbundle_flyeradmin_publishedflyercategory")
	 *
	 * @Template()
	 *
	 * Description: List flyer category 
	 *
	 * @param null
	 *
	 * @return json response
	 *
	 */
	public function publishedFlyerCategoryAction()
	{ 
		$this->initAction();
		$param=$this->request->get('_route_params');
		$flyer= $this->manager->findById($param['id']);
		if(isset($flyer))
		{
			$this->responseArray = $this->manager->publishFlyerCategory($flyer);
			if($this->responseArray['msg'] == "activated")
			{
				$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.flyer_management.publish.activatedcategory'));
			}
			else
			{
				$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.flyer_management.publish.deactivatedcategory'));
			}
		}
		return new JsonResponse($this->responseArray);
	}
	/**
	 * @author 5429
	 *
	 * Description: Add Flyer action
	 *
	 * @Route("/admin/{_locale}/add-new-flyercategory/", name="office_brain_flyerbundle_flyeradmin_flyernewaddcategory")
	 *
	 * @Method({"GET","POST"})
	 *
	 * @return json response
	 *
	 * @throws null
	 *
	 * @Template()
	 *
	 */
	public function flyerCategoryNewAddAction()
	{
		$this->initAction();
		$id = $this->request->query->get('id',0);
		//$locale = $this->request->get('_locale');
		//$cul = $this->request->get('culture');
			
		/* if(isset($cul) && $cul!="")
		{
			$locale = $cul;
		} */
	
		/* @list($localeCulture,$localeCountry) = explode("_", $locale);
		$cultureId =  $this->manager->getLanguageIdByCode($localeCulture);
		$countryId= $this->manager->getCountryIdByCode(strtolower($localeCountry));
		$flyrRootDir = $this->get('kernel')->getRootDir(); */
		
		if($id==0)
		{
			$mod = 'add';
			$flyer = new FlyerCategory();
			$flyerObj = new FlyerCategoryType($mod,$flyer,$this->manager,$this->countryInfo);
			
		}
		else
		{
			$mod = 'edit';
			$flyer = $this->manager->findById($id);
			$flyerObj = new FlyerCategoryType($mod,$flyer,$this->manager,$this->countryInfo); 
		}
		
		$parentdata=$flyer->getparentId();
		$form = $this->createForm($flyerObj, $flyer);
		$form->handleRequest($this->request);
		
		//echo $this->request->get('Is_parent').'man';
		if($this->request->getMethod() == 'POST')
		{
			if($id==0)
			{
				if ($form->isValid())
				{
					$case= $this->manager->saveAddFlyerCategory($flyer,$form,$mod);
					$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.flyercategory_save_msg'));
					$response['Message'] = "success";
					//$response['locale'] = $locale;
					return new JsonResponse($response);
				}
				else
				{
					return new JsonResponse ( $this->prepareFormError ($form));
				}
				
			}
			else
			{
				if ($form->isValid())
				{
					$this->manager->saveAddFlyerCategory($flyer, $form,$mod);
					$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.flyer_updt_msg'));
					$response['Message'] = "success";
					//$response['locale'] = $locale;
					return new JsonResponse($response);
				}
				else
				{
					return new JsonResponse ( $this->prepareFormError ($form));
				}
			}
	
		}
		return array('data'=>'yes','form'=>$form->createView(),'id'=>$id, 'mod'=>$mod,'parentdetail'=>$parentdata);
	}

	/**
	 *
	 * @author Employee Id: 5429
	 *
	 * Description : List Flyer Ajax action
	 *
	 * @param  : null
	 *
	 * @return Array
	 *
	 * @throws null
	 *
	 * @Route("/admin/{_locale}/flye/listcategory-ajax", name="office_brain_flyerbundle_flyeradmin_listflyercategoryajax")
	 *
	 * @Template("OfficeBrainCustomBundleFlyerBundle:FlyerAdmin:tabFlyerCategoryserchListContents.html.twig")
	 *
	 **/
	public function listFlyerAjaxAction()
	{
	   
		$this->initAction();
		$page  = $this->request->get('page', '1');
		$recordsPerPage =  $this->flyerConfigurationField['no_of_record_per_page'];
		$this->responseArray = $this->manager->getFlyerCategoryList($page,$recordsPerPage);
		$this->responseArray['page'] = $page;
		$this->responseArray['records_per_page'] = $recordsPerPage;
		$this->responseArray['flyerAccessField'] = $this->flyerAccessField;
		$this->responseArray['flyerConfigurationField'] = $this->flyerConfigurationField;
		return $this->responseArray;
		
		
	}
	
	/**
	 * @author 5429
	 *
	 * @Route("admin/{_locale}/delete-flyer-category/{action}/{id}", name="office_brain_flyerbundle_flyeradmin_deleteflyercategory")
	 *
	 * @Template()
	 *
	 * Description: Delete Flyer
	 *
	 * @param null
	 *
	 * @return json response
	 *
	 */
	public function deleteFlyerCategoryAction()
	{ 
		$this->initAction();
		$flyerMessage = $this->manager->deleteFlyerCategoryRecords();
		switch ($flyerMessage) {
			case  'SUCCESS' :
				$this->responseArray['status'] = 'success';
				$this->responseArray['success_message'] = $this->get('translator')->trans('admin.flyer_delete_msg');
				break;
			case  'FAIL':
				$this->responseArray['error_message'] = $this->get('translator')->trans('admin.error_msg');
				$this->responseArray['status'] = 'fail';
				break;
		}
		return new JsonResponse($this->responseArray);
	}
	/**
	 * @author Employee Id: 5429
	 * Description : menu detail.
	 *
	 * @Route("/flyer-menu-item", name="officebrain_custom_bundle_cmsmenu_flyerMenuItem")
	 * @Method("GET")
	 * @Template("OfficeBrainCustomBundleFlyerBundle:FlyerAdmin:menu.html.twig")
	 */
	public function flyerMenuItemsAction()
    {
    	$this->initAction();
    	$this->responseArray1 = $this->manager->getFlyerCategoryMenuList();
    	return $this->responseArray1;
    }
}
