<?php

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

/**
 * @author OfficeBrain 4835 <info@officebrain.com>
 * Description: Set configuration for reward-referral
 * configuration must be instance wise
 *
 * @Route("/admin/{_locale}/reward-referral")
 */
class RewardReferralAdminController extends Controller
{
    private $translator;
    private $request;
    private $projectSetting;
    private $responseArray;
    
    /**
     * @author Employee Id: 4835
     * Description : Set member variable default value
     *
     * @param 1 : NULL
     *
     * @return type NULL
     *
     * @throws Exception NULL
     *
     **/
    public function __construct()
    {
        $this->request = null;
        $this->userId = null;
        $this->responseArray = array();
        $this->projectSetting = null;
        $this->rewardReferralManager = null;
    }

     /**
     * @author Employee Id: 4835
     * Description : Unset  member variable
     *
     * @param 1 : NULL
     *
     * @return type NULL
     *
     * @throws Exception NULL
     *
     **/
    public function __destruct()
    {
        unset($this->request);
        unset($this->userId);
        unset($this->responseArray);
        unset($this->projectSetting);
        unset($this->rewardReferralManager);
    }
    
     /**
     * @author Employee Id: 4835
     * Description : Set initilize variable for easy use in every actions
     *
     * @param 1 : NULL
     *
     * @return type NULL
     *
     * @throws Exception  NULL
     *
     **/
    public function initAction()
    {
        $this->translator = $this->get('translator');
        $this->request = $this->getRequest();
        $this->projectSetting = $this->request->get('project_setting');
        $this->entityManager = $this->getDoctrine()->getManager();
        $this->rewardReferralManager= $this->container->get('office_brain_reward_referral_manager');
        $this->countryId=$this->request->get('countryId', $this->projectSetting['culture_id']);
    }
    
    /**
     * @author Employee Id: 4835
     * Description :render form.
     *
     * @param 1 : NULL
     *
     * @Route("/configuration", name="office_brain_rewardreferralbundle_rewardreferraladmin_configuration")
     *
     * @Method("GET")
     *
     * @return type Form View
     *
     * @throws Exception : Null
     *
     * @Template("OfficeBrainRewardReferralBundle:Admin:configuration.html.twig")
     * */
    public function configurationAction()
    {
        $this->initAction();
        
        $form = $this->rewardReferralManager->getForm($this);
        $this->responseArray['form'] = $form->createView();
        $this->responseArray['countryId'] = $this->countryId;
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4835
     * Description :process form.
     *
     * @param 1 : NULL
     *
     * @Route("/configuration/{countryId}", name="office_brain_rewardreferralbundle_rewardreferraladmin_processconfiguration")
     *
     * @Method("POST")
     *
     * @return Form data
     *
     * @throws Exception : Null
     *
     *  @Template("OfficeBrainRewardReferralBundle:Admin:configuration.html.twig")
     * */
    public function processconfigurationAction()
    {
        $this->initAction();
        $form=$this->rewardReferralManager->getForm($this);
      
        if ($form->isValid()) {
            $this->rewardReferralManager->setConfiguration($form);
            $this->get('session')->getFlashBag()->add('success_message', $this->get('translator')->trans("rewardreferral.admin.configuration.sucess"));
            return $this->redirect($this->generateUrl('office_brain_rewardreferralbundle_rewardreferraladmin_configuration'));
        }
        
        $this->responseArray['form'] = $form->createView();
        $this->responseArray['countryId'] = $this->countryId;
        
        return $this->responseArray;
    }
    
     /**
     * @author Employee Id: 4835
     * Description :render form.
     *
     * @param 1 : NULL
     *
     * @Route("/configurationajax/{countryId}", name="office_brain_rewardreferralbundle_rewardreferraladmin_configurationajax")
     *
     * @Method("GET")
     *
     * @return data
     *
     * @throws Exception : Null
     *
     * @Template("OfficeBrainRewardReferralBundle:Admin:configurationajax.html.twig")
     * */
    public function configurationAjaxAction()
    {
        $this->initAction();
        
        $form=$this->rewardReferralManager->getForm($this);
        $this->responseArray['form'] = $form->createView();
        
        return $this->responseArray;
    }
}
