<?php

namespace OfficeBrain\Bundle\CmsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use OfficeBrain\Bundle\CmsBundle\Entity\ContactUs;
use OfficeBrain\Bundle\CoreBundle\CustomeHandler as FormErrorManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use OfficeBrain\Bundle\CmsBundle\Form\ContactUsType;

class ContactUsController extends Controller
{
	protected $request;
	protected $user;
	protected $userId;
	protected $locale;
	protected $session;
	protected $field;
	protected $cmsContactUsManager;
	protected $translator;
	protected $formErrorManager;
	protected $emailService;

	/**
	 *
	 * @author Employee Id: 4493
	 * Description: set default value
	 *
	 * @return NULL
	 */
	public function __construct()
	{
		$this->request = null;
		$this->user = null;
		$this->userId = null;
		$this->locale = null;
		$this->session = null;
		$this->field = null;
		$this->form = null;
		//$this->contactBookId = null;
		$this->cmsContactUsManager = null;
		$this->translator = null;
		$this->formErrorManager = new FormErrorManager\JsonError();
		$this->emailService = null;
	}

	/**
	 *
	 * @author Employee Id: 4493
	 * Description: To unset value
	 *
	 * @return NULL
	 */
	public function __destruct()
	{
		unset($this->request);
		unset($this->user);
		unset($this->userId);
		unset($this->locale);
		unset($this->session);
		//unset($this->contactBookId);
		unset($this->translator);
		unset($this->field);
		unset($this->form);
		unset($this->formErrorManager) ;
		unset($this->cmsContactUsManager );
		unset($this->emailService);
		//unset($this->queueApiManager);
	}


	private function prepareFormJsonError()
	{
		return array(
				'form_name' => $this->form->getName(),
				'form_status' => 'error',
				'form_errors' => $this->formErrorManager->prepareErrorArray($this->get('validator')->validate($this->form))
		);
	}


	/**
	 *
	 * @author Employee Id: 4493
	 *
	 * Description: Create objects which use this class
	 *
	 * @return NULL
	 *
	 */
	public function initAction()
	{
		$this->request = $this->getRequest();
		$this->session = $this->request->getSession();
		//$this->contactBookId = $this->request->get('contactBookId', null);
		$this->responseArray['status'] = 'error';
		$this->translator = $this->get('translator');
		$this->projectSetting = $this->request->get('project_setting');
		$this->locale = $this->projectSetting['locale_language'];
		$this->userId = $this->projectSetting['user_id'];
		$this->emailService = $this->get('office_brain_email_service');

		$this->cmsContactUsManager = $this->get('office_brain_cms_contact_us_service_manager');
	}

	/**
	 * @author Employee Id: 4571
	 *
	 * Description: OB eCommerce Product -  contact us form
	 *
	 * @Route("{_locale}/contact-us",
	 * 	name="office_brain_cmsbundle_contactus_contact",
	 *  options={"expose"=true}
	 * )
	 *
	 * @Method({"GET"})
	 *
	 * @Template("OfficeBrainCmsBundle:ContactUs:contact.html.twig")
	 *
	 */

	public function contactAction()
	{
		$this->initAction();

		$this->form = $this->createForm(new ContactUsType($this->container), new ContactUs());
		$this->form->handleRequest($this->request);
		$this->responseArray['contactUs'] = $this->form->createView();

		return  $this->responseArray;
	}

	/**
	 * @author Employee Id: 4571
	 * Description :OB eCommerce Product -  contact us form submit
	 *
	 * @Route("{_locale}/contact-us",
	 * 	name="office_brain_cmsbundle_contactus_postcontact",
	 *  options={"expose"=true}
	 * )
	 * @Method({"POST"})
	 * @Template("OfficeBrainCmsBundle:ContactUs:contact.html.twig")
	 */

	public function postContactAction()
	{
		$this->initAction();

		$this->form = $this->createForm(new ContactUsType($this->container), new ContactUs());
		$this->form ->handleRequest($this->request);

		if ($this->form->isValid()) {
			$contactUsData = $this->form->getData();
			$add = $this->cmsContactUsManager->addContactUs($contactUsData, $this->emailService);
			switch ($add['status']) {
				case  'SUCCESS' :
					$this->get('session')->getFlashBag()->set('success_message', $this->translator->trans('cms.front.contact.success_mesg'));
					break;
				case  'FAIL':
					$this->get('session')->getFlashBag()->set('error_message', $this->translator->trans('cms.front.contact.error_mesg'));
					break;
			}

			return $this->redirect($this->generateUrl("office_brain_cmsbundle_contactus_contact"));
		}

		$this->responseArray['contactUs'] = $this->form->createView();

		return $this->responseArray;
	}

}
