<?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;

/**
 * @author Employee Id: 4861
 *
 * Description : This is Review and rating display on frontend controller class
 *
 * @Route("/{_locale}")
 */
class ReviewAndRatingUserController extends Controller
{
    /**
     * @author Employee Id: 4861
     *
     * 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->reviewRatingManager = null;
        $this->projectSetting = null;
        $this->recordPerPage= null;
        $this->ceilArray = null;
        $this->ratingType = 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->reviewRatingManager);
        unset($this->projectSetting);
        unset($this->recordPerPage);
        unset($this->ceilArray);
        unset($this->ratingType);
    }
    
    /**
     * @author Employee Id: 4861
     *
     * Description : This function is used to initialize the variables
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     */
    public function initAction()
    {
        $this->ceilArray = array();
        $this->responseArray = array();
        $this->request = $this->getRequest();
        $this->projectSetting = $this->request->get('project_setting');
        $this->page = $this->request->get('page', 1);
        $this->recordPerPage = $this->projectSetting['configuration_rule']['search']['product_detail']['review_rating']['option']['record_per_page'];
        $this->ratingType = $this->projectSetting['configuration_rule']['search']['search_page']['quick_view']['option']['review_rating']['option']['rating_type']['status'];
        
        $this->reviewRatingManager = $this->get('office_brain_feedback_manager');
    }
    
    /**
     *   @author 4861
     *
     *   Review and ratings Quick view.
     *   @return  Review and ratings Quick view
     *
     *
     * @Route("/search-review-rating-quick-view/{productId}",
     * 	name="officebrain_feed_back_bundle_search_review_rating_quick_view"
     * )
     *
     * @Template("OfficeBrainFeedBackBundle:Search:searchPageQuickViewReviewRating.html.twig")
     */
    public function searchReviewRatingQuickViewAction($productId)
    {
        $this->initAction();
        $this->responseArray['product_id'] = $productId;
        $this->responseArray['rating_count'] = $this->reviewRatingManager->getRateWiseCountFromProductMaster($productId);
        if ($this->ratingType == 1 && $this->responseArray['rating_count']['review_count'] != 0) {
            $this->responseArray['rating_count'] = $this->reviewRatingManager->ceilArray($this->responseArray['rating_count']);
        }
        $this->responseArray['review_data'] = $this->reviewRatingManager->getFeedBackListFromProductMaster($productId, $this->page, $this->recordPerPage);
        return $this->responseArray;
    }
    
    /**
     *   @author 4861
     *
     *   Review and ratings.
     *   @return  Review and ratings
     *
     *
     * @Route("/product-detail-review-rating/{productId}/{page}",
     * 	name="officebrain_feed_back_bundle_product_detail_review_rating",
     *  options={"expose"=true}
     * )
     *
     * @Template("OfficeBrainFeedBackBundle:Product:productDetailReviewRating.html.twig")
     */
    public function productDetailReviewRatingAction($productId, $page)
    {
        $this->initAction();
        $this->responseArray['feed_back_list'] = $this->reviewRatingManager->getFeedBackListFromProductMaster($productId, $this->page, $this->recordPerPage);

        return $this->responseArray;
    }
}
