<?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 Symfony\Component\HttpFoundation\JsonResponse;

/**
 * @author OfficeBrain 4571 <info@officebrain.com>
 * Description: Manage Third Party Settings for Payment and Shipping Configurations
 *
 * @Route("admin/{_locale}")
 */
class ThirdPartySettingAdminController extends Controller
{
    private $request;
    private $session;
    private $translator;
    private $projectSetting;
    private $recordsPerPage;
    private $responseArray;
    private $cmsThirdPartyManager;

    /**
     * @author Employee Id: 4571
     *
     * Description: Member variables declare with null
     *
     * @param : Null
     *
     * @throws Exception : Null
     */
    public function __construct()
    {
        $this->request = null;
        $this->session = null;
        $this->translator = null;
        $this->projectSetting = null;
        $this->responseArray = null;
        $this->cmsThirdPartyManager = null;
        $this->commonLibraryManager = null;
    }

    /**
     * @author Employee Id: 4571
     *
     * Description: Unset Member variables
     *
     * @param : Null
     *
     * @throws Exception : Null
     */
    public function __destruct()
    {
        unset($this->request);
        unset($this->session);
        unset($this->translator);
        unset($this->projectSetting);
        unset($this->responseArray);
        unset($this->cmsThirdPartyManager);
        unset($this->commonLibraryManager);
    }

