<?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 OfficeBrain\Bundle\FeedBackBundle\Form\FeedBackMasterType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * @author Employee Id: 4464
 *
 * Description : This is user controller class
 *
 * @Route("/{_locale}/my-account/feedback")
 */
class FeedBackUserController 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->userId = null;
        $this->translator = 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->userId);
        unset($this->translator);
    }
    
    /**
     * @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->productId = $this->request->get('productId', null);
        $this->projectSetting = $this->request->get('project_setting');
        $this->responseArray['product_id'] = $this->productId;
        $this->feedbackManager = $this->get('office_brain_feedback_manager');
        $this->userId = $this->projectSetting['user_id'];
        $this->translator = $this->get('translator');
        $this->sessionManager = $this->get('office_brain_session_manager');
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to show the review rating form
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/give-feedback/{order_product_detail}", name = "office_brain_feedbackbundle_feedbackuser_feedback")
     * @method("GET")
     * @Template("OfficeBrainFeedBackBundle:FeedBackUser:feedBack.html.twig")
     */
    public function feedBackAction()
    {
        $this->initAction();
        
        $form = $this->createForm(new FeedBackMasterType());
        
        $orderProductDetail = json_decode(base64_decode($this->request->get('order_product_detail')));
        $orderId = $orderProductDetail->order_id;
        $productId = $orderProductDetail->product_id;
        $this->responseArray['is_feed_back_given'] = $this->feedbackManager->isFeedBackGiven($orderId, $productId);
        $productDetails = $this->feedbackManager->getProductDetails($productId);
        
        if (isset($productDetails['product_not_found'])) {
            //throw new NotFoundHttpException($this->translator->trans('feedback.product_not_found'));
            $this->responseArray['product_not_found'] = $this->translator->trans('feedback.product_not_found');
        }/*  else {
            foreach ($productDetails as $key => $value) {
                $productDetails = $value;
            }
        } */
        
        $this->responseArray['form'] = $form->createView();
        $this->responseArray['order_product_detail'] = $this->request->get('order_product_detail');
        $this->responseArray['product_details'] = $productDetails;
        $this->responseArray['product_details']['order_id'] = $orderId;
        
        unset($form);
        unset($orderProductDetail);
        unset($orderId);
        unset($productId);
        unset($productDetails);
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to store the consumer's review and ratings to the database
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     *
     * @Route("/give-feedback/{order_product_detail}", name = "office_brain_feedbackbundle_feedbackuser_feedbackprocess")
     * @method("POST")
     * @Template("OfficeBrainFeedBackBundle:FeedBackUser:feedBack.html.twig")
     */
    public function feedBackProcessAction()
    {
        $this->initAction();
        
        $orderProductDetail = json_decode(base64_decode($this->request->get('order_product_detail')));
        $orderId = $orderProductDetail->order_id;
        $productId = $orderProductDetail->product_id;
        $productDetails = $this->feedbackManager->getProductDetails($productId);
        
        if (isset($productDetails['product_not_found'])) {
            //throw new NotFoundHttpException($this->translator->trans('feedback.product_not_found'));
            $this->responseArray['product_not_found'] = $this->translator->trans('feedback.product_not_found');
        }
        
        $form = $this->createForm(new FeedBackMasterType());
        $form->handleRequest($this->request);
        
        if ($form->isValid()) {
            $feedBack = $this->feedbackManager->setFeedBack($form->getData(), $orderId, $productId);
            
            if (isset($feedBack['user_not_found'])) {
                $this->responseArray['user_not_found'] = $feedBack['user_not_found'];
                return $this->responseArray;
            }
            
            unset($feedBack);
            $this->sessionManager->setFlashBagMessage('success_message', $this->translator->trans('feedback.site_feedback.success_message'));
            return $this->redirect($this->generateUrl('office_brain_feedbackbundle_feedbackuser_feedbacklist'));//Un comment to redirect to my account page. Please confirm path name
        }
        
        $this->responseArray['form'] = $form->createView();
        $this->responseArray['order_product_detail'] = $this->request->get('order_product_detail');
        $this->responseArray['product_details'] = $productDetails;
        $this->responseArray['product_details']['order_id'] = $orderId;
        
        unset($orderProductDetail);
        unset($orderId);
        unset($productId);
        unset($productDetails);
        unset($form);
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to list user's given review and ratings
     *
     * @param 1: records_per_page
     * @param 2: page
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/list/{page}", name = "office_brain_feedbackbundle_feedbackuser_feedbacklist",
     * requirements={"page"="\d+"},
     * defaults={"page"=1})
     * @method("GET")
     * @Template("OfficeBrainFeedBackBundle:FeedBackUser:list.html.twig")
     */
    public function listAction()
    {
        $this->initAction();
        
        $userId = $this->userId;
        $this->responseArray['pagination'] =array();
        $page = $this->request->get('page', '1');
        
        $this->responseArray['pagination'] = $this->feedbackManager->listFeedBack($userId, $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($userId);
        unset($page);
       
        return $this->responseArray;
    }
}
