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

use OfficeBrain\Bundle\CompareProductBundle\Manager\AbstractManager\CompareProductAbstract;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class CompareProductRegisteredExtended extends CompareProductAbstract
{
    public function __construct($entityManager, $request, $sessionManager, $productDetailManager, $emailManager, $twig, $commonLibraryManager, $twigExtension)
    {
        parent::__construct($entityManager, $request, $sessionManager, $productDetailManager, $emailManager, $twig, $commonLibraryManager, $twigExtension);
        $this->currentCulCode = substr($this->cultureCode, strpos($this->cultureCode, "_") + 1);
    }
    
    public function __destruct()
    {
        parent::__destruct();
        unset($this->currentCulCode);
    }
    
    public function setCompareProduct($productId, $cultureProductId)
    {
        $isCompareProductExists = $this->compareProductRepositoryManager->isCompareProductExists($productId, $cultureProductId,$this->currentCulCode);
        
        $pDetail = $this->productDetailManager->getProductDetailById($productId,$this->cultureCode);
        $pstatus = $pDetail[$productId]['currency_for_other_country'];
        $productCountryDetail = implode(",", array_keys($pstatus));
        
        if ($isCompareProductExists) {
            return 'ERROR_PRODUCT_ALREADY_EXISTS';
        } else {
            $this->totalProduct = $this->getTotalCount();

            if ($this->totalProduct >= $this->comapreProductMaxLimit) {
                return 'ERROR_MAX_LIMIT_REACHED';
            } else {
                $this->compareProductRepositoryManager->addToCompareProduct($productId, $cultureProductId,$productCountryDetail);
                return 'SUCCESS';
            }
        }
    }


    public function getTotalCount()
    {
        if (!$this->totalProduct) {
            $this->totalProduct = $this->compareProductRepositoryManager->getTotalCountUserWise($this->currentCulCode);
        }
        return $this->totalProduct;
    }

    public function getCompareProduct()
    {
        $this->totalProduct = $this->getTotalCount();

        if ($this->totalProduct) {
            $this->processPrepareCompareProductDetail();
            return $this->compareProducts;
        } else {
            return null;
        }
    }

    public function processPrepareCompareProductDetail()
    {
        $this->compareProducts = array();
        $this->processPrepareCompareProductId();

        if (is_array($this->productIdArray) && count($this->productIdArray)) {
            $productIds = json_encode($this->productIdArray);
            $this->compareProducts = $this->getProductDetailById(json_encode($this->productIdArray), $this->cultureCode);
        }
        unset($productIds);
    }
    
    public function processPrepareCompareProductId()
    {
        $recordSet = $this->compareProductRepositoryManager->getCompareProduct($this->currentCulCode);
        if (is_array($recordSet) && count($recordSet)) {
            foreach ($recordSet as $row) {
                $this->productIdArray[] = $row['productId'];
                $this->cultureProductIdArray[] = $row['cultureProductId'];
            }
            unset($recordSet);
        }
    }

    public function deleteCompareProduct($productId, $cultureProductId)
    {
        $isDeleted = $this->compareProductRepositoryManager->deleteCompareProduct($productId, $cultureProductId);

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

        if ($isDeleted) {
            $this->totalProduct = null;
            return 'SUCCESS';
        }
        return 'ERROR_NOT_FOUND';
    }

    public function checkProductExistInCompare($productId, $cultureProductId)
    {

        $isCompareProductExists = $this->compareProductRepositoryManager->isCompareProductExists($productId, $cultureProductId,$this->currentCulCode);
         if ($isCompareProductExists) {
            return true;
        }else{
            return false;
        }

    }
}