    /**
     * @author Employee Id: 4571
     *
     * Description: Initialize member varibles before any action called
     *
     * @param : Null
     *
     * @throws Exception : Null
     */
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->session = $this->request->getSession();
        $this->cmsThirdPartyManager = $this->get('office_brain_cms_third_party_service_manager');
        $this->translator = $this->get('translator');
        $this->projectSetting = $this->request->get('project_setting');
        $this->responseArray = array();
        $this->commonLibraryManager = $this->get('office_brain_common_library');
    }

    /**
     * @author Employee Id: 4571
     *
     * Description: Loads Payment Configuration
     *
     * @param : Null
     *
     * @Route("/cms/third-party-configuration-payment", name="office_brain_cmsbundle_thirdpartysettingadmin_setpaymentconfiguration")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Method("GET")
     *
     * @Template("OfficeBrainCmsBundle:ThirdPartySettingAdmin:setPaymentConfiguration.html.twig")
     */
    public function setPaymentConfigurationAction()
    {
        $this->initAction();

        $this->responseArray['payment_configuration'] = $this->cmsThirdPartyManager->loadParentPaymentMethods();
        foreach ($this->responseArray['payment_configuration'] as $paymentMethod) {
            $this->responseArray['forms'][$paymentMethod->getId()] = $this->cmsThirdPartyManager->generateForm($paymentMethod)->createView();
        }

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4571
     *
     * Description: Submit and Save Configuration
     *
     * @param : Null
     *
     * @Route("/cms/third-party-configuration-payment", name="office_brain_cmsbundle_thirdpartysettingadmin_processconfigurationsubmit")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Method("POST")
     *
     * @Template("OfficeBrainCmsBundle:ThirdPartySettingAdmin:setPaymentConfiguration.html.twig")
     */
    public function processConfigurationSubmitAction()
    {
        $this->initAction();

        $requestForm = $this->request->get('officebrain_bundle_cmsbundle_thirdpartyservicemeta');
        $paymentMethod = $this->cmsThirdPartyManager->getPaymentMethodObject($requestForm['form_hidden_id']);
        $this->form = $this->cmsThirdPartyManager->generateForm($paymentMethod);
        $this->form->handleRequest($this->request);
            
        if ($this->form->isValid()) {    //if form valid than save to db
            $this->cmsThirdPartyManager->saveFields($requestForm);
            $this->responseArray['status'] = 'success'; // 'success';
            $this->responseArray['success_message'] = $this->translator->trans('configuration.form.success_message'); // 'Done!';
        } else {
            if ($this->request->isXmlHttpRequest()) {
                $this->responseArray = $this->commonLibraryManager->prepareFormJsonError($this->form);
            }
        }

        return new JsonResponse($this->responseArray);
    }

    /**
     * @author Employee Id: 4571
     *
     * Description: Loads Payment Configuration
     *
     * @param : Null
     *
     * @Route("/cms/third-party-configuration-shipping", name="office_brain_cmsbundle_thirdpartysettingadmin_setshipmentconfiguration")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Method("GET")
     *
     * @Template("OfficeBrainCmsBundle:ThirdPartySettingAdmin:setShipmentConfiguration.html.twig")
     */
    public function setShipmentConfigurationAction()
    {
        $this->initAction();

        $this->responseArray['payment_configuration'] = $this->cmsThirdPartyManager->loadParentShipmentMethods();
        foreach ($this->responseArray['payment_configuration'] as $shippingMethod) {
            // print_r($shippingMethod);exit;
            $this->responseArray['forms'][$shippingMethod->getId()] = $this->cmsThirdPartyManager->generateForm($shippingMethod)->createView();
        }

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4571
     *
     * Description: Submit and Save Configuration
     *
     * @param : Null
     *
     * @Route("/cms/third-party-configuration-shipping", name="office_brain_cmsbundle_thirdpartysettingadmin_processshipmentconfigurationsubmit")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Method("POST")
     * 
     * @Template("OfficeBrainCmsBundle:ThirdPartySettingAdmin:setShipmentConfiguration.html.twig")
     */
    public function processShipmentConfigurationSubmitAction()
    {
        $this->initAction();

        $requestForm = $this->request->get('officebrain_bundle_cmsbundle_thirdpartyservicemeta');
        $paymentMethod = $this->cmsThirdPartyManager->getPaymentMethodObject($requestForm['form_hidden_id']);
        $this->form = $this->cmsThirdPartyManager->generateForm($paymentMethod);
        $this->form->handleRequest($this->request);

        if ($this->form->isValid()) {    //if form valid than save to db
            $this->cmsThirdPartyManager->saveFields($requestForm);
            $this->responseArray['status'] = 'success'; // 'success';
            $this->responseArray['success_message'] = $this->translator->trans('configuration.form.success_message'); // 'Done!';
        } else {
            if ($this->request->isXmlHttpRequest()) {
                $this->responseArray = $this->commonLibraryManager->prepareFormJsonError($this->form);
            }
        }

        return new JsonResponse($this->responseArray);
    }

    /**
     * @author Employee Id: 4571
     *
     * Description: Loads Configuration country wise
     *
     * @param : Null
     *
     * @Route("/cms/third-party-country-wise-configuration", name="office_brain_cmsbundle_thirdpartysettingadmin_loadconfigurationcountrywise")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Method("GET")
     *
     * @Template()
     */
    public function loadConfigurationCountryWiseAction()
    {
        $this->initAction();

        $this->responseArray = $this->cmsThirdPartyManager->getFormDetailsCountryWise();
        if (!empty($this->responseArray)) {
            $status = 'success';
            $returnData = $this->responseArray;
        } else {
            $status = 'fail';
            $returnData = null;
        }

        unset($this->responseArray);

        return new JsonResponse(array('status' => $status, 'responseVal' => $returnData));
    }

    /**
     * @author Employee Id: 4571
     *
     * Description: get active shipping method from country id
     *
     * @param : Null
     *
     * @Route("/cms/get-active-shipping-methods/{countryCode}", name="office_brain_cmsbundle_thirdpartysettingadmin_getactiveshipping")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Method("GET")
     */
    public function getActiveShippingAction($countryCode)
    {
        $this->initAction();

        $this->responseArray = $this->cmsThirdPartyManager->getShippingCountryWise($countryCode);
        if (!empty($this->responseArray)) {
            $status = 'success';
            $returnData = $this->responseArray;
        } else {
            $status = 'fail';
            $returnData = null;
        }
        unset($this->responseArray);

        return new JsonResponse(array('status' => $status, 'responseVal' => $returnData));
    }
    
    /**
     * @author Employee Id: 4571
     *
     * Description: get active shipping method from country id
     *
     * @param : Null
     *
     * @Route("/cms/get-active-payment-methods/{countryCode}", name="office_brain_cmsbundle_thirdpartysettingadmin_getactivepayment")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Method("GET")
     */
    public function getActivePaymentAction($countryCode)
    {
    	$this->initAction();
    
    	$this->responseArray = $this->cmsThirdPartyManager->getPaymentCountryWise($countryCode);
    	if (!empty($this->responseArray)) {
    		$status = 'success';
    		$returnData = $this->responseArray;
    	} else {
    		$status = 'fail';
    		$returnData = null;
    	}
    	unset($this->responseArray);
    
    	return new JsonResponse(array('status' => $status, 'responseVal' => $returnData));
    }
    
    /**
     * @author Employee Id: 4571
     *
     * Description: Loads Configuration  
     *
     * @param : Null
     *
     * @Route("/cms/load-configuration", name="office_brain_cmsbundle_thirdpartysettingadmin_loadconfiguration")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Method("GET")
     *
     * @Template()
     */
    public function loadConfigurationAction()
    {
    	$this->initAction();
    
    	$this->responseArray = $this->cmsThirdPartyManager->getConfigDetails();
    	if (!empty($this->responseArray)) {
    		$status = 'success';
    		$returnData = $this->responseArray;
    	} else {
    		$status = 'fail';
    		$returnData = null;
    	}
    
    	unset($this->responseArray);
    
    	return new JsonResponse(array('status' => $status, 'responseVal' => $returnData));
    }
    
    
    
}
