<?php

namespace OfficeBrain\Bundle\FeedBackBundle\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 OfficeBrain\Bundle\FeedBackBundle\Entity as Entity;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * @author Employee Id: 4464
 *
 * Description : This is admin controller class
 *
 * @Route("/admin/{_locale}/feedback")
 */
class FeedBackAdminController extends Controller
{
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to set member variables default value
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function __construct()
    {
        $this->request = null;
        $this->responseArray = array();
        $this->projectSetting = null;
        $this->entityManager = null;
        $this->feedbackManager = null;
        $this->recordsPerPage = 10;
        $this->translator = null;
        $this->productApiManager = null;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to unset member variables
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     */
    public function __destruct()
    {
        unset($this->request);
        unset($this->responseArray);
        unset($this->entityManager);
        unset($this->projectSetting);
        unset($this->feedbackManager);
        unset($this->recordsPerPage);
        unset($this->translator);
        unset($this->productApiManager);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to initialize the variables
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     */
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->entityManager = $this->getDoctrine()->getManager();
        $this->projectSetting = $this->request->get('project_setting');
        $this->feedbackManager = $this->get('office_brain_feedback_manager');
        $this->recordsPerPage = $this->projectSetting['configuration_rule']['site_management']['option']['feedback']['option']['feedback_record_per_page']['status'];
        $this->translator = $this->get('translator');
        $this->productApiManager = $this->get("office_brain_product_admin_manager");
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to give the list of recieved feedbacks from the CONSUMERS
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/list/{page}", name = "office_brain_feedbackbundle_feedbackadmin_list",
     * requirements={"page"="\d+"},
     * defaults={"page"=1})
     * @Template("OfficeBrainFeedBackBundle:FeedBackAdmin:list.html.twig")
     */
    public function listAction()
    {
        $this->initAction();
        
        $this->responseArray['pagination'] =array();
        $page = $this->request->get('page', '1');

        $this->responseArray['pagination'] = $this->feedbackManager->listFeedBack(null, $page);
        
        if (isset($this->responseArray['pagination']['user_not_found'])) {
            $this->responseArray['user_not_found'] = $this->responseArray['pagination']['user_not_found'];
        }
        
        if (isset($this->responseArray['pagination']['product_not_found'])) {
            $this->responseArray['product_not_found'] = $this->responseArray['pagination']['product_not_found'];
        }
        
        unset($page);
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to view the feedback
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/view/{feedback_id}", name = "office_brain_feedbackbundle_feedbackadmin_view", requirements={"feedback_id": "\d+"})
     * @method("GET")
     * @Template("OfficeBrainFeedBackBundle:FeedBackAdmin:view.html.twig")
     */
    public function viewAction()
    {
        $this->initAction();
    
        $feedBackId = $this->request->get('feedback_id');
        $this->responseArray['feedback_detail'] = $this->feedbackManager->viewFeedBack($feedBackId);
    
        unset($feedBackId);
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to approve Consumer's review and comment
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/approve/{feedback_id}", name = "office_brain_feedbackbundle_feedbackadmin_approve", requirements={"feedback_id": "\d+"})
     * @Template()
     */
    public function approveAction()
    {
        $this->initAction();
        
        $feedBackId = $this->request->get('feedback_id');
        $feedBack = $this->feedbackManager->approveFeedBack($feedBackId);

        if (count($feedBack) > 0) {
        	$this->updateSolrProductData($feedBack);
        }

        unset($feedBackId);
        unset($isFeedBackApproved);
        
        return $this->redirect($this->generateUrl('office_brain_feedbackbundle_feedbackadmin_list'));
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to reject Consumer's review and comment
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/reject/{feedback_id}", name = "office_brain_feedbackbundle_feedbackadmin_reject", requirements={"feedback_id": "\d+"})
     * @Template()
     */
    public function rejectAction()
    {
        $this->initAction();
        
        $feedBackId = $this->request->get('feedback_id');
        $feedBack = $this->feedbackManager->rejectFeedBack($feedBackId);
        
        if (count($feedBack) > 0) {
        	$this->updateSolrProductData($feedBack);
        }
        
        unset($feedBackId);
        unset($isFeedBackApproved);
        
        return $this->redirect($this->generateUrl('office_brain_feedbackbundle_feedbackadmin_list'));
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to (soft)delete Consumer's review and comment
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/delete", name = "office_brain_feedbackbundle_feedbackadmin_delete")
     * @method("GET")
     * @Template()
     */
    public function deleteAction()
    {
        $this->initAction();
        
        //$feedBackId = $this->request->get('feedback_id');
        $feedBackId = $this->request->get('id');
        $feedBack = $this->feedbackManager->deleteFeedBack($feedBackId);
        
        if (count($feedBack) > 0) {
            //$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('feedback.admin.list_feedback.feedback_delete_success'));
            $this->responseArray['status'] = 'success';
            $this->responseArray['success_message'] = $this->translator->trans('feedback.admin.list_feedback.feedback_delete_success');
            $this->updateSolrProductData($feedBack);
        } else {
            //$this->get('session')->getFlashBag()->add('error_message', $this->translator->trans('feedback.admin.list_feedback.feedback_delete_error'));
            $this->responseArray['status'] = 'error';
            $this->responseArray['error_message'] = $this->translator->trans('feedback.admin.list_feedback.feedback_delete_error');
        }
        unset($isFeedBackDeleted);
        
        /* if ($this->request->isXmlHttpRequest()) {
            return new JsonResponse(array('success' => 'success', 'page_url' => $this->generateUrl('office_brain_feedbackbundle_feedbackadmin_list', array())));
        } else {
            return $this->redirect($this->generateUrl('office_brain_feedbackbundle_feedbackadmin_list'));
        } */
        
        unset($feedBackId);
        unset($isFeedBackDeleted);
        return new JsonResponse($this->responseArray);
    }

    private function updateSolrProductData($feedBack) {
    	//SOLR SYNC CODE START
    	if($this->container->getParameter('solr_sync')){
    		$this->productApiManager->updateSolrData($feedBack->getProductId(), $this->container->getParameter('localization_to_fetch'), $this->projectSetting['project_id'],$this->container->getParameter("kernel.environment"),$this->projectSetting['instance_id'],'update');
    	}
    	//SOLR SYNC CODE END
    }
}
