<?php

namespace OfficeBrain\Bundle\SubShopBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\Bundle\SubShopBundle\Entity\SubShopInquiry;
use OfficeBrain\Bundle\SubShopBundle\Form\SubShopInquiryType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * @author OfficeBrain 4494 <info@officebrain.com>
 *
 * Description : Sub shop Inquiry class.
 * @Route("/{_locale}/sub-shop/inquiry")
 **/
class SubShopInquiryController extends Controller
{
    private $translator;

    private $request;

    private $projectSetting;

    private $responseArray;

    private $session;

    private $subShopInquiryManager;

    public function __construct(ContainerInterface $container = null)
    {
        $this->translator = null;
        $this->request = null;
        $this->projectSetting = array();
        $this->responseArray = array();
        $this->session = null;
        $this->subShopInquiryManager = null;
        $this->container = $container;
    }

    public function __destruct()
    {
        unset($this->translator);
        unset($this->request);
        unset($this->projectSetting);
        unset($this->responseArray);
        unset($this->session);
        unset($this->subShopInquiryManager);
        unset($this->container);
    }

    public function initAction()
    {
        $this->translator = $this->get('translator');
        $this->request = $this->getRequest();
        $this->projectSetting = $this->request->get('project_setting');
        $this->session = $this->get('session');
        $this->subShopInquiryManager = $this->get('office_brain_subshop_inquiry_manager');
        $this->responseArray = array();
    }

    /**
     * @author Employee Id: 4494
     * Description : Display inquiry form
     *
     * @param 1 : No paramater
     *
     * @return type : Form view.
     *
     * @throws Exception: Null
     *
     * @Route("", name="office_brain_subshopbundle_subshopinquiry_inquiry")
     *
     * @Method("GET")
     * @Template()
     */
    public function inquiryAction()
    {
        $this->responseArray['form'] = $this->createForm(new SubShopInquiryType($this->container))->createView();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Submit inquiry form
     *
     * @param 1 : No paramater
     *
     * @return type : Form view and Submit form data.
     *
     * @throws Exception: Null
     *
     * @Route("", name="office_brain_subshopbundle_subshopinquiry_processinquiry")
     *
     * @Method("POST")
     *
     * @Template("OfficeBrainSubShopBundle:SubShopInquiry:inquiry.html.twig")
     */
    public function processInquirySubmitAction()
    {
        $this->initAction();

        $subshopInquiry = new SubShopInquiry();

        $form = $this->createForm(new SubShopInquiryType($this->container), $subshopInquiry);

        $form->handleRequest($this->request);

        if ($form->isValid()) {
            $inquiryId = $this->subShopInquiryManager->setSubShopInquiry($form->getData(), true);

            $this->session->getFlashBag()->set('success_message', $this->translator->trans('subshop_inquiry.action.subshop_inquiry_add_success'));

            return $this->redirect($this->generateUrl('office_brain_subshopbundle_subshopinquiry_inquiry'));
        }

        $this->responseArray['form'] = $form->createView();

        return $this->responseArray;
    }
}
