<?php

namespace OfficeBrain\CustomBundle\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;
use OfficeBrain\Bundle\PaymentBundle\Controller\PaymentController as BasePaymentController;

/**
 *
 *
 * @Route("", defaults={"rule_service"="rule_charges"})
 */
class PaymentController extends BasePaymentController
{
	/**
	 * @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);
			if(isset($this->response['payment_type']) && $this->response['payment_type']=='check') {
				return $this->render("OfficeBrainCustomBundlePaymentBundle:Payment:".$this->response['payment_type']."PaymentMethod.html.twig", $this->response);
			}
			else {
				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 'DoDirectPayment':
				$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 'CHECK':
				$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;
			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;
		}
	}
}
