<?php

namespace OfficeBrain\Bundle\WishListBundle\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\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Translation\Translator;

/**
 *
 * @author OfficeBrain 4074 <info@officebrain.com>
 *
 * Desciption: This controller help user to manage his/her wish list
 * 
 * @package: OfficeBrain eCommerce Product Web Client
 * 
 * @subpackage: Wish List
 *
 * @version 1.0
 * 
 */
class WishListController extends Controller
{
    /**
     *
     * @author Employee Id: 4074
     *
     * Desciption: Define member variables with thier default value
     *
     * @param NULL
     * 
     * @return NULL
     * 
     * @throws NULL
     * 
     */
    public function __construct()
    {
        $this->request = null;
        $this->userId = null;
        $this->productId = null;
        $this->refererUrl = null;
        $this->wishListManager = null;
        $this->responseArray = array();
        $this->projectSetting = null;
        $this->cultureProductId = null;
        $this->commonLibraryManager = null;
        $this->inlineViewProductMaxLimit = null;
        $this->sessionManager = null;
    }
    
    /**
     *
     * @author Employee Id: 4074
     *
     * Desciption: Unset member variables
     *
     * @param NULL
     * 
     * @return NULL
     * 
     * @throws NULL
     * 
     */
    public function __destruct()
    {
        unset($this->userId);
        unset($this->request);
        unset($this->productId);
        unset($this->refererUrl);
        unset($this->responseArray);
        unset($this->projectSetting);
        unset($this->wishListManager);
        unset($this->cultureProductId);
        unset($this->commonLibraryManager);
        unset($this->inlineViewProductMaxLimit);
        unset($this->sessionManager);
    }
    
    /**
     *
     * @author Employee Id: 4074
     *
     * Desciption: Create and initialize various required object and member variables
     *
     * @param NULL
     *
     * @return NULL
     *
     * @throws NULL
     *
     */
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->page = $this->request->get('page', 1);
        $this->productId = $this->request->get('productId', null);
        $this->cultureProductId = $this->request->get('cultureProductId', null);
        
        $this->projectSetting = $this->request->get('project_setting');
        $this->userId = $this->projectSetting['user_id'];
        $this->inlineViewProductMaxLimit = $this->projectSetting['configuration_rule']['transaction']['wish_list']['inline_view']['option']['no_of_product'];

        $this->responseArray['product_id'] = $this->productId;
        $this->responseArray['status'] = 'error';
        
