<?php

namespace OfficeBrain\Bundle\EmailBundle\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 4795 <info@officebrain.com>
 *
 * Description: Manage events of email
 *
 * @Route("/{_locale}")
 */
class EmailEventController extends Controller
{
    /**
     * @author Employee Id: 4795
     *
     * Description : Set variables
     *
     * @param 1 : Null
     *
     * @return type : Null
     *
     * @throws Exception: Null
     **/
    public function __construct()
    {
        $this->responseArray = array();
        $this->request = null;
        $this->manager = null;
        $this->projectSetting = array();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Unset variables
     *
     * @param 1 : Null
     *
     * @return type : Null
     *
     * @throws Exception: Null
     **/
    public function __destruct()
    {
        unset($this->responseArray);
        unset($this->request);
        unset($this->manager);
        unset($this->projectSetting);
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Set request and service
     *
     * @param 1 : Null
     *
     * @return type : Null
     *
     * @throws Exception: Null
     **/
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->projectSettings = $this->request->get('project_setting');
        $this->accessRules = $this->projectSettings['access_rule'];
        $this->manager = $this->get('office_brain_email_event_manager');
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Generate event form
     *
     * @param 1 : Null
     *
     * @return type : Html Response
     *
     * @throws Exception: Null
     *
     * @Route("/my-account/transactional-email", name="office_brain_emailbundle_emailevent_event")
     *
     * @Method("GET")
     *
     * @Template("OfficeBrainEmailBundle:EmailEvent:event.html.twig")
     */
    public function eventAction()
    {
        $this->initAction();
        
        $this->responseArray['unsubscribed'] = $this->manager->getUnsubscribedEvents();
        $this->responseArray['events'] = $this->manager->getOptionalEvents();
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Generate event form
     *
     * @param 1 : Null
     *
     * @return type : Json Response
     *
     * @throws Exception: Null
     *
     * @Route("/my-account/email/subscription/{id}", name="office_brain_emailbundle_emailevent_subscription")
     *
     * @Method("POST")
     *
     * @Template("OfficeBrainEmailBundle:EmailEvent:subscription.html.twig")
     */
    public function subscriptionAction($id)
    {
        $this->initAction();
        
        $subscription = $this->request->get('subscription');
        $this->manager->eventSubscription($id, $subscription);
        
        //unset variable
        unset($subscription);
        
        if ($this->request->isXmlHttpRequest()) {
            $this->responseArray = array('status'=>'success','success_message'=>"Success");
            return new JsonResponse($this->responseArray);
        }
        
        return $this->responseArray;
    }
}
