<?php

namespace OfficeBrain\Bundle\ServiceGroupBundle\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 OfficeBrain\Bundle\ServiceGroupBundle\Form\ServiceGroupType;

/**
 * @author OfficeBrain 4589 <info@officebrain.com>
 *
 * Description : Manage service groups
 *
 * @Route("/admin/{_locale}/service-group")
 */
class ServiceGroupAdminController extends Controller
{
    protected $request;
    protected $entityManager;
    protected $OBCommon;
    protected $session;
    protected $isAjax;
    protected $projectSetting;
    protected $responseArray;
    protected $serviceGroupManager;
    protected $translator;

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : set value as null.
     *
     * @param 1 : No parameter
     *
     * @return type : No return type.
     *
     * @throws Exception: No exception
     *
     **/
    public function __construct()
    {
        $this->request = null;
        $this->entityManager = null;
        $this->OBCommon = null;
        $this->session = null;
        $this->isAjax = null;
        $this->projectSetting = null;
        $this->responseArray = array();
        $this->serviceGroupManager = null;
        $this->translator = null;
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : unset value.
     *
     * @param 1 : No parameter
     *
     * @return type : No return type.
     *
     * @throws Exception: No exception
     *
     **/
    public function __destruct()
    {
        unset($this->request);
        unset($this->entityManager);
        unset($this->OBCommon);
        unset($this->session);
        unset($this->isAjax);
        unset($this->projectSetting);
        unset($this->responseArray);
        unset($this->serviceGroupManager);
        unset($this->translator);
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : initialize value.
     *
     * @param 1 : No parameter
     *
     * @return type : No return type.
     *
     * @throws Exception: No exception
     *
     **/
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->entityManager = $this->getDoctrine()->getManager();
        $this->OBCommon = $this->get('office_brain_core_manager');
        $this->session = $this->get('session');
        $this->isAjax = $this->request->isXmlHttpRequest();
        $this->projectSetting = $this->request->get('project_setting');
        $this->serviceGroupManager = $this->get('office_brain_service_group_manager');
        $this->translator = $this->get('translator');
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : Get the list of service group.
     *
     * @param 1 : Record Per Page
     * @param 2 : Page Number
     *
     * @return type : Service Group Array.
     *
     * @throws Exception: No exception
     *
     * @Route("/list/{page}", name="office_brain_servicegroupbundle_servicegroupadmin_list", requirements={"page"="\d+"}, defaults={"page"=1})
     *
     * @Method({"GET"})
     *
     * @Template()
     *
     **/
    public function listAction()
    {
        $this->initAction();
        $this->responseArray = $this->serviceGroupManager->getServiceGroupList();
        return $this->responseArray;
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : Get List of service group.
     *
     * @param 1 : Record Per Page
     * @param 2 : Page Number
     *
     * @return type : Service Group Array.
     *
     * @throws Exception: No exception
     *
     * @Route("/list/{page}", name="office_brain_servicegroupbundle_servicegroupadmin_listajax", requirements={"page"="\d+"}, defaults={"page"=1})
     *
     * @Method({"POST"})
     *
     * @Template()
     *
     */
    public function listAjaxAction()
    {
        $this->initAction();
        $this->responseArray = $this->serviceGroupManager->getServiceGroupList();
        return $this->responseArray;
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : Get List of service group.
     *
     * @param 1 : Record Per Page
     * @param 2 : Page Number
     *
     * @return type : Service Group Array.
     *
     * @throws Exception: No exception
     *
     * @Route("/list-filter/{countryId}", name="office_brain_servicegroupbundle_servicegroupadmin_listfilterajax")
     *
     * @Template()
     *
     */
    public function listFilterAjaxAction()
    {
        $this->initAction();

        $this->responseArray = $this->serviceGroupManager->getServiceGroupListFilter();

        return new JsonResponse(array('data' => $this->responseArray));
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : Display Add service group form.
     *
     * @param 1 : No Parameter
     *
     * @return type : Form view
     *
     * @throws Exception: No exception
     *
     * @Route("/add", name="office_brain_servicegroupbundle_servicegroupadmin_add")
     *
     * @Template()
     *
     * @Method({"GET"})
     *
     */
    public function addAction()
    {
        $this->initAction();

        $serviceGroupObj = $this->serviceGroupManager->getBlankObject();

        $this->form = $this->createForm(new ServiceGroupType($this->container), $serviceGroupObj);
        $this->form->handleRequest($this->request);
        $this->formData = $this->form->getData();
        $this->responseArray ['form'] = $this->form->createView();

        return $this->responseArray;
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : Submit service group form.
     *
     * @param 1 : No Parameter
     *
     * @return type : Form view and data
     *
     * @throws Exception: No exception
     *
     * @Route("/service-group/add", name="office_brain_servicegroupbundle_servicegroupadmin_processadd")
     *
     * @Template("OfficeBrainServiceGroupBundle:ServiceGroupAdmin:add.html.twig")
     *
     * @Method({"POST"})
     *
     */
    public function processAddAction()
    {
        $this->initAction();

        $serviceGroupObj = $this->serviceGroupManager->getBlankObject();

        $this->form = $this->createForm(new ServiceGroupType($this->container), $serviceGroupObj);
        $this->form->handleRequest($this->request);
        $this->form = $this->serviceGroupManager->filterFormData($this->form);

        /*
        if (is_array($form)) {
            //$this->session->getFlashBag()->add('error_message',$this->translator->trans($form['error_message']));
        } else {
            $this->form = $form;
            $this->formData = $this->form->getData();
        }
        */
        if (!is_array($this->form) && $this->form->isValid()) {
            $resultArray = $this->serviceGroupManager->setServiceGroup($this->form);
            if (isset($resultArray['error_message'])) {
                $this->session->getFlashBag()->add('error_message', $this->translator->trans($resultArray['error_message']));
            } else {
                $this->session->getFlashBag()->add('success_message', $this->translator->trans($resultArray['success_message']));

                return $this->redirect($this->generateUrl('office_brain_servicegroupbundle_servicegroupadmin_list', array()));
            }
        }

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

        return $this->responseArray;
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : Edit service group form.
     *
     * @param 1 : Service Group Id
     *
     * @return type : Form view and data
     *
     * @throws Exception: No exception
     *
     * @Route("/edit/{id}", name="office_brain_servicegroupbundle_servicegroupadmin_edit")
     *
     * @Template()
     *
     * @Method({"GET"})
     *
     */
    public function editAction($id)
    {
        $this->initAction();

        /*
        $param = $this->request->get('_route_params');
        if ($param['action'] == 'update') {
            $id = $param['id'];
            $this->serviceGroupFormObject($id);
            $this->responseArray['id'] = $id;
        } else {
            $serviceGroupObj = $this->serviceGroupManager->getBlankObject();
            $this->form = $this->createForm(new ServiceGroupType($this->container), $serviceGroupObj);
            unset($serviceGroupObj);
        }
        unset($param);
        */

        $this->serviceGroupFormObject($id);

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

        return $this->responseArray;
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : Submit service group form.
     *
     * @param 1 : Service Group Id
     *
     * @return type : Form view and data
     *
     * @throws Exception: No exception
     *
     * @Route("/edit/{id}", name="office_brain_servicegroupbundle_servicegroupadmin_processedit")
     *
     * @Template("OfficeBrainServiceGroupBundle:ServiceGroupAdmin:edit.html.twig")
     *
     */
    public function processEditAction($id)
    {
        $this->initAction();

        $this->serviceGroupFormObject($id);
        $this->form->handleRequest($this->request);
        $this->form = $this->serviceGroupManager->filterFormData($this->form);

        if ($this->form->isValid()) {
            $resultArray = $this->serviceGroupManager->setServiceGroup($this->form, $id);

            if (isset($resultArray['error_message'])) {
                $this->session->getFlashBag()->add('error_message', $this->translator->trans($resultArray['error_message']));
            } else {
                $this->session->getFlashBag()->add('success_message', $this->translator->trans($resultArray['success_message']));
            }

            return $this->redirect($this->generateUrl('office_brain_servicegroupbundle_servicegroupadmin_list', array()));
        }
        /* else {
            $this->responseArray = $this->request->request->all ();
        $this->OBCommon->formRequestFormCheck ( $this->responseArray, $this->form );
        } */
        $this->responseArray['form'] = $this->form->createView();
        $this->responseArray['data'] = array();
        $this->responseArray['id'] = $id;

        return $this->responseArray;
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : Delete service group.
     *
     * @param 1 : Service Group Id
     *
     * @return type : JsonResponse object
     *
     * @throws Exception: No exception
     *
     * @Route("/delete", name="office_brain_servicegroupbundle_servicegroupadmin_delete")
     *
     * @Method({"GET"})
     *
     * @Template()
     *
     */
    public function deleteAction()
    {
        $this->initAction();
        $serviceGroupId = $this->request->get('id');

        if ($serviceGroupId) {
            $this->serviceGroupManager->deleteServiceGroup($serviceGroupId);

            $this->session->getFlashBag()->add('success_message', $this->translator->trans('admin.service_group_management.service_group.delete_message'));

            return new JsonResponse(array('success' => 'success', 'page_url' => $this->generateUrl('office_brain_servicegroupbundle_servicegroupadmin_list', array())));
        }
    }

    /**
     *
     * @author Employee Id: 4273
     *
     * Description : Change Status Service Group.
     *
     * @param 1 : Rule Master Id
     *
     * @return type : JsonResponse object
     *
     * @throws Exception: No exception
     *
     * @Route("/change-status", name="office_brain_servicegroupbundle_servicegroupadmin_change_status")
     *
     * @Method({"POST"})
     *
     * @Template()
     *
     */
    public function changeStatusAction()
    {
        $this->initAction();

        $id = $this->request->get('id');

        $serviceGroup = null;
        if ($id) {
            $serviceGroup = $this->serviceGroupManager->changeRuleMasterStatus($id);
            if ($serviceGroup) {
                return new JsonResponse(array('status' => 'success', 'status_message' => $this->translator->trans('admin.service_group_management.service_group.status_message_success')));
            }
        }

        return new JsonResponse(array('status' => 'fail', 'status_message' => $this->translator->trans('admin.service_group_management.service_group.status_message_fail')));
    }

    /**
     *
     * @author Employee Id: 4248
     *
     * Description : get New Service group form.
     *
     * @param 1 : Service Group Id
     *
     * @return type : Service group form
     *
     * @throws Exception: No exception
     *
     */
    public function serviceGroupFormObject($id)
    {
        if ($id) {
            $serviceGroup = $this->serviceGroupManager->getServiceGroupById($id);
        }
        $serviceGroupUpdate = $this->OBCommon->camelCaseKeys($serviceGroup);
        $validationParams = array('allowValidation' => true, 'fieldAllowed' => true, 'mode'=>'edit', 'data' => $serviceGroupUpdate);
        $this->form = $this->createForm(new ServiceGroupType($this->container, $validationParams), $serviceGroup);
    }

    /**
     *
     * @author Employee Id: 4589
     *
     * Description : make copy of service group
     *
     * @param 1 : No Parameter
     *
     * @return type : Redirect to Service group list
     *
     * @throws Exception: No exception
     *
     * @Route("/copy", name="office_brain_servicegroupbundle_servicegroupadmin_copyservicegroup")
     *
     * @Method({"POST"})
     *
     */
    public function copyServiceGroupAction()
    {
        $this->initAction();

        $this->serviceGroupManager->copyServiceGroup();

        $this->session->getFlashBag()->add('success_message', $this->translator->trans('admin.service_group_management.make_copy.copy_message'));

        return $this->redirect($this->generateUrl('office_brain_servicegroupbundle_servicegroupadmin_list', array()));
    }
}
