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

use OfficeBrain\Bundle\WishListBundle\Manager\AbstractManager\WishListAbstract;

/**
 *
 * @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 WishListRegisteredExtended 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);
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created to override the add wishlist product functionality defined in abstract class
    */
    public function setWishList($productId, $cultureProductId)
    {
        $isWishListExists = $this->wishListRepositoryManager->isWishListExists($productId, $cultureProductId,$this->currentCulCode);

        
        $pDetail = $this->productDetailManager->getProductDetailById($productId,$this->cultureCode);
        $pstatus = $pDetail[$productId]['currency_for_other_country'];
        $productCountryDetail = implode(",", array_keys($pstatus));
        
        if ($isWishListExists) {
            return 'ERROR_PRODUCT_ALREADY_EXISTS';
        } else {
            $this->wishListRepositoryManager->addWishList($productId, $cultureProductId,$productCountryDetail);
        }
        return 'SUCCESS';
        
         
         
        /* Add Record into the database if any product exist into the session */
        #TODO: need to change the static locale variable
        /* $data = $this->request->get('project_setting');
        $locale = "en_us";
        $wishlistInfo = $this->getWishListFromSession();
        if(count($wishlistInfo)>0)
        {
            $keys = array_keys($wishlistInfo[$locale]);
            for($i=0; $i<count($keys); $i++)
            {
                $keyValue		=	$keys[$i];
                $productDetail	=	$wishlistInfo[$locale][$keyValue];
                $productWishlistData = $this->prepareWishListAddData($productDetail);
                $this->wishListRepositoryManager->addWishList($productWishlistData);
            }
        } */
    }
    
    /**
     * @author Employee Id: 4248
     * OB eCommerce Product - Wishlist
     * Function created to override the get wishlist product functionality defined in abstract class
     * /
    public function getWishList(){
        
        $wishlistInfo = $this->getWishListOfUser();
        return $wishlistInfo;
    }*/

    
    public function getTotalCount()
    {
        $this->totalProduct = $this->wishListRepositoryManager->getTotalCountUserWise($this->currentCulCode);
        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)
    {
        $recordSet = $this->wishListRepositoryManager->getMaxNoOfProduct($maxNo,$this->currentCulCode);
        if (is_array($recordSet) && count($recordSet)) {
            foreach ($recordSet as $row) {
                $this->productIdArray[] = $row['productId'];
                $this->cultureProductIdArray[] = $row['cultureProductId'];
            }
            unset($productsDetail);
        }
    }
    
    public function getWishList()
    {
        $this->totalProduct = $this->getTotalCount();
    
        if ($this->totalProduct) {
            $this->getWishListId();
            $this->processPrepareWishListDetail();
            return $this->wishListProducts;
        } else {
            return null;
        }
    }
    
    public function getWishListPage($page)
    {
        $this->totalProduct = $this->getTotalCount();
        $this->getWishListIdPage($page);
        $this->processPrepareWishListDetail();
        $this->paginatorRecordSet->setItems($this->wishListProducts);
        return $this->paginatorRecordSet;
    }

    public function getWishListIdPage($page)
    {
    	//echo $this->cultureCode;die;
        $this->paginatorRecordSet = $this->wishListRepositoryManager->getWishListPage($page, $this->configurationRule['list']['option']['record_per_page'],$this->currentCulCode);
        $this->totalProduct = $this->paginatorRecordSet->getTotalItemCount();
        
        if ($this->paginatorRecordSet->getTotalItemCount()) {
            foreach ($this->paginatorRecordSet->getItems() as $row) {
                $this->productIdArray[] = $row['productId'];
                $this->cultureProductIdArray[] = $row['cultureProductId'];
            }
        }
    }

    public function getWishListId()
    {
        $productsDetail = $this->wishListRepositoryManager->findAllProduct($this->currentCulCode);
        if (is_array($productsDetail) && count($productsDetail)) {
            foreach ($productsDetail as $idArray) {
                $this->productIdArray[] = $idArray['productId'];
                $this->cultureProductIdArray[] = $idArray['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 deleteWishList($productId, $cultureProductId)
    {
        $isDeleted = $this->wishListRepositoryManager->deleteWishList($productId, $cultureProductId);

        if ($isDeleted) {
            $this->totalProduct = null;
            return 'SUCCESS';
        }
        return 'ERROR_NOT_FOUND';
    }
    
    public function deleteAllWishList()
    {
    	$isDeleted = $this->wishListRepositoryManager->deleteAllWishList($this->currentCulCode);
        if ($isDeleted) {
            $this->totalProduct = null;
            return 'SUCCESS';
        }
        return 'ERROR_NOT_FOUND';
    }

    public function checkProductExist($productId, $cultureProductId)
    {   
        
        $isWishListExists = $this->wishListRepositoryManager->isWishListExists($productId, $cultureProductId,$this->currentCulCode);
        if ($isWishListExists) {
            return true;
        }else{
            return false;
        }

    }
}