        $this->wishListManager = $this->get('office_brain_wish_list_manager');
        $this->commonLibraryManager = $this->get('office_brain_common_library');
        $this->sessionManager = $this->get('office_brain_session_manager');
    }

    /**
     *
     * @author Employee Id: 4014
     *
     * Desciption: This function allow user to see his/her wish list page wise 
     *
     * @param integer page
     *
     * @return Html Response 
     *
     * @throws NULL
     *
     * @Route("/{_locale}/wish-list",
     * 	name="office_brain_wishlistbundle_wishlist_wishlist"
     * )
     * 
     * @Method({"GET", "POST"})
     * 
     * @Template()
     * 
     */
    public function wishListAction()
    {
        $this->initAction();

        $this->responseArray['record_set'] = $this->wishListManager->getWishListPage($this->page);
        $this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
        $this->responseArray['render_model']=0;
        
        if ($this->request->isXmlHttpRequest() && !count($this->responseArray['record_set']->getItems())) {
            return new JsonResponse(array('status'=>'error', 'error_code'=>'END_OF_PAGINATION'));
        }

        return $this->responseArray;
    }
    
    /**
     *
     * @author Employee Id: 4014
     *
     * Desciption: This function allow user to add desire prooduct to his/her wish list. 
     *
     *  @param integer productId
     *  @param integer cultureProductId
     *
     * @return Html Response 
     *
     * @throws NULL
     *
     * @Route("/{_locale}/add-to-wishlist-product",
     * name="office_brain_wishlistbundle_wishlist_addproducttowishlist",
     *  options={"expose"=true}
     * )
     *
     * @Method({"POST"})
     * 
     * @Template()
     * 
     */
    public function addProductToWishlistAction()
    {
        $this->initAction();
        
        if ($this->productId && $this->cultureProductId) {
            $status = $this->wishListManager->setWishList($this->productId, $this->cultureProductId);
            switch ($status) {
                case 'ERROR_PRODUCT_ALREADY_EXISTS':
                    $this->responseArray['error_message'] = $this->get('translator')->trans('wishlist_product.product_already_exists');
                    break;
                case 'SUCCESS':
                    $this->responseArray['status'] = 'success';
                    $this->responseArray['success_message'] = $this->get('translator')->trans('wishlist_product.add_success_message');
                    
                    $this->refererUrl = $this->commonLibraryManager->getRefererRouteName();
                    break;
            }
        }

        if ($this->refererUrl=='office_brain_wishlistbundle_wishlist_wishlist') {
        	$this->responseArray['page_url'] = $this->generateUrl('office_brain_wishlistbundle_wishlist_wishlist');
        } else {
        	$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
        }
        
        return new JsonResponse($this->responseArray);
    }
  
    /* public function addToWishListAction()
    {
    	$this->initAction();
    	// To check wishlist enable or not
    	$this->checkEnableWishList();
    	$productId = $this->request->get('productId', null);
    	$cultureProductId = $this->request->get('cultureProductId', null);
    	if($productId &&  $cultureProductId){
    		$wishListCount = $this->entityManager->getRepository('OfficeBrainWishListBundle:WishList')->addToWishList($productId, $cultureProductId, $this->userId, $this->cultureId);
    		if($wishListCount===false) {
    			return new JsonResponse ( array( 'product_id'=>$productId, 'status'=>'error', 'error_message'=>$this->get('translator')->trans('wish_list.product_already_exists')) );
    		}
    		else {
    			return new JsonResponse ( array( 'product_id'=>$productId, 'status'=>'success', 'success_message'=>$this->get('translator')->trans('wish_list.add_success_message'), 'total'=>$wishListCount ) );
    		}
    	}
    	return new JsonResponse();
    } */
    
    /**
     *
     * @author Employee Id: 4014
     *
     * Desciption: This function allow user to view recently added prooduct to his/her wish list. 
     *
     * @param integer productId
     * @param integer cultureProductId
     *
     * @return Html Response 
     *
     * @throws NULL
     *
     * @Route("/{_locale}/recent-wish-list",
     * 	name="office_brain_wishlistbundle_wishlist_recentwishlist",
     *  options={"expose"=true}
     * )
     *
     * @Method({"GET"})
     * 
     * @Template()
     * 
     */
    public function recentWishListAction()
    {
        $this->initAction();

        $this->responseArray['record_set'] = $this->wishListManager->getMaxNoOfWishList($this->inlineViewProductMaxLimit);
        $this->responseArray['total_record'] = $this->wishListManager->getTotalCount();

        return $this->responseArray;
    }
    
    /**
     *
     * @author Employee Id: 4471
     *
     * Desciption: This function allow user to delete prooduct from his/her wish list. 
     *
     * @param integer productId
     * @param integer cultureProductId
     *
     * @return Json Response 
     *
     * @throws NULL
     *
     * @Route("/{_locale}/wish-list-delete",
     * 	name="office_brain_wishlistbundle_wishlist_deletewishlist",
     *  options={"expose"=true}
     * )
     *
     * @Method({"POST"})
     * 
     * @Template()
     * 
     */
    public function deleteWishListAction()
    {
        $this->initAction();

        if ($this->productId && $this->cultureProductId) {
            $status = $this->wishListManager->deleteWishList($this->productId, $this->cultureProductId);
            switch ($status) {
                case 'ERROR_NOT_FOUND':
                    $this->responseArray['error_message'] = $this->get('translator')->trans('wish_list.product_not_found');
                    break;
                case 'SUCCESS':
                    $this->responseArray['status'] = 'success';
                    $this->responseArray['success_message'] = $this->get('translator')->trans('wish_list.remove_success_message');
    
                    $this->refererUrl = $this->commonLibraryManager->getRefererRouteName();
                    break;
            }
        }
    
        if ($this->refererUrl=='office_brain_wishlistbundle_wishlist_wishlist') {
        	$this->sessionManager->set('wishlist_success_deleted',$this->get('translator')->trans('wish_list.remove_success_message'));
            $this->responseArray['page_url'] = $this->generateUrl('office_brain_wishlistbundle_wishlist_wishlist');
        } else {
            $this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
        }
    
        return new JsonResponse($this->responseArray);
    }
    
    /**
     *
     * @author Employee Id: 4471
     *
     * Desciption: This function allow user to delete all prooduct from his/her wish list. 
     *
     * @param integer productId
     * @param integer cultureProductId
     *
     * @return Html Response 
     *
     * @throws NULL
     *
     * @Route("/{_locale}/wish-list-delete-all",
     * 	name="office_brain_wishlistbundle_wishlist_deleteallwishlist"
     * )
     *
     * @Method({"POST"})
     * 
     * @Template()
     * 
     */
    public function deleteAllWishListAction()
    { 
    	$this->initAction();
        $status = $this->wishListManager->deleteAllWishList();
        switch ($status) 
        {
        	case 'ERROR_NOT_FOUND':
            	$this->responseArray['error_message'] = $this->get('translator')->trans('wish_list.product_not_found');
                break;
            case 'SUCCESS':
            	$this->responseArray['status'] = 'success';
                $this->responseArray['success_message'] = $this->get('translator')->trans('wish_list.remove_success_message');
                $this->refererUrl = $this->commonLibraryManager->getRefererRouteName();
                break;
        }

        if ($this->refererUrl=='office_brain_wishlistbundle_wishlist_wishlist') {
        	$this->sessionManager->set('wishlist_success_deleted',$this->get('translator')->trans('wish_list.remove_success_message'));
        	$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
            $this->responseArray['page_url'] = $this->generateUrl('office_brain_wishlistbundle_wishlist_wishlist');
        } else {
            $this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
        }
    
        return new JsonResponse($this->responseArray);
                
    }
}
