<?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;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

/**
 *
 * @author OfficeBrain 410175 <info@officebrain.com>
 *
 * Desciption: This controller help user to manage his/her wish list
 * 
 * @package: OfficeBrain eCommerce Product Web Client
 * 
 * @subpackage: Wish List Mobile Api
 * 
 * @Route("/api/v1")
 *  
 * @version 1.0 
 * 
 */
class ApiWishListController extends WishListController
{
    /**
     *
     * @author Employee Id: 410175
     *
     * Desciption: Define member variables with thier default value
     *
     * @param NULL
     * 
     * @return NULL
     * 
     * @throws NULL
     * 
     */
    public function __construct()
    {
    	parent::__construct();
        $this->responseFormat = null;
    }
    
    /**
     *
     * @author Employee Id: 410175
     *
     * Desciption: Unset member variables
     *
     * @param NULL
     * 
     * @return NULL
     * 
     * @throws NULL
     * 
     */
    public function __destruct()
    {
    	parent::__destruct();
        unset($this->responseFormat);
    }
    
    /**
     *
     * @author Employee Id: 410175
     *
     * Desciption: Create and initialize various required object and member variables
     *
     * @param NULL
     *
     * @return NULL
     *
     * @throws NULL
     *
     */
    public function initAction()
    {
    	parent::initAction();
        $this->responseFormat = $this->request->get('_format', null) ;
        $this->translator=$this->get('translator');
        
    	if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
        	throw new AccessDeniedException();
        }
    }
	/*
     *
     * @author Employee Id: 410175
     *
     * 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_api"
     * )
     * 
     * @ApiDoc(
     *      resource=true,
     *      section="Wish List",
     *      description="Get Wish list data",
     *      requirements={
     *      	{"name"="_locale","default"="en_us", "dataType"="string", "description"="en_us"},
     *      	{"name"="access_token", "dataType"="string", "required"=true, "description"="valid access token"},
     *     		{"name"="page","default"="1", "dataType"="integer", "description"="page number"}
     *      },
     *      statusCodes={
     *              1="Returned when success",
     *              403="Returned when the user is not authorized to see this link",
     *              0="Returned when form validation is failed",
     *      }
     * )
     * 
     * @Method({"GET"})
     * 
     * @Template()
     * 
     * /
    public function wishListAction()
    {
    	$this->initAction();
    	$data = parent::wishListAction();    	
    	if( $data instanceof JsonResponse ){    		
    		$dataAry = json_decode($data->getContent(),true);
    		return array('status'=>'error', 'status_code'=>0, 'message'=>$dataAry['error_code']);
    	}
    	
    	if($this->responseFormat == 'json'){    		
    		$newData = array();
    		$newData['items'] = $data['record_set']->getItems();    		
    		$newData['pagination'] = $data['record_set']->getPaginationData();
	    	if(isset($newData['pagination']['current']) ){
	        	$newData['pagination']['current'] = (int)$newData['pagination']['current'];
	        }
    		$newData['total_record'] = $data['total_record'];
    		$newData['items'] = array_values($newData['items']);
    		if($newData['items'] && count($newData['items'])>0){
    			foreach ($newData['items'] as $k=>$item){
	    			if(isset($item['pricing']['decorative']) && count($item['pricing']['decorative'])>0){
			        	foreach ($item['pricing']['decorative'] as $priceKey=>$priceData){		        		
			        		if($priceData['to']=='')$newData['items'][$k]['pricing']['decorative'][$priceKey]['to']=0;
			        		if($priceData['from']=='')$newData['items'][$k]['pricing']['decorative'][$priceKey]['from']=0;
			        		if(isset($priceData['rate']))$newData['items'][$k]['pricing']['decorative'][$priceKey]['rate'] = (double)$priceData['rate'];
			        		if(isset($priceData['rate_orig']))$newData['items'][$k]['pricing']['decorative'][$priceKey]['rate_orig'] = (double)$priceData['rate_orig'];
			        	}
			        }
					if(isset($item['pricing']['blank']) && count($item['pricing']['blank'])>0){
			        	foreach ($item['pricing']['blank'] as $priceKey=>$priceData){		        		
			        		if($priceData['to']=='')$newData['items'][$k]['pricing']['blank'][$priceKey]['to']=0;
			        		if($priceData['from']=='')$newData['items'][$k]['pricing']['blank'][$priceKey]['from']=0;
			        		if(isset($priceData['rate']))$newData['items'][$k]['pricing']['blank'][$priceKey]['rate'] = (double)$priceData['rate'];
			        		if(isset($priceData['rate_orig']))$newData['items'][$k]['pricing']['blank'][$priceKey]['rate_orig'] = (double)$priceData['rate_orig'];
			        	}
			        }
			        
					if(isset($item['pricing_for_other_country'])){
						foreach ($item['pricing_for_other_country'] as $countryName=>$priceType){						
							foreach ($priceType as $typeKey=>$typeData){						
								foreach ($typeData as $priceKey=>$priceData){								
									if($priceData['to']=='')$newData['items'][$k]['pricing_for_other_country'][$countryName][$typeKey][$priceKey]['to']=0;
			        				if($priceData['from']=='')$newData['items'][$k]['pricing_for_other_country'][$countryName][$typeKey][$priceKey]['from']=0;
			        				if(isset($priceData['rate']))$newData['items'][$k]['pricing_for_other_country'][$countryName][$typeKey][$priceKey]['rate'] = (double)$priceData['rate'];
			        				if(isset($priceData['rate_orig']))$newData['items'][$k]['pricing_for_other_country'][$countryName][$typeKey][$priceKey]['rate_orig'] = (double)$priceData['rate_orig'];
								}
							}
			        	}
			        }
			         if(isset($item['images']) && count($item['images'])>0){
				        foreach ($item['images'] as $iKey=>$imgData){
			        		foreach ($imgData as $imgKey=>$img){
				        		if($img==""){
				        			$newData['items'][$k]['images'][$iKey][$imgKey] = null;
				        		}else if(is_numeric($img)){
				        			$newData['items'][$k]['images'][$iKey][$imgKey] = (double)$img;
				        		}
			        		}
			        	}
			        } 
    			}
    		}else{
    			$newData['items'] = null;
    		}
    		
    		return array('status'=>'success', 'status_code'=>1, 'data'=>$newData);
    	} 
    	return $data;       
    }
 	/**
     *
     * @author Employee Id: 410175
     *
     * 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_api")
     * 
     * @ApiDoc(
     *      resource=true,
     *      section="Wish List",
     *      description="Add Product to Wish list ",
     *      requirements={
     *      	{"name"="_locale","default"="en_us", "dataType"="string", "description"="en_us"},
     *      	{"name"="access_token", "dataType"="string", "required"=true, "description"="valid access token"},
     *      	{"name"="productId", "dataType"="integer","description"="enter product id","required"=true},
     *      	{"name"="cultureProductId", "dataType"="integer","description"="enter culture id","required"=true},    
     *      },
     *      statusCodes={
     *              1="Returned when success",
     *              403="Returned when the user is not authorized to see this link",
     *              0="Returned when form validation is failed",
     *      }
     * )
     * 
     * @Method({"POST"})
     * 
     * @Template()
     * 
     * /
    public function addProductToWishlistAction()
    {
    	$this->initAction();
    	$product_id=$this->request->get('productId',null);
    	$culture_product_id=$this->request->get('cultureProductId',null);
    	if($product_id == null || $product_id<=0)
    	{
    		$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
    		return array('status'=>'error','status_code'=>0,'message' => $this->translator->trans('wishlist_product.enter_product_id'));    		
    	}
    	if($culture_product_id == null || $culture_product_id<=0)
    	{
    		$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
    		return array('status'=>'error','status_code'=>0, 'message' => $this->translator->trans('wishlist_product.enter_culture_product_id'));
    	}
    	 if ($product_id && $culture_product_id) 
    	 {     	 	
    	 	$status = $this->wishListManager->setWishList($product_id, $culture_product_id);
    	 	
    	 	switch ($status) 
    	 	{ 
    	 		case 'ERROR_PRODUCT_ALREADY_EXISTS': 
    	 			if($this->responseFormat == 'json')
    	 			{   
    	 				$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
    	 				return array('status' => 'error', 'status_code' => 0, 'message' => $this->translator->trans('wishlist_product.product_already_exists'));
    	 			}
    	 			 
    	 		break; 
    	 		case 'SUCCESS': 

    	 			if($this->responseFormat == 'json')
    	 			{
    	 				$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
    	 				return array('status' => 'success', 'status_code' => 1, 'data'=>array('total_record'=> $this->responseArray['total_record']));
    	 			} 
    	 			break; 
    	 	} 
    	 } 
    	 return new JsonResponse($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_api", 
     * options={"expose"=true}
     * )
     *@ApiDoc(
     *      resource=true,
     *      section="Wish List",
     *      description="Delete Product to Wish list ",
     *      requirements={
     *      	{"name"="_locale","default"="en_us", "dataType"="string", "description"="en_us", "required"=true},
     *      	{"name"="access_token", "dataType"="string", "required"=true, "description"="valid access token"},
     *      	{"name"="productId", "dataType"="integer","description"="enter product id","required"=true},
     *      	{"name"="cultureProductId", "dataType"="integer","description"="enter culture id","required"=true},    
     *      },
     *      statusCodes={
     *              1="Returned when success",
     *              403="Returned when the user is not authorized to see this link",
     *              0="Returned when form validation is failed",
     *      }
     * )
     * 
     * @Method({"POST"})
     *
     * @Template()
     *
     * /
    public function deleteWishListAction()
    {
    	$this->initAction();
    	$product_id=$this->request->get('productId',null);
    	$culture_product_id=$this->request->get('cultureProductId',null);
    	if($product_id == null || $product_id<=0)
    	{
    		$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
    		return array('status'=>'error','status_code'=>0,'message' => $this->translator->trans('wishlist_product.enter_product_id'));    		
    	}
    	if($culture_product_id == null || $culture_product_id<=0)
    	{
    		$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
    		return array('status'=>'error','status_code'=>0, 'message' => $this->translator->trans('wishlist_product.enter_culture_product_id'));
    	}
    
    	if ($product_id && $culture_product_id) {
    		
    		$status = $this->wishListManager->deleteWishList($this->productId, $this->cultureProductId);
    		switch ($status) {
    			case 'ERROR_NOT_FOUND':
    				if($this->responseFormat == 'json')
    				{
    					$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
    					return array('status' => 'error', 'status_code' => 0, 'message' => $this->translator->trans('wish_list.product_not_found'));
    				}    				
    				break;
    			case 'SUCCESS':
    				if($this->responseFormat == 'json')
    				{
    					$this->responseArray['total_record'] = $this->wishListManager->getTotalCount();
    					return array('status' => 'success', 'status_code' => 1,'data'=>array('total_record'=> $this->responseArray['total_record']));
    				}
    				break;
    		}
    	}    	 
    	return new JsonResponse($this->responseArray);
    }*/
}