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

use OfficeBrain\Bundle\WishListBundle\Manager\AbstractManager\WishListAbstract;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;

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

class WishListGuestExtended extends WishListAbstract
{
    /**
     * @author Employee Id: 4248
     * Initialize the required variable on class load
     *
    */
    public function __construct($entityManager, $request, $session, $productDetailManager, $paginatorManager)
    {
        parent::__construct($entityManager, $request, $session, $productDetailManager, $paginatorManager);
        $this->currentCulCode = substr($this->cultureCode, strpos($this->cultureCode, "_") + 1);
    }
    
    public function __destruct()
    {
        parent::__destruct();
        unset($this->currentCulCode);
    }
    
    public function setWishList($productId, $cultureProductId)
    {
        $productKey = $productId.':'.$cultureProductId;
        
        $pDetail = $this->productDetailManager->getProductDetailById($productId,$this->cultureCode);
        $pstatus = $pDetail[$productId]['currency_for_other_country'];
        $productCountryDetail = implode(",", array_keys($pstatus));
        $productDefaultCountry = explode("_", $pDetail[$productId]["culture"]);
        if(!$productCountryDetail){
            $productCountryDetail = $productDefaultCountry[1];
        }
        
        if (array_key_exists($productKey, $this->sessionProductId[$this->sessionInstanceKey])) {
            return 'ERROR_PRODUCT_ALREADY_EXISTS';
        } else {
            $this->sessionProductId[$this->sessionInstanceKey][$productKey] = array('productId'=>$productId, 'cultureProductId'=>$cultureProductId, 'productCalture'=>$productCountryDetail);
            $this->sessionManager->set('wish-list-id', serialize($this->sessionProductId));
            $this->sessionProductId = unserialize($this->sessionManager->get('wish-list-id'));
    
            unset($productId);
            unset($productKey);
            unset($cultureProductId);
    
            return 'SUCCESS';
        }
    }

