<?php

namespace OfficeBrain\Bundle\ProductBundle\Manager\AbstractManager;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Boolean;
use Doctrine\ORM\EntityManager;

abstract class ProductAbstract
{
	protected $productImageUrl;
	
	/**
	 * @author Employee Id: 4532
	 * OB eCommerce Product - Product Info Abstract class
	 * Initialize the required variable on class load
	 */
	public function __construct($request, $productApi, $serviceContainer, $commonLibraryManager, $entityManager)
	{ 
		$this->productImageUrl = '';
		$this->request = $request;
		$this->productApi = $productApi;
		$this->serviceContainer = $serviceContainer;
		$this->commonLibraryManager = $commonLibraryManager;
		$this->projectSetting = $this->request->get('project_setting');
		$this->productAccessRules = $this->projectSetting['access_rule'];
		$this->productConfigurationRules = $this->projectSetting['configuration_rule'];
		$this->entityManager = $entityManager;
		$this->userRepositoryManager = $this->entityManager->getRepository('OfficeBrainUserBundle:User');
		$this->orderSampleRepositoryManager = $this->entityManager->getRepository('OfficeBrainProductBundle:OrderSample');
		$this->productExtraChargeRepositoryManager = $this->entityManager->getRepository('OfficeBrainProductBundle:ProductExtraCharge');
	}
	
	public function getPaginatorService()
	{
		return $this->paginatorService;
	}
	
	public function setPaginatorService($paginatorService)
	{
		$this->paginatorService = $paginatorService;
	}
	
	public function getProjectSetting()
	{
		return $this->paginatorService;
	}
	
	public function setProjectSetting($projectSetting)
	{
		$this->projectSetting = $projectSetting;
	}
	
    public function getProductDetailBySlug($productSlug, $culture)
    {
    }
    
    public function getProductDetailById($productId, $culture)
    {
    }
    
    public function getExtraChargeByProductId($productId){
		return $this->productExtraChargeRepositoryManager->getExtraChargeByProductId($productId);
	}
	
	public function setProductExtraCharge($object,$data){
		return $this->productExtraChargeRepositoryManager->setProductExtraCharge($object,$data);
	}
	
    public function downloadImageZip($productSKU, $supplierId)
    {	
    }
    
    public function downloadPdfTemplate($productSKU, $supplierId)
    {
    }

    public function downloadFileFromRemoteToLocal($downloadPathUrlAbsolute, $supplierId, $productImageUrl, $downloadPathUrlRelative, $filename)
    {	
    	$folderName = explode("/",$filename);
    	$sourceZipFile = $productImageUrl.$supplierId.'/'.$filename;
    	$destinationZip = $downloadPathUrlAbsolute.$supplierId.'/'.$filename;
    	$destinationRelativeZip = $downloadPathUrlRelative.$supplierId.'/'.$filename;
    	$destinationZipFolder =  $downloadPathUrlRelative.$supplierId."/".$folderName[0];
    	$destinationZipFolder1 =  $downloadPathUrlRelative.$supplierId;
    	$destinationAbsoluteZipFolder = $downloadPathUrlRelative.$supplierId.'/'.$filename;
    	
	    if (!file_exists($destinationRelativeZip)) {
	    	$this->commonLibraryManager->makeDir($destinationZipFolder,$downloadPathUrlRelative);
	    	$this->commonLibraryManager->saveUrlToFile($sourceZipFile, $destinationRelativeZip,$destinationAbsoluteZipFolder);
	    }
	   	$this->forceFullyDownloadFile($filename, $destinationZipFolder1);
    	
    }
    
    public function forceFullyDownloadFile($filename, $dynamicFilePath)
    {
    	$downloadFile = $dynamicFilePath.'/'.$filename;
    	header('Content-Description: File Transfer');
    	header('Content-Type: application/octet-stream');
    	header('Content-Disposition: attachment; filename=' .$filename);
    	header('Expires: 0');
    	header('Cache-Control: must-revalidate');
    	header('Pragma: public');
    	readfile($downloadFile);
    	echo 'done'; die;
    }
    
    public function checkRemoteFileAvaibility($sku, $supplierId, $type)
    {
    	if($type == 'pdf'){
    		$filename = $this->serviceContainer->getParameter('product_image.base_url.templete').$sku.'.'.$type;
    	}else{
    		$filename = $this->serviceContainer->getParameter('product_image.base_url.high_res').$sku.'.'.$type;
    	}
    	$productImageUrl =  $this->serviceContainer->getParameter('product_template.base_url');
    	
    	$sourceZipFile = $productImageUrl.$supplierId.'/'.$filename;
    	if($this->commonLibraryManager->checkFileExistOnRemoteServer($sourceZipFile)){
    		return true;
    	}
    	return false;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to check weather the file is exists or not using URL of the file (Not the path)
     *
     * @param : File Url
     *
     * @return Boolean
     *
     * @throws Exception: Null
     */
    public function is_file_exists($file){
        $file_headers = @get_headers($file);
        if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
            $exists = false;
        }
        else {
            $exists = true;
        }
        return $exists;
    }
    /**
     * @author Employee Id: 4488
     *
     * Description : This function is used to check weather ob customer id exist in user
     *
     */
    public function getObCustomerId($obCustomerId='')
    {
//     	$this->userRepositoryManager = $this->entityManager->getRepository('OfficeBrainUserBundle:User');
    	return $this->userRepositoryManager->existObCustomerIdUser($obCustomerId);
    }

	
	 /**
    *
    * Description : This function is used to save contact us fields and send mail 
    *
    * @param : None
    *
    * @return NULL
    *
    * @throws Exception: Null
    */
    public function addOrderSample($requestSampleData, $productData,$userinfo)
    {  	
		$requestSample = $this->orderSampleRepositoryManager->addOrderSample($requestSampleData,$productData,$userinfo);
		
		if ($requestSample) {				 
		//    // after saving to db send mail from here ---- 			
		//    $sendEmail = $this->sendContactUsEmail($contactUsData, " Contact Us Form Enquiry ", $emailService);			
									
		   return array('status'=>'SUCCESS','data'=>$requestSample);
		}
		
		return array('status'=>'FAIL');
    }
    
}
