<?php

namespace OfficeBrain\Bundle\PaymentBundle\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;

/**
 *
 *
 * @Route("", defaults={"rule_service"="rule_charges"})
 */
class PaymentController extends Controller
{
	/**
	 *
	 * @author Employee Id: 4203
	 * To set member variables default value
	 * @return NULL
	 */
	public function __construct()
	{
		$this->request = null;
		$this->user = null;
		$this->userId = null;
		$this->locale = null;
		$this->cultureId = null;
		$this->refererUrl = null;
		$this->sessionManager = null;
		$this->projectSetting = null;
		$this->formManager= null;
		$this->validationManager= null;
		$this->coreManager= null;
		$this->response= null;
		$this->orderManager= null;
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 * To unset member variables
	 * @return NULL
	 */
	public function __destruct()
	{
		unset($this->request);
		unset($this->user);
		unset($this->userId);
		unset($this->locale);
		unset($this->cultureId);
		unset($this->refererUrl);
		unset($this->sessionManager);
		unset($this->projectSetting);
		unset($this->formManager);
		unset($this->validationManager);
		unset($this->coreManager);
		unset($this->response);
		unset($this->orderManager);
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 * Create various objects required action in this class 
	 * @return NULL
	 */
	public function initAction()
	{
		$this->request = $this->getRequest();
		$this->projectUserSetting = $this->request->get('project_user_setting');
		$this->user = $this->projectUserSetting['user_data'];
		$this->userId = $this->projectSetting['user_id'];
		$this->locale = $this->projectSetting['culture_code'];
		$this->cultureId = $this->projectSetting['culture_id'];
		$this->sessionManager = $this->get('office_brain_session_manager');
		$this->projectSetting = $this->request->get('project_setting');
		$this->validationManager = $this->get('office_brain_validation_manager');
		$this->coreManager = $this->get('office_brain_core_manager');
		$this->paymentManager = $this->get('office_brain_payment_manager');
		$this->orderManager = $this->get('office_brain_order_manager');
		$this->response =array();
	}
	
	/**
	 * @author Employee Id: 4203
	 * 
	 * @Route("/{_locale}/payment-form",
	 * name="office_brain_paymentbundle_payment_loadpaymentform",
	 * options={"expose"=true}
	 * )
	 */
	public function loadPaymentFormAction()
	{
		$this->initAction();
		$this->responseArray['total_record'] = $this->orderManager->getTotalCount();
		if($this->responseArray['total_record']>0){
		$this->response=$this->paymentManager->loadPaymentForm($this);
			return $this->render("OfficeBrainPaymentBundle:Payment:".$this->response['payment_type']."PaymentMethod.html.twig", $this->response);
		}else{
			if ($this->request->isXmlHttpRequest()) {
    			$this->responseArray = 'logout';
    			return new JsonResponse($this->responseArray);
    		}else{
    			return $this->redirect($this->generateUrl('office_brain_orderbundle_order_cartlist'));
    		}
		}
	}

	/**
	 * @author Employee Id: 4203
	 * @Route("/{_locale}/payment",
	 * name="office_brain_paymentbundle_payment_preparepayment",
	 * options={"expose"=true}
	 * )
	 */
	public function preparePaymentAction()
	{
		$this->initAction();
		$this->response=$this->paymentManager->preparePayment($this); 
		switch ($this->response['status']) {
			case 'CAPTURE':
				$this->responseArray['status'] = 'success';
				$this->responseArray['goto_url'] = $this->response['captureUrl'];
				if ($this->request->isXmlHttpRequest()) {
					return new JsonResponse($this->responseArray);
				}
				break;
			case 'CAPTURE_AIM':
				return $this->forward('PayumBundle:Capture:do', array('payum_token' => $this->response['payum_token']));
				break; 
			case 'INVOICE':
				$this->responseArray['status']='success';
				$this->responseArray['goto_url']=$this->generateUrl('office_brain_orderbundle_payment_paymentsuccess');
				if ($this->request->isXmlHttpRequest()) {
					return new JsonResponse($this->responseArray);
				}
				break; 
			case 'NOTVALIDATE':
				if ($this->request->isXmlHttpRequest()) {
					return new JsonResponse($this->response['notValid']);
				}
				break;
		}
	}
	
	
	/**
	 * @author Employee Id: 4203
	 * @Route("/{_locale}/paypal-response/{payment_type}",
	 * name="office_brain_paymentBundle_payment_paypalresponse",
	 * defaults={"payment_type"="paypalExpress"}
	 * )
	 * 
	 * @Route("/{_locale}/stripe-response/{payment_type}",
	 * name="office_brain_paymentBundle_payment_striperesponse",
	 * defaults={"payment_type"="stripe"}
	 * )
	 * 
	 * @Route("/{_locale}/authorize-response/{payment_type}",
	 * name="office_brain_paymentBundle_payment_authorizeresponse",
	 * defaults={"payment_type"="authorizeDotNetAim"}
	 * )
	 * 
	 */
	
	public function captureDoneAction(){
		$this->initAction();
		$this->response=$this->paymentManager->captureDone($this);
		switch ($this->response['status']) {
			case 'FAILED':
				if ($this->request->isXmlHttpRequest()) {
					$this->responseArray['status'] = 'success';
					$this->responseArray['goto_url'] = $this->response['url'];
					return new JsonResponse($this->responseArray);
				}
				return $this->redirect($this->response['url']);
				break;
			case 'SUCCESS':
    			if ($this->request->isXmlHttpRequest()) {
    				$this->responseArray['status'] = 'success';
    				$this->responseArray['goto_url'] = $this->response['url'];
    				return new JsonResponse($this->responseArray);
    			} 
				return $this->redirect($this->response['url']);
				break;
		}
	}
	
}