    public function getTotalCount()
    {
       /*  if (!$this->totalProduct) {
            $this->totalProduct = count($this->sessionProductId[$this->sessionInstanceKey]);
        } */
        if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey]) > 0) {
        	foreach ($this->sessionProductId[$this->sessionInstanceKey] as $productInfo) {
        		$pCalture=explode(',',$productInfo['productCalture']);
        		if(in_array($this->currentCulCode,$pCalture)){
        			$totalProducts[] = $productInfo['productId'];
        		}
        	}
        	unset($productInfo);
        	unset($sessionProductId);
        }
        
        if (!$this->totalProduct) {
        	$this->totalProduct = count($totalProducts);
        }
        return $this->totalProduct;
    }

    public function getMaxNoOfWishList($maxNo)
    {
        $this->totalProduct = $this->getTotalCount();
    
        if ($this->totalProduct) {
            $this->getMaxNoOfWishListId($maxNo);
            $this->processPrepareWishListDetail();
            return $this->wishListProducts;
        } else {
            return null;
        }
    }

    public function getMaxNoOfWishListId($maxNo)
    {
        if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey]) > 0) {
            $productsDetail = array_slice($this->sessionProductId[$this->sessionInstanceKey], 0, $maxNo);
            if (is_array($productsDetail) && count($productsDetail)) {
                foreach ($productsDetail as $idArray) {
                	$pCalture=explode(',',$idArray['productCalture']);
                	if(in_array($this->currentCulCode,$pCalture)){
	                    $this->productIdArray[] = $idArray['productId'];
	                    $this->cultureProductIdArray[] = $idArray['cultureProductId'];
                	}
                }
                unset($productsDetail);
            }
        }
    }

    public function getWishList()
    {
        $this->totalProduct = $this->getTotalCount();
        if ($this->totalProduct) {
            $this->processPrepareWishListId();
            $this->processPrepareWishListDetail();
            return $this->wishListProducts;
        } else {
            return null;
        }
    }
    
    public function getWishListPage($page)
    {
        $this->totalProduct = $this->getTotalCount();
        $this->getWishListIdPage($page);
        $this->processPrepareWishListDetail();
        
        return $this->paginatorManager->paginate($this->wishListProducts, $page, $this->configurationRule['list']['option']['record_per_page']);
    }
    
    public function getWishListIdPage($page)
    {
        if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey]) > 0) {
            $startIndex = ($page-1)* (int) $this->configurationRule['list']['option']['record_per_page'];
            $wishListPage = array_slice($this->sessionProductId[$this->sessionInstanceKey], $startIndex, $this->configurationRule['list']['option']['record_per_page']);
        
            foreach ($this->sessionProductId[$this->sessionInstanceKey] as $productInfo) {
            	$pCalture=explode(',',$productInfo['productCalture']);
            	if(in_array($this->currentCulCode,$pCalture)){
	                $this->productIdArray[] = $productInfo['productId'];
	                $this->cultureProductIdArray[] = $productInfo['cultureProductId'];
            	}
            }
            unset($startIndex);
            unset($productInfo);
            unset($wishListPage);
            unset($sessionProductId);
        }
        /*
    	$this->processPrepareWishListIdPage($page);
    	$this->processPrepareWishListDetail();
    	//$this->paginatorRecordSet = $this->wishListRepositoryService->getWishListPage($page, $this->configurationRule['list']['option']['record_per_page']);
    	
    	$this->paginatorRecordSet = $this->paginatorService->paginate($this->wishListProducts, $page, $this->configurationRule['list']['option']['record_per_page']);
    	
    	$this->totalProduct = $this->paginatorRecordSet->getTotalItemCount();
    
    	if ($this->paginatorRecordSet->getTotalItemCount()) {
    		foreach ($this->paginatorRecordSet->getItems() as $row) {
    			$this->productIdArray[] = $row['productId'];
    			$this->cultureProductIdArray[] = $row['cultureProductId'];
    		}
    		//unset($productsDetail);
    	}
    	*/
    }

    public function processPrepareWishListDetail()
    {
        if (is_array($this->productIdArray) && count($this->productIdArray)) {
            $productIds = json_encode($this->productIdArray);
            $this->wishListProducts = $this->getProductDetailById(json_encode($this->productIdArray), $this->cultureCode);
        }
        unset($productIds);
    }
    
    public function processPrepareWishListId()
    {
        if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey]) > 0) {
            foreach ($this->sessionProductId[$this->sessionInstanceKey] as $productInfo) {
            	$pCalture=explode(',',$productInfo['productCalture']);
            	if(in_array($this->currentCulCode,$pCalture)){
	                $this->productIdArray[] = $productInfo['productId'];
	                $this->cultureProductIdArray[] = $productInfo['cultureProductId'];
            	}
            }
            unset($productInfo);
            unset($sessionProductId);
        }
    }
    
    public function deleteWishList($productId, $cultureProductId)
    {
        if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey])) {
            //$productKey = $productId.':'.$cultureProductId;
        	$productKey = $productId.':'.$productId;
            if (array_key_exists($productKey, $this->sessionProductId[$this->sessionInstanceKey])) {
                unset($this->sessionProductId[$this->sessionInstanceKey][$productKey]);
                $this->sessionManager->set('wish-list-id', serialize($this->sessionProductId));
                $returnStatus = 'SUCCESS';
            } else {
                $returnStatus = 'ERROR_NOT_FOUND';
            }
        } else {
            $returnStatus = 'ERROR_NOT_FOUND';
        }
        unset($productId);
        unset($productKey);
        unset($cultureProductId);
        
        return $returnStatus;
    }
    
    public function deleteAllWishList()
    {
        /* if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey])) {
            $this->sessionProductId[$this->sessionInstanceKey] = array();
            $this->sessionManager->set('wish-list-id', serialize($this->sessionProductId));
            return 'SUCCESS';
        } else {
            return 'ERROR_NOT_FOUND';
        } */
        if (is_array($this->sessionProductId[$this->sessionInstanceKey]) && count($this->sessionProductId[$this->sessionInstanceKey]) > 0) {
        	foreach ($this->sessionProductId[$this->sessionInstanceKey] as $productInfo) {
        		$pCalture = explode(',',$productInfo['productCalture']);
        		if(in_array($this->currentCulCode,$pCalture)){
        			$quickQuoteKey = $productInfo['productId'].':'.$productInfo['cultureProductId'];
        			unset($this->sessionProductId[$this->sessionInstanceKey][$quickQuoteKey]);
        			$this->sessionManager->set('wish-list-id', serialize($this->sessionProductId));
        		}
        	}
        	return 'SUCCESS';
        }
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created to override the get wishlist product functionality defined in abstract class
    * /
    public function setWishList($productId, $cultureProductId){
        #TODO: Need to change static lacale and static product detail
        $locale = 'en_us';
        // $productData = $this->productDetailService->getProductDetail('1-flexible-magnet','en');
        $productData = $this->productDetailService->getProductDetailById($productId,'en');
        
        if(count($productData[$productId]['attributes'])>0){
            $productDetail = $this->prepareProductDetail($productData[$productId]);
        }else {
            $productDetail = $productData[$productId];
        }
        
        if($this->getSessionParameter('wishListProduct')){
            $wishlistInfo = $this->getSessionParameter('wishListProduct');
            if(isset($wishlistInfo[$locale]) && array_key_exists($this->getHash($productDetail),$wishlistInfo[$locale])){
                $flag=false;
            }else{
                $wishlistInfo[$locale][$this->getHash($productDetail)] = $productDetail;
                $flag=true;
            }
        }else {
            $wishlistInfo[$locale] = array();
            $wishlistInfo[$locale][$this->getHash($productDetail)] = $productDetail;
            $flag=true;
        }
        $this->setSessionParameter('wishListProduct', $wishlistInfo);
        die;
    }
    */

    public function checkProductExist($productId, $cultureProductId)
    { 

        $productKey = $productId.':'.$cultureProductId;
        if (array_key_exists($productKey, $this->sessionProductId[$this->sessionInstanceKey])) {
            return true;
        } else {
            return false;
        }
    }
}
