<?php

namespace OfficeBrain\CustomBundle\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 Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use OfficeBrain\CustomBundle\CmsBundle\Entity\ContactUs;
use OfficeBrain\CustomBundle\CmsBundle\Form\ContactUsType;
use OfficeBrain\Bundle\CmsBundle\Controller\ContactUsController as BaseContactUsController;

class ContactUsController extends BaseContactUsController
{


	/**
	 *
	 * @author Employee Id: 5069
	 *
	 * Description: Create objects which use this class
	 *
	 * @return NULL
	 *
	 */
	public function initAction()
	{
		parent::initAction();
		$this->cmsContactUsManager = $this->get('office_brain_cms_contact_us_service_manager');
	}

	/**
	 * @author Employee Id: 5069
	 *
	 * Description: OB eCommerce Product -  contact us form
	 *
	 * @Route("{_locale}/contact-us",
	 * 	name="office_brain_cmsbundle_contactus_contact",
	 *  options={"expose"=true}
	 * )
	 *
	 * @Method({"GET"})
	 *
	 * @Template("OfficeBrainCustomBundleCmsBundle: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("OfficeBrainCustomBundleCmsBundle: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(isset($_POST['g-recaptcha-response']) && $_POST['g-recaptcha-response']==''){

             $this->get('session')->getFlashBag()->set('error_message', $this->translator->trans('Oops, you have to check the recaptcha !'));
        }

		if ($this->form->isValid()) {
            if(isset($_POST['g-recaptcha-response']) && $_POST['g-recaptcha-response']!=''){
             $captcha=$_POST['g-recaptcha-response'];
            }
            $secretKey = "6LdFSNYjAAAAAIRwiVnI4UJIfjq1oCbzWuSFQ9a-";
            $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
            $response = file_get_contents($url);
        	$responseKeys = json_decode($response,true);
        	if($responseKeys['success']!=1){
        		$this->get('session')->getFlashBag()->set('error_message', $this->translator->trans('Oops, you have to check the recaptcha !'));
        		return $this->redirect($this->generateUrl("office_brain_page_contact_confirmation"));
        	}
			$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;
			}
			if($add['status']=="SUCCESS"){
				return $this->redirect($this->generateUrl("office_brain_page_contact_confirmation"));
			}else{
				return $this->redirect($this->generateUrl("office_brain_page_contact_confirmation"));
			}
		}

		$this->responseArray['contactUs'] = $this->form->createView();
		$contactUsData = $this->form->getData();
		$this->responseArray['submittedFirstName'] = $contactUsData->getFullName();
		$this->responseArray['submittedLastName'] = $contactUsData->getLastName();
		$this->responseArray['submittedEmail'] = $contactUsData->getEmail();

		return $this->responseArray;
	}

}
