<?php
namespace OfficeBrain\Bundle\WishListBundle\Manager\AbstractManager;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 *
 * @author Employee Id: 4248
 * OB eCommerce Product - WishList
 * WishListAbstract Class Add to define the common function and logic
 * - List All the WishList Product
 * - Add New Product to Wishlist
 * - Delete Product from Wishlist
 */

abstract class WishListAbstract
{
    protected $entityManager;
    protected $session;
    protected $request;
    protected $productDetailService;
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $userId;
    protected $cultureId;
    protected $cultureCode;
    protected $configurationRule;
    protected $inlineViewProductMaxLimit;
    protected $createdUid;
    protected $upatedUid;
    protected $totalProduct;
    protected $sessionProductId;
    protected $sessionInstanceKey;
    protected $wishListProducts;
    protected $wishListRepositoryService;
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product -
     * Initialize the required variable on class load
     */
    public function __construct($entityManager, $request, $session,  $productDetailManager, $paginatorManager)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->sessionManager = $session;
        $this->productDetailManager = $productDetailManager;
        $this->paginatorManager = $paginatorManager;
    
        $this->init();
        
        $this->projectSetting = $this->request->get('project_setting');
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $this->configurationRule = $this->projectSetting['configuration_rule']['transaction']['wish_list'];
        $this->inlineViewProductMaxLimit = $this->configurationRule['inline_view']['option']['no_of_product'];
        $this->createdUid = $this->userId;
        $this->upatedUid =$this->userId;
        
        $this->viewType = null;
        $this->totalProduct = null;
        $this->wishListProducts = array();
        $this->sessionProductId = array();
        $this->productIdArray = array();
        $this->cultureProductIdArray = array();
        
        $this->sessionInstanceKey = $this->instanceId.'-'.$this->cultureId;
    
        if ($this->sessionManager->has('wish-list-id')) {
            $this->sessionProductId = unserialize($this->sessionManager->get('wish-list-id'));
        }

        if (!array_key_exists($this->sessionInstanceKey, $this->sessionProductId)) {
            $this->sessionProductId[$this->sessionInstanceKey] = array();
        }

        $this->wishListRepositoryManager = $this->wishListRepository = $this->entityManager->getRepository('OfficeBrainWishListBundle:WishList');
        $this->wishListRepositoryManager->setPaginatorService($this->paginatorManager);
        $this->wishListRepositoryManager->setProjectSetting($this->projectSetting);
        $this->wishListRepositoryManager->setPaginatorService($this->paginatorManager);
    }
    
    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->sessionManager);
        unset($this->productDetailManager);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->configurationRule);
        unset($this->inlineViewProductMaxLimit);
        unset($this->createdUid);
        unset($this->upatedUid);
        unset($this->wishListProducts);
        unset($this->totalProduct);
        unset($this->sessionProductId);
        unset($this->sessionInstanceKey);
        unset($this->paginatorManager);
        unset($this->wishListRepositoryManager);
    }
    
    public function init()
    {
        $this->prepareProjectSetting();
    }
    
    public function prepareProjectSetting()
    {
    	
    }
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created for define a common logic for add wishlist product
    */
    public function setWishList($productId, $cultureProductId)
    {
    }

    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created for define a common logic for getting all wishlist product
    */
    public function getWishList()
    {
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created for define a common logic for getting wish list page wise
    */
    public function getWishListPage($page)
    {
    }

    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created for define a common logic for delete wishlist product
    */
    public function deleteWishList($productId, $cultureProductId)
    {
    }
    
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created for define a common logic for add wishlist product into the Quote
    */
    public function setWishListInQuote()
    {
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created for define a common logic for delete wishlist product into the Quote
    */
    public function deleteAllWishList()
    {
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created for define a common logic for getting the recent 4 added product
    */
    public function getLatestFourWishListInCart()
    {
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Get Product Info from the session
     */
    public function getSessionParameter($parameter_name)
    {
        return $this->sessionManager->get($parameter_name);
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Set Product Info into the session
     */
    public function setSessionParameter($parameter_name, $value)
    {
        return $this->sessionManager->set($parameter_name, $value);
    }

    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created to prepare the required product detail for twig
     */
    public function prepareProductDetail($productDetail)
    {
        $product=$productDetail;
        /* if($productDetail['attributes']['quantity']!='' && $productDetail['attribute']['quantity']>0){
         $product=$this->getProductInfo($productDetail,$productDetail['attribute']['quantity']);
        unset($product['attribute']['quantity']);
        }else{
        unset($product['attribute']['quantity']);
        }*/
        if (count($product['attributes'])==0) {
            $product['attributes']=array();
        } else {
            $product=$this->getProductAttributeInfo($product);
        }
        return $product;
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created to get the product Attribute
     */
    public function getProductAttributeInfo($productDetail)
    {
        if (count($productDetail['attributes'])>0 && array_key_exists('attributes', $productDetail)) {
            if (array_key_exists('Color', $productDetail['attributes'])) {
                if (array_key_exists('values', $productDetail['attributes']['Color'])) {
                    if (array_key_exists($productDetail['attributes']['Color'], $productDetail['attributes']['Color']['values'])) {
                        $productDetail['attributes']['Color']=$productDetail['attributes']['Color']['values'][$productDetail['attributes']['Color']];
                    }
                }
            }
            if (array_key_exists('Size', $productDetail['attributes'])) {
                if (array_key_exists('values', $productDetail['attributes']['Size'])) {
                    if (array_key_exists($productDetail['attributes']['Size'], $productDetail['attributes']['Size']['values'])) {
                        $productDetail['attributes']['Size']=$productDetail['attributes']['Size']['values'][$productDetail['attributes']['Size']];
                    }
                }
            }
        } else {
            $productDetail['attributes']=array();
        }
    
        if (isset($productDetail['attributes']['Color']) && $productDetail['attributes']['Color']=='') {
            unset($productDetail['attributes']['Color']);
        }
        if (isset($productDetail['attributes']['Size']) && $productDetail['attributes']['Size']=='') {
            unset($productDetail['attributes']['Size']);
        }
    
        return $productDetail;
    }

    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created to override the get wishlist product functionality defined in abstract class
     */
    public function getWishListFromSession()
    {
        #TODO: Need to change static lacale and commented product slug
        $wishlistInfo = array();
        if ($this->getSessionParameter('wishListProduct')) {
            $wishlistInfo = $this->getSessionParameter('wishListProduct');
            return $wishlistInfo;
        } else {
            $wishlistInfo = "";
        }
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created to prepate the wishlist data to be inserted
    */
    public function prepareWishListAddData($productId, $cultureProductId)
    {
        $wishList = array();
        $wishList['product_id']        = $productId;
        $wishList['culture_product_id'] = $cultureProductId;
        $wishList['culture_id']        = $this->cultureId;
        $wishList['user_id']            = $this->userId;
        $wishList['created_uid']        = $this->userId;
        $wishList['instance_id']        = $this->instanceId;
        $wishList['instance_type']        = $this->instanceType;
        return $wishList;
    }
    
    public function getProductDetailBySlug($productSlug, $culture)
    {
        return $this->productDetailManager->getProductDetailBySlug($productSlug, $culture);
    }
    
    public function getProductDetailById($productId, $culture)
    {
        return $this->productDetailManager->getProductDetailById($productId, $culture);
    }
}
