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

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;

abstract class ShippingTaxAbstract
{
    protected $entityManager;
    protected $request;
    protected $productDetailService;
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $userId;
    protected $cultureId;
    protected $cultureCode;
    protected $commonLibraryManager;
    protected $twig;
    protected $twigExtension;
    
    /**
     * @author Employee Id: 4203
     * OB eCommerce Product - Requestinfo
     * Initialize the required variable on class load
     */
    public function __construct($entityManager, $request, $productDetailManager, $twig, $commonLibraryManager,$twigExtension,$userContactBookManager,$coreManager,$session,$paginatorManager)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->productDetailManager = $productDetailManager;
        $this->commonLibraryManager = $commonLibraryManager;
        $this->twig = $twig;
        $this->twigExtension=$twigExtension;
        $this->userContactBookManager=$userContactBookManager;
        $this->coreManager = $coreManager;
        $this->sessionManager=$session;
        $this->paginatorManager=$paginatorManager;
        $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']['tax_management'];
        $this->taxMasterRepositoryManager = $this->entityManager->getRepository('OfficeBrainTaxBundle:TaxMaster');
        $this->taxMasterRepositoryManager->setProjectSetting($this->projectSetting);
        $this->taxMasterRepositoryManager->setPaginatorService($this->paginatorManager);
        
        $this->taxMappingRepositoryManager = $this->entityManager->getRepository('OfficeBrainTaxBundle:TaxMapping');
        $this->taxMappingRepositoryManager->setProjectSetting($this->projectSetting);
        $this->taxMappingRepositoryManager->setPaginatorService($this->paginatorManager);;
       
    }
    
    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->productDetailManager);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->taxMasterRepositoryManager);
        unset($this->taxMappingRepositoryManager);
        unset($this->commonLibraryManager);
        unset($this->twig);
    }
    
    
    /**
     * @author Employee Id: 4471
     * OB eCommerce Product - TAX
     * To get the product detail By Id
     * @param integer productId
     * @param srting culture
     * return productDetailService array
     */
    public function getProductDetailById($productId=null, $cultureCode=null){
    	if( $productId == null ){  $productId = $this->productId; }
    	if( $cultureCode == null ){  $cultureCode = $this->cultureCode; }
    	$result =  $this->productDetailManager->getProductDetailById($productId, $cultureCode);
    	if(!$result)return null;
    	if(is_array(json_decode($productId))){
    		return $result;
    	}else{
    		return $result[$productId];
    	}
    }
    
    public function getShippingTax ($countryId,$stateId,$productCat,$productId) {
    
    	$shippingTax= array();
    	if($this->configurationRule['status']==1)
    	{
    		if($this->configurationRule['option']['shipping_tax']['status']==1)
    		{
    			$confiShippingTax=$this->configurationRule['option']['shipping_tax']['option'];
    			$stateTaxConfi = $confiShippingTax['state']['status'];
    			$catTaxConfi = $confiShippingTax['category']['status'];
    			$productTaxConfi = $confiShippingTax['product']['status'];
    			if($stateTaxConfi ==1 &&  $catTaxConfi == 0 && $productTaxConfi == 0)
    			{
    				$shippingTax= $this->taxMappingRepositoryManager->getShippingTax($countryId,$stateId,null,null);
    			}
    			else if($stateTaxConfi == 0 &&  $catTaxConfi == 1 && $productTaxConfi == 0)
    			{
    				$shippingTax= $this->taxMappingRepositoryManager->getShippingTax($countryId,null,$productCat,null);
    			}
    			else if($stateTaxConfi == 0 &&  $catTaxConfi == 0 && $productTaxConfi == 1)
    			{
    				$shippingTax= $this->taxMappingRepositoryManager->getShippingTax($countryId,null,null,$productId);
    			}
    			else if($stateTaxConfi == 1 &&  $catTaxConfi == 1 && $productTaxConfi == 0)
    			{
    				$shippingTax= $this->taxMappingRepositoryManager->getShippingTax($countryId,$stateId,$productCat,null);
    			}
    			else if($stateTaxConfi == 1 &&  $catTaxConfi == 0 && $productTaxConfi == 1)
    			{
    				$shippingTax= $this->taxMappingRepositoryManager->getShippingTax($countryId,$stateId,null,$productId);
    			}
    			else if($stateTaxConfi == 0 &&  $catTaxConfi == 1 && $productTaxConfi == 1)
    			{
    				$shippingTax= $this->taxMappingRepositoryManager->getShippingTax($countryId,null,$productCat,$productId);
    			}
    			else if($stateTaxConfi == 1 &&  $catTaxConfi == 1 && $productTaxConfi == 1)
    			{
    				$shippingTax= $this->taxMappingRepositoryManager->getShippingTax($countryId,$stateId,$productCat,$productId);
    			}
    		}
    
    	}
    	return $shippingTax ;
    }
    
    public function getTaxMasterById ($taxId) {
    	return $this->taxMasterRepositoryManager->getTaxMasterById($taxId);
    }
    public function getTaxMasterByIdObj ($taxId) {
    	return $this->taxMasterRepositoryManager->getTaxMasterByIdObj($taxId);
    }
    public function getTaxMappingById ($taxMappingId) {
    	return $this->taxMappingRepositoryManager->getTaxMappingById($taxMappingId);
    }
    
    public function getCategoryList($categories,$countryLanguageId)
    {
    	$sub_category = array();
    	$has_child = 'no';
    	foreach ($categories as $index=>$category)
    	{
    		if (isset($category['child']))
    		{
    			$has_child = 'yes';
    			$item_type = 'product-category';
    		}
    		else
    		{
    			$has_child = 'no';
    			$item_type = 'category-product-list';
    		}
    		if ($has_child == 'yes')
    		{
    			$this->getCategoryList($category['child'], $countryLanguageId);
    		}
    		else
    		{
    		 	$catgeoryArray[$category['cat_master_id']] = $category['cat_name'];
    		}
    	}
    	return $catgeoryArray;
    }
    
    
}