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

use Symfony\Component\DependencyInjection\ContainerInterface as Container;
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;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\Exception\FileException;

/**
 *
 * @author Employee Id: 4471
 * OB eCommerce Product - CurrencyConversionA
 * CurrencyConversionAbstactAbstract Class Add to define the common function and logic for currency conversion
 */

abstract class CurrencyConversionAbstract
{
    private $entityManager;
    protected $request;

    /**
     * @author Employee Id: 4273
     * Initialize the required variable on class load
     *
     */
    public function __construct($entityManager, $request)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
		$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->currencyConversionRepositoryManager = $this->entityManager->getRepository('OfficeBrainCoreBundle:CurrencyConversion');
        $this->currencyConversionRepositoryManager->setProjectSetting($this->projectSetting);
		
    }
	
	public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->createdUid);
        unset($this->upatedUid);
        unset($this->currencyConversionRepositoryManager);
	}
	
	
	public function addCurrencyConversion($data)
	{
		$result=$this->currencyConversionRepositoryManager->addCurrencyConversion($data);
		if ($result) {
			return 'Sucess';
		}
		else
		{
			return 'Error';
		}
	}
	
	public function getCurrencyConversionList()
	{
		return $this->currencyConversionRepositoryManager->getCurrencyConversionList();
	}
	
	public function getCurrencyConversionDetailById($ccId)
	{
		return $this->currencyConversionRepositoryManager->getCurrencyConversionDetailById($ccId);
	}
	
	public function deleteCurrencyConversionList($ccId)
	{
		return $this->currencyConversionRepositoryManager->deleteCurrencyConversionList($ccId);
	}
	
	public function getCurrencyCodeList()
	{
		$currencyCode = array();
		$currencyCodedetail=$this->entityManager->getRepository('OfficeBrainCoreBundle:Country')->getCurrencyCodeList();
		foreach ($currencyCodedetail as $key => $value) {
			foreach ($value as $key => $value1) {
				$currencyCode[$value1]=$value1;
			}
		}
		return $currencyCode;
	}
	
	public function editCurrencyConversion($data)
	{
		$result=$this->currencyConversionRepositoryManager->editCurrencyConversion($data);
		if ($result) {
			return 'Sucess';
		}
		else
		{
			return 'Error';
		}
	}
	

    public  function convertCurrencyRate($fromCurrency, $toCurrency){
    	$url  = "https://www.google.com/finance/converter?a=1&from=$fromCurrency&to=$toCurrency";
    	$data = file_get_contents($url);
    	preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
    	$converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    	return round($converted, 2);
    }
	
	public function updateStatusCurrencyConversion()
	{
		$data=array();
		$data['id'] = $this->request->get('id');
		$data['status'] = $this->request->get('status');
		return $this->currencyConversionRepositoryManager->updateStatusCurrencyConversion($data);
	}

}
